Refactor Jar resource access and entry list

* Add getResource(), extracted from getClass()
* Remove getInputStream(), using getResource() instead
* Change getEntries() to getEntryNames(), hiding JarEntry
* Store filename for getFilename()
This commit is contained in:
Agaricus 2013-03-02 16:54:56 -08:00
parent 61270b1259
commit 9fcbdce6e7
2 changed files with 30 additions and 14 deletions

View File

@ -56,6 +56,7 @@ public class Jar {
private final JarFile file; private final JarFile file;
private final String main; private final String main;
private final String filename;
private final Set<String> contains = new HashSet<String>(); private final Set<String> contains = new HashSet<String>();
private final Map<String, ClassNode> classes = new HashMap<String, ClassNode>(); private final Map<String, ClassNode> classes = new HashMap<String, ClassNode>();
@ -63,13 +64,20 @@ public class Jar {
return contains.contains(clazz) ? true : getClass(clazz) != null; return contains.contains(clazz) ? true : getClass(clazz) != null;
} }
public InputStream getResource(String name) throws IOException {
ZipEntry e = file.getEntry(name);
return e == null ? null : file.getInputStream(e);
}
public InputStream getClass(String clazz) { public InputStream getClass(String clazz) {
try { try {
ZipEntry e = file.getEntry(clazz + ".class"); InputStream inputStream = getResource(clazz + ".class");
if (e != null) {
if (inputStream != null) {
contains.add(clazz); contains.add(clazz);
} }
return e == null ? null : file.getInputStream(e); return inputStream;
} catch (IOException ex) { } catch (IOException ex) {
throw new RuntimeException(ex); throw new RuntimeException(ex);
} }
@ -102,15 +110,22 @@ public class Jar {
} }
public String getFilename() { public String getFilename() {
return file.getName(); return filename;
} }
public InputStream getInputStream(ZipEntry zipEntry) throws IOException { /**
return file.getInputStream(zipEntry); * Get all filenames in the jar (preserves archive order)
*/
public List<String> getEntryNames() {
List<String> entryNames = new ArrayList<String>();
for (Enumeration<JarEntry> entr = file.entries(); entr.hasMoreElements();) {
JarEntry entry = entr.nextElement();
entryNames.add(entry.getName());
} }
public Enumeration<JarEntry> getEntries() { return entryNames;
return file.entries();
} }
public static Jar init(String jar) throws IOException { public static Jar init(String jar) throws IOException {
@ -120,6 +135,7 @@ public class Jar {
public static Jar init(File file) throws IOException { public static Jar init(File file) throws IOException {
JarFile jarFile = new JarFile(file); JarFile jarFile = new JarFile(file);
String filename = file.getName();
String main = null; String main = null;
Manifest manifest = jarFile.getManifest(); Manifest manifest = jarFile.getManifest();
@ -133,6 +149,6 @@ public class Jar {
} }
} }
return new Jar(jarFile, main); return new Jar(jarFile, main, filename);
} }
} }

View File

@ -105,12 +105,12 @@ public class JarRemapper extends Remapper {
if (jar == null) { if (jar == null) {
return; return;
} }
for (Enumeration<JarEntry> entr = jar.getEntries(); entr.hasMoreElements();) { for (String name : jar.getEntryNames()) {
JarEntry entry = entr.nextElement();
InputStream is = jar.getInputStream(entry); JarEntry entry;
InputStream is = jar.getResource(name);
try { try {
String name = entry.getName();
byte[] data; byte[] data;
if (name.endsWith(".class")) { if (name.endsWith(".class")) {
name = name.substring(0, name.length() - CLASS_LEN); name = name.substring(0, name.length() - CLASS_LEN);