1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
|
package codesize;
import java.io.*;
import java.util.*;
import java.util.zip.*;
import org.apache.bcel.classfile.*;
public class Codesize
{
static byte buf[] = new byte[2048];
static ByteArrayOutputStream bufOutputStream = new ByteArrayOutputStream();
public static class Item {
int ttlCodeSize;
Item(int ttlCodeSize) {
this.ttlCodeSize = ttlCodeSize;
}
public int getCodeSize() { return ttlCodeSize; }
}
static int processClassInputStream(InputStream inputStream, String filename)
throws IOException
{
int result = 0;
ClassParser classParser = new ClassParser(inputStream, filename);
Method methods[] = classParser.parse().getMethods();
for(int i=0; i<methods.length; i++) {
Code code = methods[i].getCode();
if(code != null)
result += code.getCode().length;
}
return result;
}
public static Item processZipFile(File zipFile)
{
try {
ZipInputStream inputStream = new ZipInputStream(
new BufferedInputStream(new FileInputStream(zipFile))
);
try {
return processZipFile(zipFile, inputStream);
} finally {
inputStream.close();
}
} catch(IOException e) {
System.err.println("Ignoring " + zipFile + ": " + e.getMessage());
}
return null;
}
public static Item processZipFile(File zipFile, ZipInputStream inputStream)
throws IOException
{
int ttlCodeSize = 0;
ZipEntry zipEntry;
while((zipEntry = inputStream.getNextEntry()) != null) {
if(zipEntry.getName().toLowerCase().endsWith(".class")) {
bufOutputStream.reset();
int nRead;
while((nRead = inputStream.read(buf, 0, buf.length)) > -1)
bufOutputStream.write(buf, 0, nRead);
ttlCodeSize += processClassInputStream(
new ByteArrayInputStream(bufOutputStream.toByteArray()),
zipEntry.getName()
);
}
}
if(ttlCodeSize == 0)
throw new IOException("total code size is 0");
return new Item(ttlCodeSize);
}
}
|