Search in sources :

Example 11 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 12 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 13 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 14 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 15 with JarOutputStream

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

the class AccumulateClassNamesStepTest method testExecuteAccumulateClassNamesStepOnJarFile.

@Test
public void testExecuteAccumulateClassNamesStepOnJarFile() throws IOException {
    // Create a JAR file.
    String name = "example.jar";
    File jarFile = tmp.newFile(name);
    try (JarOutputStream out = new JarOutputStream(new BufferedOutputStream(new FileOutputStream(jarFile)))) {
        out.putNextEntry(new ZipEntry("com/example/Foo.class"));
        out.closeEntry();
        out.putNextEntry(new ZipEntry("com/example/Bar.class"));
        out.closeEntry();
        out.putNextEntry(new ZipEntry("com/example/not_a_class.png"));
        out.closeEntry();
        out.putNextEntry(new ZipEntry("com/example/subpackage/Baz.class"));
        out.closeEntry();
    }
    // Create the AccumulateClassNamesStep and execute it.
    ProjectFilesystem filesystem = new ProjectFilesystem(tmp.getRoot().toPath());
    AccumulateClassNamesStep accumulateClassNamesStep = new AccumulateClassNamesStep(filesystem, Optional.of(Paths.get(name)), Paths.get("output.txt"));
    ExecutionContext context = TestExecutionContext.newInstance();
    accumulateClassNamesStep.execute(context);
    String contents = Files.toString(new File(tmp.getRoot(), "output.txt"), Charsets.UTF_8);
    String separator = AccumulateClassNamesStep.CLASS_NAME_HASH_CODE_SEPARATOR;
    assertEquals("Verify that the contents are sorted alphabetically and ignore non-.class files.", Joiner.on('\n').join("com/example/Bar" + separator + SHA1_FOR_EMPTY_STRING, "com/example/Foo" + separator + SHA1_FOR_EMPTY_STRING, "com/example/subpackage/Baz" + separator + SHA1_FOR_EMPTY_STRING) + '\n', contents);
}
Also used : ExecutionContext(com.facebook.buck.step.ExecutionContext) TestExecutionContext(com.facebook.buck.step.TestExecutionContext) FileOutputStream(java.io.FileOutputStream) ZipEntry(java.util.zip.ZipEntry) JarOutputStream(java.util.jar.JarOutputStream) ProjectFilesystem(com.facebook.buck.io.ProjectFilesystem) File(java.io.File) BufferedOutputStream(java.io.BufferedOutputStream) Test(org.junit.Test)

Aggregations

JarOutputStream (java.util.jar.JarOutputStream)244 FileOutputStream (java.io.FileOutputStream)151 File (java.io.File)126 JarEntry (java.util.jar.JarEntry)99 Manifest (java.util.jar.Manifest)72 ZipEntry (java.util.zip.ZipEntry)56 IOException (java.io.IOException)53 JarFile (java.util.jar.JarFile)46 Test (org.junit.Test)43 FileInputStream (java.io.FileInputStream)35 InputStream (java.io.InputStream)34 ByteArrayOutputStream (java.io.ByteArrayOutputStream)33 ByteArrayInputStream (java.io.ByteArrayInputStream)27 JarInputStream (java.util.jar.JarInputStream)26 BufferedOutputStream (java.io.BufferedOutputStream)22 OutputStream (java.io.OutputStream)21 Path (java.nio.file.Path)21 ArrayList (java.util.ArrayList)20 URL (java.net.URL)13 HashMap (java.util.HashMap)13