Refine search
@Override public void modifyOutputStream(JarOutputStream jarOut) throws IOException { for (String key : this.entries.keySet()) { jarOut.putNextEntry(new JarEntry(key)); jarOut.write(this.entries.get(key).getBytes()); } } }
/** * {@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(); } }
@Override public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { String name = resourceRoot.relativize(file).toString(); jar.putNextEntry(new ZipEntry(name)); try (InputStream input = Files.newInputStream(file, StandardOpenOption.READ)) { int len; while ((len = input.read(data)) > 0) { jar.write(data, 0, len); } } return FileVisitResult.CONTINUE; } });
/** * {@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; }
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(); } } } } } }
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 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); } }
/** * {@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; }
byte[] replacement = files.remove(jarEntry.getName()); if (replacement == null) { outputStream.putNextEntry(jarEntry); byte[] buffer = new byte[BUFFER_SIZE]; int index; while ((index = inputStream.read(buffer)) != END_OF_FILE) { outputStream.write(buffer, FROM_BEGINNING, index); outputStream.putNextEntry(new JarEntry(jarEntry.getName())); outputStream.write(replacement); outputStream.putNextEntry(new JarEntry(entry.getKey())); outputStream.write(entry.getValue()); outputStream.closeEntry();
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(); } } }
out.putNextEntry(outEntry); out.write(buffer, 0, num);
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); if (mVerbose) { System.out.println("wrote " + mByteCount + " bytes");
jarEntry.setTime(file.lastModified()); jarOutputStream.putNextEntry(jarEntry); jarOutputStream.write(buffer, 0, count); jarEntry.setTime(file.lastModified()); jarOutputStream.putNextEntry(jarEntry); jarOutputStream.closeEntry();
jos.putNextEntry(e); FileInputStream fis = new FileInputStream(f); byte[] buffer = new byte[64 * 1024]; int length; while ((length = fis.read(buffer)) != -1) { jos.write(buffer, 0, length);
jStream.putNextEntry(zipEntry); int count = 0; do { jStream.write(byteBuffer, 0, count); count = iStream.read(byteBuffer, 0, byteBuffer.length); } while (count != -1);
jarOut.putNextEntry(new JarEntry(name)); } else if (name.endsWith(".class")) { String className = name.substring(0, name.length() - ".class".length()).replace('/', '.'); classInstrumentor.instrument( classBytes, instrumentationConfiguration, classNodeProvider); jarOut.putNextEntry(new JarEntry(name)); jarOut.write(outBytes); classCount++; } catch (Exception e) { jarOut.putNextEntry(new JarEntry(name)); Util.copy(jarFile.getInputStream(jarEntry), jarOut); nonClassCount++;
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(); }