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()); } } }
private static Optional<Path> getOverrideClasspath(@Nullable List<Class<?>> overrideClasspath) { if (overrideClasspath == null) { return Optional.empty(); } try { Path tempJarFile = Files.createTempFile(/* prefix = */ null, /* suffix = */ ".jar"); try (OutputStream os = Files.newOutputStream(tempJarFile); JarOutputStream jos = new JarOutputStream(os)) { for (Class<?> clazz : overrideClasspath) { String entryPath = clazz.getName().replace('.', '/') + ".class"; jos.putNextEntry(new JarEntry(entryPath)); try (InputStream is = clazz.getClassLoader().getResourceAsStream(entryPath)) { ByteStreams.copy(is, jos); } } } return Optional.of(tempJarFile); } catch (IOException e) { throw new UncheckedIOException(e); } }
/** * {@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; }
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(); } }
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 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(); } }
/** * {@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); } }
public void testJar(JarOutputStream jos) throws Exception { JarEntry je = new JarEntry("foo"); jos.putNextEntry(je); jos.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(); }
/** * {@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(); }
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(); } } }
if (inEntry.getMethod() == JarEntry.STORED) { outEntry = new JarEntry(inEntry); } else { outEntry = new JarEntry(name); out.putNextEntry(outEntry);
? new JarOutputStream(new FileOutputStream(targetJar)) : new JarOutputStream(new FileOutputStream(targetJar), manifest); try { Map<TypeDescription, byte[]> rawAuxiliaryTypes = getAuxiliaryTypes(); 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.putNextEntry(new JarEntry(entry.getKey())); outputStream.write(entry.getValue()); outputStream.closeEntry();
String subPath = (path == null) ? "" : (path + dirOrFile2jar.getName() + SEP); if (path != null) { 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);
Set<String> failedClasses = new TreeSet<>(); try (JarOutputStream jarOut = new JarOutputStream( new BufferedOutputStream(new FileOutputStream(destFile), 32 * 1024))) { System.out.println("Instrumenting from " + sourceFile + " to " + destFile); 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++; jarOut.putNextEntry(new JarEntry(name)); Util.copy(jarFile.getInputStream(jarEntry), jarOut); nonClassCount++;
JarEntry jarEntry = new JarEntry(name); jarEntry.setTime(file.lastModified()); jarOutputStream.putNextEntry(jarEntry); JarEntry jarEntry = new JarEntry(name); jarEntry.setTime(file.lastModified()); jarOutputStream.putNextEntry(jarEntry); jarOutputStream.closeEntry();
JarOutputStream target = new JarOutputStream(new FileOutputStream("output.jar"), manifest); add(new File("inputDirectory"), target); target.close(); JarEntry entry = new JarEntry(name); entry.setTime(source.lastModified()); target.putNextEntry(entry); target.closeEntry(); JarEntry entry = new JarEntry(source.getPath().replace("\\", "/")); entry.setTime(source.lastModified()); target.putNextEntry(entry); in = new BufferedInputStream(new FileInputStream(source));
public void modifyOutputStream( JarOutputStream jos ) throws IOException { jos.putNextEntry( new JarEntry( resource ) ); new XMLOutputter( Format.getPrettyFormat() ).output( doc, jos ); doc = null; } }
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; } }
JarEntry entry = new JarEntry(fileName.replace('\\', '/')); jarFile.putNextEntry(entry); streamOut = jarFile; } else {