/** Generate output JAR-file and packages */ public void outputToJar() throws IOException { // create the manifest final Manifest manifest = new Manifest(); final java.util.jar.Attributes atrs = manifest.getMainAttributes(); atrs.put(java.util.jar.Attributes.Name.MANIFEST_VERSION, "1.2"); final Map map = manifest.getEntries(); // create manifest Enumeration classes = _bcelClasses.elements(); final String now = (new Date()).toString(); final java.util.jar.Attributes.Name dateAttr = new java.util.jar.Attributes.Name("Date"); while (classes.hasMoreElements()) { final JavaClass clazz = (JavaClass) classes.nextElement(); final String className = clazz.getClassName().replace('.', '/'); final java.util.jar.Attributes attr = new java.util.jar.Attributes(); attr.put(dateAttr, now); map.put(className + ".class", attr); } final File jarFile = new File(_destDir, _jarFileName); final JarOutputStream jos = new JarOutputStream(new FileOutputStream(jarFile), manifest); classes = _bcelClasses.elements(); while (classes.hasMoreElements()) { final JavaClass clazz = (JavaClass) classes.nextElement(); final String className = clazz.getClassName().replace('.', '/'); jos.putNextEntry(new JarEntry(className + ".class")); final ByteArrayOutputStream out = new ByteArrayOutputStream(2048); clazz.dump(out); // dump() closes it's output stream out.writeTo(jos); } jos.close(); }
public void dumpClass(JavaClass clazz) { if (_outputType == FILE_OUTPUT || _outputType == BYTEARRAY_AND_FILE_OUTPUT) { File outFile = getOutputFile(clazz.getClassName()); String parentDir = outFile.getParent(); if (parentDir != null) { File parentFile = new File(parentDir); if (!parentFile.exists()) parentFile.mkdirs(); } } try { switch (_outputType) { case FILE_OUTPUT: clazz.dump( new BufferedOutputStream(new FileOutputStream(getOutputFile(clazz.getClassName())))); break; case JAR_OUTPUT: _bcelClasses.addElement(clazz); break; case BYTEARRAY_OUTPUT: case BYTEARRAY_AND_FILE_OUTPUT: case BYTEARRAY_AND_JAR_OUTPUT: case CLASSLOADER_OUTPUT: ByteArrayOutputStream out = new ByteArrayOutputStream(2048); clazz.dump(out); _classes.addElement(out.toByteArray()); if (_outputType == BYTEARRAY_AND_FILE_OUTPUT) clazz.dump( new BufferedOutputStream( new FileOutputStream(getOutputFile(clazz.getClassName())))); else if (_outputType == BYTEARRAY_AND_JAR_OUTPUT) _bcelClasses.addElement(clazz); break; } } catch (Exception e) { e.printStackTrace(); } }