/** * {@inheritDoc} */ public File toJar(File file, Manifest manifest) throws IOException { if (!file.isFile() && !file.createNewFile()) { throw new IllegalArgumentException("Could not create file: " + file); } JarOutputStream outputStream = new JarOutputStream(new FileOutputStream(file), manifest); try { for (Map.Entry<TypeDescription, byte[]> entry : getAuxiliaryTypes().entrySet()) { outputStream.putNextEntry(new JarEntry(entry.getKey().getInternalName() + CLASS_FILE_EXTENSION)); outputStream.write(entry.getValue()); outputStream.closeEntry(); } outputStream.putNextEntry(new JarEntry(typeDescription.getInternalName() + CLASS_FILE_EXTENSION)); outputStream.write(binaryRepresentation); outputStream.closeEntry(); } finally { outputStream.close(); } return file; }
/** * Jars a given directory or single file into a JarOutputStream. */ public void jarDir(File dirOrFile2Jar, File destJar) throws IOException { if (dirOrFile2Jar == null || destJar == null) { throw new IllegalArgumentException(); } mDestJarName = destJar.getCanonicalPath(); FileOutputStream fout = new FileOutputStream(destJar); JarOutputStream jout = new JarOutputStream(fout); //jout.setLevel(0); try { jarDir(dirOrFile2Jar, jout, null); } catch (IOException ioe) { throw ioe; } finally { jout.close(); fout.close(); } }
JarOutputStream jar = new JarOutputStream(mem); DataOutputStream bc = new DataOutputStream(jar); for( CtClass c : updated ) { jar.putNextEntry(new JarEntry(c.getName().replace('.', '/') + ".class")); c.toBytecode(bc); bc.flush(); packages.add(p); jar.close(); weavePackages(packages.toArray(new String[0])); AddJar task = new AddJar();
/** * The minimum requirement to have a "valid" archive plugin is to include * findbugs.xml, messages.xml and MANIFEST.MF files. The rest of the * resources are load using the parent ClassLoader (Not requires to be in * the jar). * <p> * Instead of building a file on disk, the result of the stream is kept in * memory and return as a byte array. * * @return * @throws IOException * @throws URISyntaxException */ private byte[] buildFakePluginJar() throws IOException, URISyntaxException { ClassLoader cl = getClass().getClassLoader(); ByteArrayOutputStream buffer = new ByteArrayOutputStream(); JarOutputStream jar = new JarOutputStream(buffer); final URL metadata = cl.getResource("metadata"); if (metadata != null) { final File dir = new File(metadata.toURI()); //Add files to the jar stream addFilesToStream(cl, jar, dir, ""); } jar.finish(); jar.close(); return buffer.toByteArray(); }
/** * Closes the GEDCOM X output stream as well as the stream being filtered. */ public void close() throws IOException { this.gedxOutputStream.putNextEntry(new JarEntry(JarFile.MANIFEST_NAME)); this.mf.write(this.gedxOutputStream); this.gedxOutputStream.close(); } }
private void createJar(String className, OutputStream outStream, byte[] bytes) throws IOException { JarOutputStream jarOutputStream = new JarOutputStream(outStream); // Add the class file to the JAR file String formattedName = className; if (!formattedName.endsWith(".class")) { formattedName = formattedName.concat(".class"); } JarEntry entry = new JarEntry(formattedName); entry.setTime(System.currentTimeMillis()); jarOutputStream.putNextEntry(entry); jarOutputStream.write(bytes); jarOutputStream.closeEntry(); jarOutputStream.close(); }
try { File jarFile = manifestJar.toFile(); OutputStream os = new FileOutputStream(jarFile); jos = new JarOutputStream(os, manifest); } catch (IOException e) { e.printStackTrace(); } finally { if (jos != null) { jos.close();
private void zeroOutArchive(FileObject garbage) throws IOException { OutputStream fileToOverwrite = garbage.getOutputStream(); try { JarOutputStream jos = new JarOutputStream(fileToOverwrite); try { jos.putNextEntry(new ZipEntry("META-INF/MANIFEST.MF")); // NOI18N // UTF-8 guaranteed on any platform jos.write("Manifest-Version: 1.0\n".getBytes("UTF-8")); // NOI18N } finally { jos.close(); } } finally { fileToOverwrite.close(); } }
public static byte[] jarFiles(String path) throws IOException { ByteArrayOutputStream bos = new ByteArrayOutputStream(); JarOutputStream jarOut = new JarOutputStream(bos); int size = addFiles(jarOut, "", new File(path)); if (size == 0) { // no files in folder return null; } jarOut.close(); return bos.toByteArray(); }
private void next() throws IOException { ArtifactData current = (m_iter.hasNext()) ? m_iter.next() : null; if (current == null) { m_output.close(); } else if (!m_fixPack || current.hasChanged()) { m_current = current.getUrl().openStream(); m_output.putNextEntry(new ZipEntry(current.getFilename())); } else { next(); } }
/** * {@inheritDoc} */ public Map<String, Class<?>> injectRaw(Map<? extends String, byte[]> types) { File jarFile = new File(folder, JAR + randomString.nextString() + "." + JAR); try { if (!jarFile.createNewFile()) { throw new IllegalStateException("Cannot create file " + jarFile); } JarOutputStream jarOutputStream = new JarOutputStream(new FileOutputStream(jarFile)); try { for (Map.Entry<? extends String, byte[]> entry : types.entrySet()) { jarOutputStream.putNextEntry(new JarEntry(entry.getKey().replace('.', '/') + CLASS_FILE_EXTENSION)); jarOutputStream.write(entry.getValue()); } } finally { jarOutputStream.close(); } target.inject(instrumentation, new JarFile(jarFile)); Map<String, Class<?>> result = new HashMap<String, Class<?>>(); for (String name : types.keySet()) { result.put(name, Class.forName(name, false, ClassLoader.getSystemClassLoader())); } return result; } catch (IOException exception) { throw new IllegalStateException("Cannot write jar file to disk", exception); } catch (ClassNotFoundException exception) { throw new IllegalStateException("Cannot load injected class", exception); } }
try (FileOutputStream outputStream = new FileOutputStream(jarFile)) { JarOutputStream jarOutputStream = manifest != null ? new JarOutputStream(outputStream, manifest) : new JarOutputStream(outputStream); jarOutputStream.close();
private void zeroOutArchive(FileObject garbage) throws IOException { OutputStream fileToOverwrite = garbage.getOutputStream(); try { JarOutputStream jos = new JarOutputStream(fileToOverwrite); try { jos.putNextEntry(new ZipEntry("META-INF/MANIFEST.MF")); // NOI18N // UTF-8 guaranteed on any platform jos.write("Manifest-Version: 1.0\n".getBytes("UTF-8")); // NOI18N } finally { jos.close(); } } finally { fileToOverwrite.close(); } } }
static InputStream getSurrogateInput(Dictionary compositeManifest, BundleDescription compositeDesc, ExportPackageDescription[] matchingExports) throws IOException { // use an in memory stream to store the content ByteArrayOutputStream bytesOut = new ByteArrayOutputStream(); Manifest manifest = CompositeHelper.getSurrogateManifest(compositeManifest, compositeDesc, matchingExports); JarOutputStream jarOut = new JarOutputStream(bytesOut, manifest); jarOut.flush(); jarOut.close(); return new ByteArrayInputStream(bytesOut.toByteArray()); }
private static Set<String> buildJar(ClassManager classManager, ClassBuilder builder, String outPath) throws IOException { Set<String> classNames = classManager.getFrameworkClassNames(); JarOutputStream out = new JarOutputStream(new FileOutputStream(outPath)); for (String className : classNames) { VirtualClass virtualClass = classManager.getVirtualClass(className); ClassDef classDef = virtualClass.getClassDef(); byte[] b = builder.build(classDef); String entryName = className.substring(1, className.length() - 1); out.putNextEntry(new ZipEntry(entryName + ".class")); out.write(b); out.closeEntry(); } out.close(); return classNames; }
private void addMetaInf( File outputFile, ArrayList<File> jarFiles ) throws IOException { File tmp = File.createTempFile( outputFile.getName(), ".add", outputFile.getParentFile() ); FileOutputStream fos = new FileOutputStream( tmp ); JarOutputStream zos = new JarOutputStream( fos ); Set<String> entries = new HashSet<String>(); updateWithMetaInf( zos, outputFile, entries, false ); for ( File f : jarFiles ) { updateWithMetaInf( zos, f, entries, true ); } if ( transformers != null ) { for ( ResourceTransformer transformer : transformers ) { if ( transformer.hasTransformedResource() ) { transformer.modifyOutputStream( zos ); } } } zos.close(); outputFile.delete(); if ( ! tmp.renameTo( outputFile ) ) { throw new IOException( String.format( "Cannot rename %s to %s", tmp, outputFile.getName() ) ); } }
/** * creates a JarInputStream containing the passed text files. Each Pair<String */ public static InputStream jarInputStream(TextFile... files) { try { ByteArrayOutputStream out2 = new ByteArrayOutputStream(); JarOutputStream jo = new JarOutputStream(new BufferedOutputStream(out2)); for (TextFile textFile : files) { JarEntry je = new JarEntry(textFile.path); jo.putNextEntry(je); byte[] bytes = textFile.content.getBytes(); jo.write(bytes, 0, bytes.length); } jo.close(); return new ByteArrayInputStream(out2.toByteArray()); } catch (IOException e) { throw new WrappedException(e); } }
static InputStream getSurrogateInput(Dictionary compositeManifest, BundleDescription compositeDesc, ExportPackageDescription[] matchingExports) throws IOException { // use an in memory stream to store the content ByteArrayOutputStream bytesOut = new ByteArrayOutputStream(); Manifest manifest = CompositeHelper.getSurrogateManifest(compositeManifest, compositeDesc, matchingExports); JarOutputStream jarOut = new JarOutputStream(bytesOut, manifest); jarOut.flush(); jarOut.close(); return new ByteArrayInputStream(bytesOut.toByteArray()); }
/** * Create a JAR using the given file contents and with the given file name. * * @param fileName Name of the file to create * @param content Content of the created file * @return The JAR file content * @throws IOException If there is a problem creating the output stream for the JAR file. */ public byte[] createJarFromFileContent(final String fileName, final String content) throws IOException { ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); JarOutputStream jarOutputStream = new JarOutputStream(byteArrayOutputStream); JarEntry entry = new JarEntry(fileName); entry.setTime(System.currentTimeMillis()); jarOutputStream.putNextEntry(entry); jarOutputStream.write(content.getBytes()); jarOutputStream.closeEntry(); jarOutputStream.close(); return byteArrayOutputStream.toByteArray(); }
@Before public void run() throws IOException { Manifest manifest = new Manifest(); manifest.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION, "1.0"); tempJarPath = Files.createTempFile("test", ".jar"); JarOutputStream target = new JarOutputStream(new FileOutputStream(tempJarPath.toFile()), manifest); File startingDirectory = Paths.get(System.getProperty("user.dir"), "src", "test", "java").toFile(); addFileToJar(startingDirectory, startingDirectory, target); target.close(); }