Search in sources :

Example 1 with JarOutputStream

use of java.util.jar.JarOutputStream in project elasticsearch by elastic.

the class JarHellTests method makeJar.

URL makeJar(Path dir, String name, Manifest manifest, String... files) throws IOException {
    Path jarpath = dir.resolve(name);
    ZipOutputStream out;
    if (manifest == null) {
        out = new JarOutputStream(Files.newOutputStream(jarpath, StandardOpenOption.CREATE));
    } else {
        out = new JarOutputStream(Files.newOutputStream(jarpath, StandardOpenOption.CREATE), manifest);
    }
    for (String file : files) {
        out.putNextEntry(new ZipEntry(file));
    }
    out.close();
    return jarpath.toUri().toURL();
}
Also used : Path(java.nio.file.Path) ZipOutputStream(java.util.zip.ZipOutputStream) ZipEntry(java.util.zip.ZipEntry) JarOutputStream(java.util.jar.JarOutputStream)

Example 2 with JarOutputStream

use of java.util.jar.JarOutputStream in project buck by facebook.

the class ClassNodeListSupplierTest method testOneJar.

@Test
public void testOneJar() throws IOException {
    File jar = new File(tmpDir.getRoot(), "primary.jar");
    ZipOutputStream jarOut = new JarOutputStream(new FileOutputStream(jar));
    jarOut.putNextEntry(new JarEntry("com/facebook/buck/android/ClassNodeListSupplierTest.class"));
    writeClassBytes(ClassNodeListSupplierTest.class, jarOut);
    jarOut.close();
    Supplier<ImmutableList<ClassNode>> supplier = ClassNodeListSupplier.createMemoized(ImmutableList.of(jar.toPath()));
    ImmutableList<ClassNode> classNodes = supplier.get();
    assertEquals(1, classNodes.size());
    assertEquals(Type.getType(ClassNodeListSupplierTest.class).getInternalName(), classNodes.get(0).name);
    // Memoized should always return the same object
    assertSame(classNodes, supplier.get());
}
Also used : ClassNode(org.objectweb.asm.tree.ClassNode) ZipOutputStream(java.util.zip.ZipOutputStream) ImmutableList(com.google.common.collect.ImmutableList) FileOutputStream(java.io.FileOutputStream) JarOutputStream(java.util.jar.JarOutputStream) JarEntry(java.util.jar.JarEntry) File(java.io.File) Test(org.junit.Test)

Example 3 with JarOutputStream

use of java.util.jar.JarOutputStream in project jetty.project by eclipse.

the class WarURLConnection method substitueManifest.

/**
     * Use PipedOuputStream and PipedInputStream to do the transformation without making
     * a new temporary file ust to replace the manifest.
     * @param newmanifest The new manifest
     * @param rawIn The file input stream or equivalent. not the jar input stream.
     */
public static InputStream substitueManifest(final Manifest newmanifest, final InputStream rawIn) throws IOException {
    final PipedOutputStream pOut = new PipedOutputStream();
    PipedInputStream pIn = new PipedInputStream(pOut);
    Runnable run = new Runnable() {

        public void run() {
            JarInputStream jin = null;
            JarOutputStream dest = null;
            try {
                jin = new JarInputStream(rawIn, false);
                dest = new JarOutputStream(pOut, newmanifest);
                ZipEntry next = jin.getNextEntry();
                while (next != null) {
                    if (next.getName().equalsIgnoreCase(JarFile.MANIFEST_NAME)) {
                        continue;
                    }
                    dest.putNextEntry(next);
                    if (next.getSize() > 0) {
                        IO.copy(jin, dest, next.getSize());
                    }
                    next = jin.getNextJarEntry();
                }
            } catch (IOException ioe) {
                ioe.printStackTrace();
            } finally {
                if (dest != null)
                    IO.close(dest);
                if (jin != null)
                    IO.close(jin);
                IO.close(pOut);
            }
        }
    };
    Thread th = new Thread(run);
    th.start();
    return pIn;
}
Also used : JarInputStream(java.util.jar.JarInputStream) ZipEntry(java.util.zip.ZipEntry) JarOutputStream(java.util.jar.JarOutputStream) PipedOutputStream(java.io.PipedOutputStream) PipedInputStream(java.io.PipedInputStream) IOException(java.io.IOException)

Example 4 with JarOutputStream

use of java.util.jar.JarOutputStream in project weave by continuuity.

the class YarnWeavePreparer method saveLauncher.

/**
   * Creates the launcher.jar for launch the main application.
   */
private void saveLauncher(Map<String, LocalFile> localFiles) throws URISyntaxException, IOException {
    LOG.debug("Create and copy {}", Constants.Files.LAUNCHER_JAR);
    Location location = createTempLocation(Constants.Files.LAUNCHER_JAR);
    final String launcherName = WeaveLauncher.class.getName();
    // Create a jar file with the WeaveLauncher optionally a json serialized classpath.json in it.
    final JarOutputStream jarOut = new JarOutputStream(location.getOutputStream());
    ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
    if (classLoader == null) {
        classLoader = getClass().getClassLoader();
    }
    Dependencies.findClassDependencies(classLoader, new Dependencies.ClassAcceptor() {

        @Override
        public boolean accept(String className, URL classUrl, URL classPathUrl) {
            Preconditions.checkArgument(className.startsWith(launcherName), "Launcher jar should not have dependencies: %s", className);
            try {
                jarOut.putNextEntry(new JarEntry(className.replace('.', '/') + ".class"));
                InputStream is = classUrl.openStream();
                try {
                    ByteStreams.copy(is, jarOut);
                } finally {
                    is.close();
                }
            } catch (IOException e) {
                throw Throwables.propagate(e);
            }
            return true;
        }
    }, WeaveLauncher.class.getName());
    try {
        if (!classPaths.isEmpty()) {
            jarOut.putNextEntry(new JarEntry("classpath"));
            jarOut.write(Joiner.on(':').join(classPaths).getBytes(Charsets.UTF_8));
        }
    } finally {
        jarOut.close();
    }
    LOG.debug("Done {}", Constants.Files.LAUNCHER_JAR);
    localFiles.put(Constants.Files.LAUNCHER_JAR, createLocalFile(Constants.Files.LAUNCHER_JAR, location));
}
Also used : InputStream(java.io.InputStream) JarOutputStream(java.util.jar.JarOutputStream) WeaveLauncher(com.continuuity.weave.launcher.WeaveLauncher) Dependencies(com.continuuity.weave.internal.utils.Dependencies) IOException(java.io.IOException) JarEntry(java.util.jar.JarEntry) URL(java.net.URL) Location(com.continuuity.weave.filesystem.Location)

Example 5 with JarOutputStream

use of java.util.jar.JarOutputStream in project jersey by jersey.

the class JarUtils method createJarFile.

public static File createJarFile(final String name, final Suffix s, final String base, final Map<String, String> entries) throws IOException {
    final File tempJar = File.createTempFile(name, "." + s);
    tempJar.deleteOnExit();
    final JarOutputStream jos = new JarOutputStream(new BufferedOutputStream(new FileOutputStream(tempJar)), new Manifest());
    final Set<String> usedSegments = new HashSet<String>();
    for (final Map.Entry<String, String> entry : entries.entrySet()) {
        for (final String path : getPaths(entry.getValue())) {
            if (usedSegments.contains(path)) {
                continue;
            }
            usedSegments.add(path);
            final JarEntry e = new JarEntry(path);
            jos.putNextEntry(e);
            jos.closeEntry();
        }
        final JarEntry e = new JarEntry(entry.getValue());
        jos.putNextEntry(e);
        final InputStream f = new BufferedInputStream(new FileInputStream(base + entry.getKey()));
        final byte[] buf = new byte[1024];
        int read = 1024;
        while ((read = f.read(buf, 0, read)) != -1) {
            jos.write(buf, 0, read);
        }
        jos.closeEntry();
    }
    jos.close();
    return tempJar;
}
Also used : BufferedInputStream(java.io.BufferedInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) JarOutputStream(java.util.jar.JarOutputStream) Manifest(java.util.jar.Manifest) JarEntry(java.util.jar.JarEntry) FileInputStream(java.io.FileInputStream) BufferedInputStream(java.io.BufferedInputStream) FileOutputStream(java.io.FileOutputStream) File(java.io.File) BufferedOutputStream(java.io.BufferedOutputStream) HashMap(java.util.HashMap) Map(java.util.Map) HashSet(java.util.HashSet)

Aggregations

JarOutputStream (java.util.jar.JarOutputStream)485 FileOutputStream (java.io.FileOutputStream)308 File (java.io.File)265 JarEntry (java.util.jar.JarEntry)194 Manifest (java.util.jar.Manifest)140 IOException (java.io.IOException)130 ZipEntry (java.util.zip.ZipEntry)116 InputStream (java.io.InputStream)89 FileInputStream (java.io.FileInputStream)84 JarFile (java.util.jar.JarFile)82 ByteArrayOutputStream (java.io.ByteArrayOutputStream)76 ByteArrayInputStream (java.io.ByteArrayInputStream)55 Test (org.junit.Test)55 BufferedOutputStream (java.io.BufferedOutputStream)47 Path (java.nio.file.Path)42 JarInputStream (java.util.jar.JarInputStream)41 OutputStream (java.io.OutputStream)36 Attributes (java.util.jar.Attributes)36 ArrayList (java.util.ArrayList)35 Map (java.util.Map)27