List of usage examples for java.util.jar JarOutputStream putNextEntry
public void putNextEntry(ZipEntry ze) throws IOException
From source file:com.wolvereness.overmapped.OverMapped.java
private void writeToFile(final MultiProcessor executor, final Map<String, ByteClass> byteClasses, final List<Pair<ZipEntry, byte[]>> fileEntries, final BiMap<String, String> nameMaps, final BiMap<Signature, Signature> signatureMaps, final Map<Signature, Integer> flags) throws IOException, FileNotFoundException, InterruptedException, ExecutionException { final Collection<Future<Pair<ZipEntry, byte[]>>> classWrites = newArrayList(); for (final ByteClass clazz : byteClasses.values()) { classWrites.add(//from w w w. j a v a 2s . co m executor.submit(clazz.callable(signatureMaps, nameMaps, byteClasses, flags, correctEnums))); } FileOutputStream fileOut = null; JarOutputStream jar = null; try { jar = new JarOutputStream(fileOut = new FileOutputStream(output)); for (final Pair<ZipEntry, byte[]> fileEntry : fileEntries) { jar.putNextEntry(fileEntry.getLeft()); jar.write(fileEntry.getRight()); } for (final Future<Pair<ZipEntry, byte[]>> fileEntryFuture : classWrites) { final Pair<ZipEntry, byte[]> fileEntry = fileEntryFuture.get(); jar.putNextEntry(fileEntry.getLeft()); jar.write(fileEntry.getRight()); } } finally { if (jar != null) { try { jar.close(); } catch (final IOException ex) { } } if (fileOut != null) { try { fileOut.close(); } catch (final IOException ex) { } } } }
From source file:com.android.builder.testing.MockableJarGenerator.java
/** * Writes a modified *.class file to the output JAR file. *//* w ww . j ava 2 s. c o m*/ private void rewriteClass(JarEntry entry, InputStream inputStream, JarOutputStream outputStream) throws IOException { ClassReader classReader = new ClassReader(inputStream); ClassNode classNode = new ClassNode(Opcodes.ASM5); classReader.accept(classNode, EMPTY_FLAGS); modifyClass(classNode); ClassWriter classWriter = new ClassWriter(ClassWriter.COMPUTE_MAXS | ClassWriter.COMPUTE_FRAMES); classNode.accept(classWriter); outputStream.putNextEntry(new ZipEntry(entry.getName())); outputStream.write(classWriter.toByteArray()); }
From source file:org.echocat.nodoodle.transport.HandlerPacker.java
protected void writeJarResource(JarResource jarResource, JarOutputStream jar, String fileName) throws IOException { if (jar == null) { throw new NullPointerException(); }//w w w . j a v a 2s.com if (fileName == null) { throw new NullPointerException(); } if (jarResource != null) { final InputStream inputStream = jarResource.getResourceAsJar(); try { final JarEntry e = new JarEntry(fileName); jar.putNextEntry(e); IOUtils.copy(inputStream, jar); jar.closeEntry(); } finally { inputStream.close(); } } }
From source file:UnpackedJarFile.java
public static void copyToPackedJar(JarFile inputJar, File outputFile) throws IOException { if (inputJar.getClass() == JarFile.class) { // this is a plain old jar... nothign special copyFile(new File(inputJar.getName()), outputFile); } else if (inputJar instanceof NestedJarFile && ((NestedJarFile) inputJar).isPacked()) { NestedJarFile nestedJarFile = (NestedJarFile) inputJar; JarFile baseJar = nestedJarFile.getBaseJar(); String basePath = nestedJarFile.getBasePath(); if (baseJar instanceof UnpackedJarFile) { // our target jar is just a file in upacked jar (a plain old directory)... now // we just need to find where it is and copy it to the outptu copyFile(((UnpackedJarFile) baseJar).getFile(basePath), outputFile); } else {//from w w w. ja va2 s.c o m // out target is just a plain old jar file directly accessabel from the file system copyFile(new File(baseJar.getName()), outputFile); } } else { // copy out the module contents to a standalone jar file (entry by entry) JarOutputStream out = null; try { out = new JarOutputStream(new FileOutputStream(outputFile)); byte[] buffer = new byte[4096]; Enumeration entries = inputJar.entries(); while (entries.hasMoreElements()) { ZipEntry entry = (ZipEntry) entries.nextElement(); InputStream in = inputJar.getInputStream(entry); try { out.putNextEntry(new ZipEntry(entry.getName())); try { int count; while ((count = in.read(buffer)) > 0) { out.write(buffer, 0, count); } } finally { out.closeEntry(); } } finally { close(in); } } } finally { close(out); } } }
From source file:org.reficio.p2.FeatureBuilder.java
private void addToJar(JarOutputStream jar, File content) throws IOException { for (File f : FileUtils.listFiles(content, null, true)) { String fname = f.getPath().replace("\\", "/"); if (f.isDirectory()) { if (!fname.endsWith("/")) { fname = fname + "/"; }/*w w w .j av a 2 s . co m*/ JarEntry entry = new JarEntry(fname); entry.setTime(f.lastModified()); jar.putNextEntry(entry); jar.closeEntry(); } else { //must be a file JarEntry entry = new JarEntry(fname); entry.setTime(f.lastModified()); jar.putNextEntry(entry); jar.write(IOUtils.toByteArray(new FileInputStream(f))); jar.closeEntry(); } } }
From source file:com.aliyun.odps.mapred.BridgeJobRunner.java
/** * Create jar with jobconf.//from w w w.j av a 2 s.c o m * * @return * @throws OdpsException */ private ByteArrayOutputStream createJarArchive() throws OdpsException { try { ByteArrayOutputStream archiveOut = new ByteArrayOutputStream(); // Open archive file JarOutputStream out = new JarOutputStream(archiveOut, new Manifest()); ByteArrayOutputStream jobOut = new ByteArrayOutputStream(); job.writeXml(jobOut); // Add jobconf entry JarEntry jobconfEntry = new JarEntry("jobconf.xml"); out.putNextEntry(jobconfEntry); out.write(jobOut.toByteArray()); out.close(); return archiveOut; } catch (IOException ex) { throw new OdpsException(ErrorCode.UNEXPECTED.toString(), ex); } }
From source file:org.codehaus.mojo.minijar.resource.ComponentsXmlHandler.java
public void onStopProcessing(JarOutputStream pOutput) throws IOException { if (components.size() == 0) { // no components information available return;//from ww w .j a va 2 s .co m } final Xpp3Dom dom = new Xpp3Dom("component-set"); final Xpp3Dom componentDom = new Xpp3Dom("components"); dom.addChild(componentDom); for (Iterator it = components.values().iterator(); it.hasNext();) { final Xpp3Dom component = (Xpp3Dom) it.next(); componentDom.addChild(component); } // insert aggregated license information into new jar pOutput.putNextEntry(new JarEntry(COMPONENTS_XML_PATH)); Xpp3DomWriter.write(new OutputStreamWriter(pOutput), dom); }
From source file:rapture.kernel.JarApiImplTest.java
private void add(File source, JarOutputStream target) throws IOException { BufferedInputStream in = null; try {/*from w w w .j a v a 2 s. c o m*/ if (source.isDirectory()) { String name = source.getPath().replace("\\", "/"); if (!name.isEmpty()) { if (!name.endsWith("/")) { name += "/"; } JarEntry entry = new JarEntry(name); entry.setTime(source.lastModified()); target.putNextEntry(entry); target.closeEntry(); } for (File nestedFile : source.listFiles()) { add(nestedFile, target); } return; } JarEntry entry = new JarEntry(source.getPath().replace("\\", "/")); entry.setTime(source.lastModified()); target.putNextEntry(entry); in = new BufferedInputStream(new FileInputStream(source)); byte[] buffer = new byte[1024]; while (true) { int count = in.read(buffer); if (count == -1) { break; } target.write(buffer, 0, count); } target.closeEntry(); } finally { if (in != null) { in.close(); } } }
From source file:com.wolvereness.renumerated.Renumerated.java
private void process() throws Throwable { validateInput();/* w w w.j ava 2 s .co m*/ final MultiProcessor executor = MultiProcessor.newMultiProcessor(cores - 1, new ThreadFactoryBuilder().setDaemon(true) .setNameFormat(Renumerated.class.getName() + "-processor-%d") .setUncaughtExceptionHandler(this).build()); final Future<?> fileCopy = executor.submit(new Callable<Object>() { @Override public Object call() throws Exception { if (original != null) { if (original.exists()) { original.delete(); } Files.copy(input, original); } return null; } }); final List<Pair<ZipEntry, Future<byte[]>>> fileEntries = newArrayList(); final List<Pair<MutableObject<ZipEntry>, Future<byte[]>>> classEntries = newArrayList(); { final ZipFile input = new ZipFile(this.input); final Enumeration<? extends ZipEntry> inputEntries = input.entries(); while (inputEntries.hasMoreElements()) { final ZipEntry entry = inputEntries.nextElement(); final Future<byte[]> future = executor.submit(new Callable<byte[]>() { @Override public byte[] call() throws Exception { return ByteStreams.toByteArray(input.getInputStream(entry)); } }); if (entry.getName().endsWith(".class")) { classEntries.add(new MutablePair<MutableObject<ZipEntry>, Future<byte[]>>( new MutableObject<ZipEntry>(entry), future)); } else { fileEntries.add(new ImmutablePair<ZipEntry, Future<byte[]>>(entry, future)); } } for (final Pair<MutableObject<ZipEntry>, Future<byte[]>> pair : classEntries) { final byte[] data = pair.getRight().get(); pair.setValue(executor.submit(new Callable<byte[]>() { String className; List<String> fields; @Override public byte[] call() throws Exception { try { return method(); } catch (final Exception ex) { throw new Exception(pair.getLeft().getValue().getName(), ex); } } private byte[] method() throws Exception { final ClassReader clazz = new ClassReader(data); clazz.accept(new ClassVisitor(ASM4) { @Override public void visit(final int version, final int access, final String name, final String signature, final String superName, final String[] interfaces) { if (superName.equals("java/lang/Enum")) { className = name; } } @Override public FieldVisitor visitField(final int access, final String name, final String desc, final String signature, final Object value) { if (className != null && (access & 0x4000) != 0) { List<String> fieldNames = fields; if (fieldNames == null) { fieldNames = fields = newArrayList(); } fieldNames.add(name); } return null; } }, ClassReader.SKIP_CODE); if (className == null) return data; final String classDescriptor = Type.getObjectType(className).getDescriptor(); final ClassWriter writer = new ClassWriter(0); clazz.accept(new ClassVisitor(ASM4, writer) { @Override public MethodVisitor visitMethod(final int access, final String name, final String desc, final String signature, final String[] exceptions) { final MethodVisitor methodVisitor = super.visitMethod(access, name, desc, signature, exceptions); if (!name.equals("<clinit>")) { return methodVisitor; } return new MethodVisitor(ASM4, methodVisitor) { final Iterator<String> it = fields.iterator(); boolean active; String lastName; @Override public void visitTypeInsn(final int opcode, final String type) { if (!active && it.hasNext()) { // Initiate state machine if (opcode != NEW) throw new AssertionError("Unprepared for " + opcode + " on " + type + " in " + className); active = true; } super.visitTypeInsn(opcode, type); } @Override public void visitLdcInsn(final Object cst) { if (active && lastName == null) { if (!(cst instanceof String)) throw new AssertionError( "Unprepared for " + cst + " in " + className); // Switch the first constant in the Enum constructor super.visitLdcInsn(lastName = it.next()); } else { super.visitLdcInsn(cst); } } @Override public void visitFieldInsn(final int opcode, final String owner, final String name, final String desc) { if (opcode == PUTSTATIC && active && lastName != null && owner.equals(className) && desc.equals(classDescriptor) && name.equals(lastName)) { // Finish the current state machine active = false; lastName = null; } super.visitFieldInsn(opcode, owner, name, desc); } }; } }, ClassReader.EXPAND_FRAMES); final MutableObject<ZipEntry> key = pair.getLeft(); key.setValue(new ZipEntry(key.getValue().getName())); return writer.toByteArray(); } })); } for (final Pair<ZipEntry, Future<byte[]>> pair : fileEntries) { pair.getRight().get(); } input.close(); } fileCopy.get(); FileOutputStream fileOut = null; JarOutputStream jar = null; try { jar = new JarOutputStream(fileOut = new FileOutputStream(output)); for (final Pair<ZipEntry, Future<byte[]>> fileEntry : fileEntries) { jar.putNextEntry(fileEntry.getLeft()); jar.write(fileEntry.getRight().get()); } for (final Pair<MutableObject<ZipEntry>, Future<byte[]>> classEntry : classEntries) { final byte[] data = classEntry.getRight().get(); final ZipEntry entry = classEntry.getLeft().getValue(); entry.setSize(data.length); jar.putNextEntry(entry); jar.write(data); } } finally { if (jar != null) { try { jar.close(); } catch (final IOException ex) { } } if (fileOut != null) { try { fileOut.close(); } catch (final IOException ex) { } } } final Pair<Thread, Throwable> uncaught = this.uncaught; if (uncaught != null) throw new MojoExecutionException(String.format("Uncaught exception in %s", uncaught.getLeft()), uncaught.getRight()); }
From source file:org.commonjava.web.test.fixture.JarKnockouts.java
public void rewriteJar(final File source, final File targetDir) throws IOException { targetDir.mkdirs();/*from ww w . java 2 s .co m*/ final File target = new File(targetDir, source.getName()); JarFile in = null; JarOutputStream out = null; try { in = new JarFile(source); final BufferedOutputStream fos = new BufferedOutputStream(new FileOutputStream(target)); out = new JarOutputStream(fos, in.getManifest()); final Enumeration<JarEntry> entries = in.entries(); while (entries.hasMoreElements()) { final JarEntry entry = entries.nextElement(); if (!knockout(entry.getName())) { final InputStream stream = in.getInputStream(entry); out.putNextEntry(entry); copy(stream, out); out.closeEntry(); } } } finally { closeQuietly(out); if (in != null) { try { in.close(); } catch (final IOException e) { } } } }