Search in sources :

Example 46 with JarInputStream

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

the class BundleFileProcessorTest method compareJarContents.

private static void compareJarContents(File orgJar, File actualJar) throws IOException {
    JarInputStream jis1 = null;
    JarInputStream jis2 = null;
    try {
        jis1 = new JarInputStream(new FileInputStream(orgJar));
        jis2 = new JarInputStream(new FileInputStream(actualJar));
        JarEntry je1 = null;
        while ((je1 = jis1.getNextJarEntry()) != null) {
            if (je1.isDirectory())
                continue;
            JarEntry je2 = null;
            while ((je2 = jis2.getNextJarEntry()) != null) {
                if (!je2.isDirectory())
                    break;
            }
            assertEquals(je1.getName(), je2.getName());
            assertEquals(je1.getSize(), je2.getSize());
            try {
                byte[] buf1 = streamToByteArray(jis1);
                byte[] buf2 = streamToByteArray(jis2);
                assertArrayEquals("Contents not equal: " + je1.getName(), buf1, buf2);
            } finally {
                jis1.closeEntry();
                jis2.closeEntry();
            }
        }
    } finally {
        closeQuietly(jis1);
        closeQuietly(jis2);
    }
}
Also used : JarInputStream(java.util.jar.JarInputStream) JarEntry(java.util.jar.JarEntry) FileInputStream(java.io.FileInputStream)

Example 47 with JarInputStream

use of java.util.jar.JarInputStream in project cloudstack by apache.

the class OnwireClassRegistry method getFromJARFile.

static Set<Class<?>> getFromJARFile(String jar, String packageName) throws IOException, ClassNotFoundException {
    Set<Class<?>> classes = new HashSet<Class<?>>();
    try (JarInputStream jarFile = new JarInputStream(new FileInputStream(jar))) {
        JarEntry jarEntry;
        do {
            jarEntry = jarFile.getNextJarEntry();
            if (jarEntry != null) {
                String className = jarEntry.getName();
                if (className.endsWith(".class")) {
                    className = stripFilenameExtension(className);
                    if (className.startsWith(packageName)) {
                        try {
                            Class<?> clz = Class.forName(className.replace('/', '.'));
                            classes.add(clz);
                        } catch (ClassNotFoundException | NoClassDefFoundError e) {
                            s_logger.warn("Unable to load class from jar file", e);
                        }
                    }
                }
            }
        } while (jarEntry != null);
        return classes;
    }
}
Also used : JarInputStream(java.util.jar.JarInputStream) JarEntry(java.util.jar.JarEntry) FileInputStream(java.io.FileInputStream) HashSet(java.util.HashSet)

Example 48 with JarInputStream

use of java.util.jar.JarInputStream in project aries by apache.

the class Helper method getBundleDescriptor.

private static BundleDescriptor getBundleDescriptor(String path, TinyBundle bundle) throws Exception {
    File file = new File(path);
    FileOutputStream fos = new FileOutputStream(file, true);
    try {
        copy(bundle.build(), fos);
    } finally {
        close(fos);
    }
    FileInputStream fis = null;
    JarInputStream jis = null;
    try {
        fis = new FileInputStream(file);
        jis = new JarInputStream(fis);
        Map<String, String> headers = new HashMap<String, String>();
        for (Map.Entry<Object, Object> entry : jis.getManifest().getMainAttributes().entrySet()) {
            headers.put(entry.getKey().toString(), entry.getValue().toString());
        }
        return new BundleDescriptor(bundle.getClass().getClassLoader(), new URL("jar:" + file.toURI().toString() + "!/"), headers);
    } finally {
        close(fis, jis);
    }
}
Also used : BundleDescriptor(de.kalpatec.pojosr.framework.launch.BundleDescriptor) JarInputStream(java.util.jar.JarInputStream) HashMap(java.util.HashMap) FileOutputStream(java.io.FileOutputStream) File(java.io.File) HashMap(java.util.HashMap) Map(java.util.Map) FileInputStream(java.io.FileInputStream) URL(java.net.URL)

Example 49 with JarInputStream

use of java.util.jar.JarInputStream in project aries by apache.

the class BundleManifestTest method testZipWithName.

@Test
public void testZipWithName() throws Exception {
    // make sure that the manifest is not the first file in the jar archive
    JarInputStream jarIs = new JarInputStream(new FileInputStream(BUNDLE_WITH_NAME_HEADER));
    assertNull(jarIs.getManifest());
    jarIs.close();
    BundleManifest sut = BundleManifest.fromBundle(BUNDLE_WITH_NAME_HEADER);
    assertEquals(EXPECTED_SYMBOLIC_NAME, sut.getSymbolicName());
    assertEquals(EXPECTED_VERSION, sut.getVersion().toString());
}
Also used : JarInputStream(java.util.jar.JarInputStream) FileInputStream(java.io.FileInputStream) Test(org.junit.Test)

Example 50 with JarInputStream

use of java.util.jar.JarInputStream in project aries by apache.

the class BundleManifestTest method testZipFromIDirectory.

@Test
public void testZipFromIDirectory() throws Exception {
    // make sure that the manifest is not the first file in the jar archive
    JarInputStream jarIs = new JarInputStream(new FileInputStream(BUNDLE_WITHOUT_NAME_HEADER));
    assertNull(jarIs.getManifest());
    jarIs.close();
    BundleManifest sut = BundleManifest.fromBundle(FileSystem.getFSRoot(BUNDLE_WITHOUT_NAME_HEADER));
    assertEquals(EXPECTED_SYMBOLIC_NAME, sut.getSymbolicName());
    assertEquals(EXPECTED_VERSION, sut.getVersion().toString());
}
Also used : JarInputStream(java.util.jar.JarInputStream) FileInputStream(java.io.FileInputStream) Test(org.junit.Test)

Aggregations

JarInputStream (java.util.jar.JarInputStream)185 JarEntry (java.util.jar.JarEntry)82 IOException (java.io.IOException)73 FileInputStream (java.io.FileInputStream)66 Manifest (java.util.jar.Manifest)56 File (java.io.File)48 InputStream (java.io.InputStream)45 ZipEntry (java.util.zip.ZipEntry)34 JarOutputStream (java.util.jar.JarOutputStream)29 Test (org.junit.Test)29 FileOutputStream (java.io.FileOutputStream)26 ByteArrayInputStream (java.io.ByteArrayInputStream)24 URL (java.net.URL)20 ArrayList (java.util.ArrayList)15 ByteArrayOutputStream (java.io.ByteArrayOutputStream)14 OutputStream (java.io.OutputStream)14 JarFile (java.util.jar.JarFile)14 BufferedInputStream (java.io.BufferedInputStream)11 Attributes (java.util.jar.Attributes)11 HashSet (java.util.HashSet)9