Search in sources :

Example 56 with JarOutputStream

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

the class DalvikExecTest method test_execCreatedJarWithManifest.

/**
     * This test does quite the same as test_execCreatedJar, but includes a manifest.
     * Note however that the Dalvik JAR format does not require this manifest.
     * We just test whether the manifest is placed correctly within the JAR by
     * dumping its contents read as a simple text resource.
     * No! We can't do that so easily either, as there are other (parent) JARs
     * with a manifest inside, taken with precedence.
     * So we will reopen the JAR as a JarFile and check the manifest
     *  with a top level end-to-end approach.
     */
public void test_execCreatedJarWithManifest() throws IOException, InterruptedException {
    File jarFile = File.createTempFile("cts_dalvikExecTest_", ".jar");
    jarFile.deleteOnExit();
    // Create the manifest:
    Manifest manifest = new Manifest();
    Attributes attrs = manifest.getMainAttributes();
    attrs.put(Attributes.Name.MANIFEST_VERSION, "3.1415962");
    attrs.put(Attributes.Name.MAIN_CLASS, "dalvikExecTest.HelloWorld");
    attrs.put(Attributes.Name.CLASS_PATH, jarFile.getName());
    // Create a JAR output stream on the temp file using the manifest:
    JarOutputStream jarOut = new JarOutputStream(new FileOutputStream(jarFile), manifest);
    // Define the entry for the classes.dex:
    jarOut.putNextEntry(new JarEntry("classes.dex"));
    // Fill in the classes.dex contents, i.e. the Dalvik executable code:
    // (See below for the detailed source code contents.)
    Streams.copy(Support_Resources.getResourceStream("cts_dalvikExecTest_classes.dex"), jarOut);
    // Now add a resource file:
    //
    jarOut.putNextEntry(new JarEntry("dalvikExecTest/myResource"));
    jarOut.write("This Resource contains some text.".getBytes());
    // Close the stream to the completed JAR file.
    jarOut.close();
    // The resulting JAR file contains the classes listed at the end of this text,
    // like the 'cts_dalvikExecTest.jar' as part of the resources, too.
    String res;
    res = execDalvik(jarFile.getAbsolutePath(), "dalvikExecTest.HelloWorld");
    assertEquals("Hello Android World!", "Hello Android World!\n", res);
    res = execDalvik(jarFile.getAbsolutePath(), "dalvikExecTest.ResourceDumper");
    assertTrue("Android Resource Dumper started", res.contains("Android Resource Dumper started"));
    assertTrue("This Resource contains some text.", res.contains("This Resource contains some text."));
    // And now reread the manifest:
    //
    JarFile jarIn = new JarFile(jarFile);
    manifest = jarIn.getManifest();
    attrs = manifest.getMainAttributes();
    assertEquals("MANIFEST_VERSION must match!", "3.1415962", attrs.get(Attributes.Name.MANIFEST_VERSION));
    assertEquals("MAIN_CLASS must match!", "dalvikExecTest.HelloWorld", attrs.get(Attributes.Name.MAIN_CLASS));
    assertEquals("CLASS_PATH must match!", jarFile.getName(), attrs.get(Attributes.Name.CLASS_PATH));
}
Also used : FileOutputStream(java.io.FileOutputStream) Attributes(java.util.jar.Attributes) JarOutputStream(java.util.jar.JarOutputStream) Manifest(java.util.jar.Manifest) JarEntry(java.util.jar.JarEntry) JarFile(java.util.jar.JarFile) JarFile(java.util.jar.JarFile) File(java.io.File)

Example 57 with JarOutputStream

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

the class DalvikExecTest method test_execCreatedJar.

// Create a temp file, fill it with contents according to Dalvik JAR format, and execute it on dalvikvm using -classpath option.",
public void test_execCreatedJar() throws IOException, InterruptedException {
    File jarFile = File.createTempFile("cts_dalvikExecTest_", ".jar");
    jarFile.deleteOnExit();
    // Create a JAR output stream on the temp file:
    JarOutputStream jarOut = new JarOutputStream(new FileOutputStream(jarFile));
    // Define the entry for the classes.dex:
    jarOut.putNextEntry(new JarEntry("classes.dex"));
    // Fill in the classes.dex contents, i.e. the Dalvik executable code:
    // (See below for the detailed source code contents.)
    Streams.copy(Support_Resources.getResourceStream("cts_dalvikExecTest_classes.dex"), jarOut);
    // Now add a resource file:
    //
    jarOut.putNextEntry(new JarEntry("dalvikExecTest/myResource"));
    jarOut.write("This Resource contains some text.".getBytes());
    // Close the stream to the completed JAR file.
    jarOut.close();
    // The resulting JAR file contains the classes listed at the end of this text,
    // like the 'cts_dalvikExecTest.jar' as part of the resources, too.
    String res;
    res = execDalvik(jarFile.getAbsolutePath(), "dalvikExecTest.HelloWorld");
    assertEquals("Hello Android World!", "Hello Android World!\n", res);
    res = execDalvik(jarFile.getAbsolutePath(), "dalvikExecTest.ResourceDumper");
    assertTrue("Android Resource Dumper started", res.contains("Android Resource Dumper started"));
    assertTrue("This Resource contains some text.", res.contains("This Resource contains some text."));
}
Also used : FileOutputStream(java.io.FileOutputStream) JarOutputStream(java.util.jar.JarOutputStream) JarEntry(java.util.jar.JarEntry) JarFile(java.util.jar.JarFile) File(java.io.File)

Example 58 with JarOutputStream

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

the class DeploymentPackageBuilder method writeStream.

private void writeStream(List<ArtifactData> files, Manifest manifest, OutputStream outputStream) throws Exception {
    byte[] buffer = new byte[BUFFER_SIZE];
    try (JarOutputStream output = new JarOutputStream(outputStream)) {
        // Write out the manifest...
        if (isAddSignatures()) {
            m_signer.writeSignedManifest(manifest, output, m_signingKey, m_signingCert);
        } else {
            output.putNextEntry(new ZipEntry(JarFile.MANIFEST_NAME));
            manifest.write(output);
            output.closeEntry();
        }
        for (ArtifactData file : files) {
            if (file.isMissing()) {
                // No need to write the 'missing' files...
                continue;
            }
            output.putNextEntry(new JarEntry(file.getFilename()));
            try (InputStream is = file.createInputStream()) {
                int bytes;
                while ((bytes = is.read(buffer)) != -1) {
                    output.write(buffer, 0, bytes);
                }
            } finally {
                output.closeEntry();
            }
        }
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) JarInputStream(java.util.jar.JarInputStream) InputStream(java.io.InputStream) ZipEntry(java.util.zip.ZipEntry) JarOutputStream(java.util.jar.JarOutputStream) JarEntry(java.util.jar.JarEntry)

Example 59 with JarOutputStream

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

the class ContentCopyingJarInputStreamTest method createJar.

private void createJar(Manifest man, boolean includeIndex) throws IOException {
    FileOutputStream fos = new FileOutputStream(m_jarFile);
    JarOutputStream jos;
    if (man == null || includeIndex) {
        jos = new JarOutputStream(fos);
    } else {
        jos = new JarOutputStream(fos, man);
    }
    if (includeIndex) {
        // Write the INDEX.LIST file as first entry...
        jos.putNextEntry(new ZipEntry(INDEX_NAME));
        jos.write(("JarIndex-Version: 1.0\n\n" + m_jarFile.getName() + "\n").getBytes());
        jos.closeEntry();
        if (man != null) {
            jos.putNextEntry(new ZipEntry(MANIFEST_NAME));
            man.write(jos);
            jos.closeEntry();
        }
    }
    try {
        appendFiles(jos, 5);
    } finally {
        jos.close();
    }
}
Also used : FileOutputStream(java.io.FileOutputStream) ZipEntry(java.util.zip.ZipEntry) JarOutputStream(java.util.jar.JarOutputStream)

Example 60 with JarOutputStream

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

the class BootLoaderTest method createBundle.

private static File createBundle(String manifest) throws IOException {
    File f = File.createTempFile("felix-bundle", ".jar");
    f.deleteOnExit();
    Manifest mf = new Manifest(new ByteArrayInputStream(manifest.getBytes("utf-8")));
    mf.getMainAttributes().putValue("Manifest-Version", "1.0");
    JarOutputStream os = new JarOutputStream(new FileOutputStream(f), mf);
    os.close();
    return f;
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) FileOutputStream(java.io.FileOutputStream) JarOutputStream(java.util.jar.JarOutputStream) Manifest(java.util.jar.Manifest) File(java.io.File)

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