throws IOException {
JarFile jarFile = new JarFile(sourceJarFileName);
ArrayList<JarEntry> entries = Collections.list(jarFile.entries());
OutputStream os = new FileOutputStream(destJarFileName);
JarOutputStream out = new JarOutputStream(os);
int n = 0;
for (JarEntry entry : entries) {
if (entry.getName().endsWith(".class"))
n++;
}
displayStartMessage(n);
for (JarEntry entry : entries) {
String name = entry.getName();
InputStream dataStream = null;
if (name.endsWith(".class")) {
// weave class
InputStream is = jarFile.getInputStream(entry);
ByteArrayOutputStream classStream = new ByteArrayOutputStream();
if (weave(is, name, classStream)) {
// class file was modified
weavedClassCount++;
dataStream = new ByteArrayInputStream(classStream
.toByteArray());
// create new entry
String newName;
if (originalClassName != className) {
String unqualifiedName;
int index = className.lastIndexOf('/');
if (index == -1)
unqualifiedName = className;
else
unqualifiedName = className.substring(index + 1,
className.length());
index = name.lastIndexOf('/');
if (index == -1)
newName = unqualifiedName + ".class";
else
newName = name.substring(0, index + 1)
+ unqualifiedName + ".class";
} else {
newName = name;
}
entry = new JarEntry(newName);
recordFileForVerifier(newName);
}
}
if (dataStream == null) {
// not a class file or class wasn't no
dataStream = jarFile.getInputStream(entry);
}
// writing entry
out.putNextEntry(entry);
// writing data
int len;
final byte[] buf = new byte[1024];
while ((len = dataStream.read(buf)) >= 0) {
out.write(buf, 0, len);
}
}
out.close();
displayEndMessage();
if (verifier != null) {
verifier.verifyJarFile(destJarFileName);