Search in sources :

Example 76 with JarOutputStream

use of java.util.jar.JarOutputStream in project tika by apache.

the class ForkClient method fillBootstrapJar.

/**
     * Fills in the jar file used to bootstrap the forked server process.
     * All the required <code>.class</code> files and a manifest with a
     * <code>Main-Class</code> entry are written into the archive.
     *
     * @param file file to hold the bootstrap archive
     * @throws IOException if the bootstrap archive could not be created
     */
private static void fillBootstrapJar(File file) throws IOException {
    try (JarOutputStream jar = new JarOutputStream(new FileOutputStream(file))) {
        String manifest = "Main-Class: " + ForkServer.class.getName() + "\n";
        jar.putNextEntry(new ZipEntry("META-INF/MANIFEST.MF"));
        jar.write(manifest.getBytes(UTF_8));
        Class<?>[] bootstrap = { ForkServer.class, ForkObjectInputStream.class, ForkProxy.class, ClassLoaderProxy.class, MemoryURLConnection.class, MemoryURLStreamHandler.class, MemoryURLStreamHandlerFactory.class, MemoryURLStreamRecord.class };
        ClassLoader loader = ForkServer.class.getClassLoader();
        for (Class<?> klass : bootstrap) {
            String path = klass.getName().replace('.', '/') + ".class";
            try (InputStream input = loader.getResourceAsStream(path)) {
                jar.putNextEntry(new JarEntry(path));
                IOUtils.copy(input, jar);
            }
        }
    }
}
Also used : DataInputStream(java.io.DataInputStream) InputStream(java.io.InputStream) FileOutputStream(java.io.FileOutputStream) ZipEntry(java.util.zip.ZipEntry) JarOutputStream(java.util.jar.JarOutputStream) JarEntry(java.util.jar.JarEntry)

Example 77 with JarOutputStream

use of java.util.jar.JarOutputStream in project sling by apache.

the class ModelArchiveWriter method write.

/**
     * Create a model archive.
     * The output stream will not be closed by this method. The caller
     * must call {@link JarOutputStream#close()} or {@link JarOutputStream#finish()}
     * on the return output stream. The caller can add additional files through
     * the return stream.
     *
     * In order to create an archive for a model, each feature in the model must
     * have a name and a version and the model must be valid, therefore {@link ModelUtility#validateIncludingVersion(Model)}
     * is called first. If the model is invalid an {@code IOException} is thrown.
     *
     * @param out The output stream to write to
     * @param model The model to write
     * @param baseManifest Optional base manifest used for creating the manifest.
     * @param provider The artifact provider
     * @return The jar output stream.
     * @throws IOException If anything goes wrong
     */
public static JarOutputStream write(final OutputStream out, final Model model, final Manifest baseManifest, final ArtifactProvider provider) throws IOException {
    // check model
    final Map<Traceable, String> errors = ModelUtility.validate(model);
    if (errors != null) {
        throw new IOException("Model is not valid: " + errors);
    }
    // create manifest
    final Manifest manifest = (baseManifest == null ? new Manifest() : new Manifest(baseManifest));
    manifest.getMainAttributes().putValue("Manifest-Version", "1.0");
    manifest.getMainAttributes().putValue(MANIFEST_HEADER, String.valueOf(ARCHIVE_VERSION));
    // create archive
    final JarOutputStream jos = new JarOutputStream(out, manifest);
    // write model first
    final JarEntry entry = new JarEntry(MODEL_NAME);
    jos.putNextEntry(entry);
    final Writer writer = new OutputStreamWriter(jos, "UTF-8");
    ModelWriter.write(writer, model);
    writer.flush();
    jos.closeEntry();
    final byte[] buffer = new byte[1024 * 1024 * 256];
    for (final Feature f : model.getFeatures()) {
        for (final RunMode rm : f.getRunModes()) {
            for (final ArtifactGroup g : rm.getArtifactGroups()) {
                for (final Artifact a : g) {
                    final JarEntry artifactEntry = new JarEntry(ARTIFACTS_PREFIX + a.getRepositoryPath());
                    jos.putNextEntry(artifactEntry);
                    try (final InputStream is = provider.getInputStream(a)) {
                        int l = 0;
                        while ((l = is.read(buffer)) > 0) {
                            jos.write(buffer, 0, l);
                        }
                    }
                    jos.closeEntry();
                }
            }
        }
    }
    return jos;
}
Also used : InputStream(java.io.InputStream) JarOutputStream(java.util.jar.JarOutputStream) IOException(java.io.IOException) Manifest(java.util.jar.Manifest) JarEntry(java.util.jar.JarEntry) Feature(org.apache.sling.provisioning.model.Feature) Artifact(org.apache.sling.provisioning.model.Artifact) RunMode(org.apache.sling.provisioning.model.RunMode) OutputStreamWriter(java.io.OutputStreamWriter) Traceable(org.apache.sling.provisioning.model.Traceable) ArtifactGroup(org.apache.sling.provisioning.model.ArtifactGroup) Writer(java.io.Writer) OutputStreamWriter(java.io.OutputStreamWriter)

Example 78 with JarOutputStream

use of java.util.jar.JarOutputStream in project sling by apache.

the class InstallServlet method createJar.

private static void createJar(final File sourceDir, final File jarFile, final Manifest mf) throws IOException {
    final JarOutputStream zos = new JarOutputStream(new FileOutputStream(jarFile));
    try {
        zos.setLevel(Deflater.NO_COMPRESSION);
        // manifest first
        final ZipEntry anEntry = new ZipEntry(JarFile.MANIFEST_NAME);
        zos.putNextEntry(anEntry);
        mf.write(zos);
        zos.closeEntry();
        zipDir(sourceDir, zos, "");
    } finally {
        try {
            zos.close();
        } catch (final IOException ignore) {
        // ignore
        }
    }
}
Also used : FileOutputStream(java.io.FileOutputStream) ZipEntry(java.util.zip.ZipEntry) JarOutputStream(java.util.jar.JarOutputStream) IOException(java.io.IOException)

Example 79 with JarOutputStream

use of java.util.jar.JarOutputStream in project jdk8u_jdk by JetBrains.

the class TestExceptions method unpack200Test2.

// test the Pack200.unpack(File, OutputStream);
static void unpack200Test2() {
    List<UnpackTestFileInput> tlist = new ArrayList<UnpackTestFileInput>();
    try {
        // setup the test scenarios
        try {
            tlist.add(new UnpackTestFileInput((File) null, null));
            tlist.add(new UnpackTestFileInput(testPackFile, null));
            tlist.add(new UnpackTestFileInput((File) null, new JarOutputStream(new ByteArrayOutputStream())));
        } catch (Exception e) {
            throw new Error("Initialization error", e);
        }
        // test the scenarios
        for (UnpackTestFileInput ti : tlist) {
            System.out.println(ti);
            try {
                Pack200.Unpacker unpacker = Pack200.newUnpacker();
                unpacker.unpack(ti.getInputFile(), ti.getJarOutputStream());
            } catch (Exception e) {
                ti.checkException(e);
            }
        }
    } finally {
        // keep jprt happy
        for (TestInput ti : tlist) {
            if (ti != null) {
                ti.close();
            }
        }
    }
}
Also used : Pack200(java.util.jar.Pack200) ArrayList(java.util.ArrayList) JarOutputStream(java.util.jar.JarOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) JarFile(java.util.jar.JarFile) File(java.io.File) IOException(java.io.IOException)

Example 80 with JarOutputStream

use of java.util.jar.JarOutputStream in project jdk8u_jdk by JetBrains.

the class TestExceptions method unpack200Test1.

// test the Pack200.unpack(InputStream, OutputStream);
static void unpack200Test1() {
    List<UnpackTestInput> tlist = new ArrayList<UnpackTestInput>();
    try {
        // setup the test scenarios
        try {
            tlist.add(new UnpackTestInput((InputStream) null, null));
            tlist.add(new UnpackTestInput(new FileInputStream(testPackFile), null));
            tlist.add(new UnpackTestInput((InputStream) null, new JarOutputStream(new ByteArrayOutputStream())));
        } catch (Exception e) {
            throw new Error("Initialization error", e);
        }
        // test the scenarios
        for (UnpackTestInput ti : tlist) {
            System.out.println(ti);
            try {
                Pack200.Unpacker unpacker = Pack200.newUnpacker();
                unpacker.unpack(ti.getInputStream(), ti.getJarOutputStream());
            } catch (Exception e) {
                ti.checkException(e);
            }
        }
    } finally {
        // keep jprt happy
        for (TestInput ti : tlist) {
            if (ti != null) {
                ti.close();
            }
        }
    }
}
Also used : Pack200(java.util.jar.Pack200) FileInputStream(java.io.FileInputStream) JarInputStream(java.util.jar.JarInputStream) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) JarOutputStream(java.util.jar.JarOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) FileInputStream(java.io.FileInputStream) IOException(java.io.IOException)

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