Refine search
public static void addFileToJar(JarOutputStream jar, File source, String entryName) throws IOException { try (BufferedInputStream in = new BufferedInputStream(new FileInputStream(source))) { JarEntry entry = new JarEntry(entryName); entry.setTime(source.lastModified()); jar.putNextEntry(entry); copyStream(in, jar); jar.closeEntry(); } }
/** * {@inheritDoc} */ public void store(Map<TypeDescription, byte[]> binaryRepresentations) throws IOException { for (Map.Entry<TypeDescription, byte[]> entry : binaryRepresentations.entrySet()) { outputStream.putNextEntry(new JarEntry(entry.getKey().getInternalName() + CLASS_FILE_EXTENSION)); outputStream.write(entry.getValue()); outputStream.closeEntry(); } }
public void testJar(JarOutputStream jos) throws Exception { JarEntry je = new JarEntry("foo"); jos.putNextEntry(je); jos.closeEntry(); } }
/** * {@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; }
@ExpectWarning("AM_CREATES_EMPTY_JAR_FILE_ENTRY") void bug(JarOutputStream any, ZipEntry anyZipEntry) throws IOException { any.putNextEntry(anyZipEntry); any.closeEntry(); }
private void packageTopology(Path jar, AbstractStreamsProcessor processor) throws IOException { Manifest manifest = new Manifest(); Attributes attr = manifest.getMainAttributes(); attr.put(Attributes.Name.MANIFEST_VERSION, "1.0"); attr.put(Attributes.Name.MAIN_CLASS, processor.getClass().getCanonicalName()); try (JarOutputStream out = new JarOutputStream( new BufferedOutputStream(new FileOutputStream(jar.toFile())), manifest)) { List<CompilingClassLoader> classLoaders = processor.getClassLoaders(); if (classLoaders != null && !classLoaders.isEmpty()) { for (CompilingClassLoader classLoader : classLoaders) { for (Map.Entry<String, ByteArrayOutputStream> e : classLoader.getClasses().entrySet()) { out.putNextEntry(new ZipEntry(e.getKey().replace(".", "/") + ".class")); out.write(e.getValue().toByteArray()); out.closeEntry(); } } } } } }
/** * Constructs a new {@code JarOutputStream} using an output stream. The * content of the {@code Manifest} must match the JAR entry information * written subsequently to the stream. * * @param os * the {@code OutputStream} to write to * @param manifest * the {@code Manifest} to output for this JAR file. * @throws IOException * if an error occurs creating the {@code JarOutputStream}. */ public JarOutputStream(OutputStream os, Manifest manifest) throws IOException { super(os); if (manifest == null) { throw new NullPointerException("manifest == null"); } this.manifest = manifest; ZipEntry ze = new ZipEntry(JarFile.MANIFEST_NAME); putNextEntry(ze); this.manifest.write(this); closeEntry(); }
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(); }
/** * {@inheritDoc} */ public void retain(Source.Element element) throws IOException { JarEntry entry = element.resolveAs(JarEntry.class); outputStream.putNextEntry(entry == null ? new JarEntry(element.getName()) : entry); InputStream inputStream = element.getInputStream(); try { byte[] buffer = new byte[1024]; int length; while ((length = inputStream.read(buffer)) != -1) { outputStream.write(buffer, 0, length); } } finally { inputStream.close(); } outputStream.closeEntry(); }
/** * 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(); }
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 buildJar(File outputJarFile, List<CompiledSourceCode> compiledSourceCodes) throws IOException { assertThat(outputJarFile).doesNotExist(); try (FileOutputStream outputStream = new FileOutputStream(outputJarFile)) { JarOutputStream jarOutputStream = new JarOutputStream(outputStream); for (CompiledSourceCode compiledSource : compiledSourceCodes) { String formattedName = compiledSource.className.replace(".", "/"); if (!formattedName.endsWith(".class")) { formattedName = formattedName.concat(".class"); } JarEntry entry = new JarEntry(formattedName); entry.setTime(System.currentTimeMillis()); jarOutputStream.putNextEntry(entry); jarOutputStream.write(compiledSource.compiledBytecode); jarOutputStream.closeEntry(); } jarOutputStream.close(); } } }
byte[] replacement = files.remove(jarEntry.getName()); if (replacement == null) { outputStream.putNextEntry(jarEntry); byte[] buffer = new byte[BUFFER_SIZE]; int index; outputStream.putNextEntry(new JarEntry(jarEntry.getName())); outputStream.write(replacement); outputStream.closeEntry(); outputStream.putNextEntry(new JarEntry(entry.getKey())); outputStream.write(entry.getValue()); outputStream.closeEntry();
JarEntry je = new JarEntry(subPath); je.setTime(dirOrFile2jar.lastModified()); jos.putNextEntry(je); jos.flush(); jos.closeEntry(); JarEntry entry = new JarEntry(path + dirOrFile2jar.getName()); entry.setTime(dirOrFile2jar.lastModified()); jos.putNextEntry(entry); while ((mByteCount = fis.read(mBuffer)) != -1) { jos.write(mBuffer, 0, mByteCount); jos.closeEntry(); } catch (IOException ioe) { throw ioe;
jarEntry.setTime(file.lastModified()); jarOutputStream.putNextEntry(jarEntry); jarOutputStream.closeEntry(); jarEntry.setTime(file.lastModified()); jarOutputStream.putNextEntry(jarEntry); jarOutputStream.closeEntry();
private static void writeSelfReferencingJarFile(File jarFile, String... entries) throws IOException { Manifest manifest = new Manifest(); // Without version, the manifest is silently ignored. Ugh! manifest.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION, "1.0"); manifest.getMainAttributes().put(Attributes.Name.CLASS_PATH, jarFile.getName()); Closer closer = Closer.create(); try { FileOutputStream fileOut = closer.register(new FileOutputStream(jarFile)); JarOutputStream jarOut = closer.register(new JarOutputStream(fileOut)); for (String entry : entries) { jarOut.putNextEntry(new ZipEntry(entry)); Resources.copy(ClassPathTest.class.getResource(entry), jarOut); jarOut.closeEntry(); } } catch (Throwable e) { throw closer.rethrow(e); } finally { closer.close(); } }
jos.putNextEntry(e); FileInputStream fis = new FileInputStream(f); byte[] buffer = new byte[64 * 1024]; jos.closeEntry();
private static File createJarFile(File jar) throws IOException, CannotCompileException, NotFoundException { Manifest manifest = new Manifest(); Attributes attrs = manifest.getMainAttributes(); attrs.put(Attributes.Name.MANIFEST_VERSION, "1.0"); attrs.put(new Attributes.Name("Premain-Class"), HotSwapAgent.class.getName()); attrs.put(new Attributes.Name("Agent-Class"), HotSwapAgent.class.getName()); attrs.put(new Attributes.Name("Can-Retransform-Classes"), "true"); attrs.put(new Attributes.Name("Can-Redefine-Classes"), "true"); JarOutputStream jos = null; try { jos = new JarOutputStream(new FileOutputStream(jar), manifest); String cname = HotSwapAgent.class.getName(); JarEntry e = new JarEntry(cname.replace('.', '/') + ".class"); jos.putNextEntry(e); ClassPool pool = ClassPool.getDefault(); CtClass clazz = pool.get(cname); jos.write(clazz.toBytecode()); jos.closeEntry(); } finally { if (jos != null) jos.close(); } return jar; } }
public URL createJarFor( File f, Class<?>... classesToInclude ) throws IOException { try ( FileOutputStream fout = new FileOutputStream( f ); JarOutputStream jarOut = new JarOutputStream( fout ) ) { for ( Class<?> target : classesToInclude ) { String fileName = target.getName().replace( ".", "/" ) + ".class"; jarOut.putNextEntry( new ZipEntry( fileName ) ); jarOut.write( classCompiledBytes( fileName ) ); jarOut.closeEntry(); } } return f.toURI().toURL(); }
private static File createJarFile(File jar) throws IOException, CannotCompileException, NotFoundException { Manifest manifest = new Manifest(); Attributes attrs = manifest.getMainAttributes(); attrs.put(Attributes.Name.MANIFEST_VERSION, "1.0"); attrs.put(new Attributes.Name("Premain-Class"), HotSwapAgent.class.getName()); attrs.put(new Attributes.Name("Agent-Class"), HotSwapAgent.class.getName()); attrs.put(new Attributes.Name("Can-Retransform-Classes"), "true"); attrs.put(new Attributes.Name("Can-Redefine-Classes"), "true"); JarOutputStream jos = null; try { jos = new JarOutputStream(new FileOutputStream(jar), manifest); String cname = HotSwapAgent.class.getName(); JarEntry e = new JarEntry(cname.replace('.', '/') + ".class"); jos.putNextEntry(e); ClassPool pool = ClassPool.getDefault(); CtClass clazz = pool.get(cname); jos.write(clazz.toBytecode()); jos.closeEntry(); } finally { if (jos != null) jos.close(); } return jar; } }