Search in sources :

Example 71 with JarInputStream

use of java.util.jar.JarInputStream in project J2ME-Loader by nikita36078.

the class AndroidProducer method processJar.

public static void processJar(File jarInputFile, File jarOutputFile, boolean isMidlet) throws IOException {
    JarInputStream jis = null;
    JarOutputStream jos = null;
    HashMap<String, byte[]> resources = new HashMap<>();
    try {
        jis = new JarInputStream(new FileInputStream(jarInputFile));
        Manifest manifest = jis.getManifest();
        if (manifest == null) {
            jos = new JarOutputStream(new FileOutputStream(jarOutputFile));
        } else {
            Attributes attributes = manifest.getMainAttributes();
            if (!attributes.containsKey(Attributes.Name.MANIFEST_VERSION)) {
                attributes.put(Attributes.Name.MANIFEST_VERSION, "1.0");
            }
            jos = new JarOutputStream(new FileOutputStream(jarOutputFile), manifest);
        }
        byte[] buffer = new byte[1024];
        JarEntry jarEntry;
        while ((jarEntry = jis.getNextJarEntry()) != null) {
            if (!jarEntry.isDirectory()) {
                String name = jarEntry.getName();
                int size = 0;
                int read;
                int length = buffer.length;
                while ((read = jis.read(buffer, size, length)) > 0) {
                    size += read;
                    length = 1024;
                    if (size + length > buffer.length) {
                        byte[] newInputBuffer = new byte[size + length];
                        System.arraycopy(buffer, 0, newInputBuffer, 0, buffer.length);
                        buffer = newInputBuffer;
                    }
                }
                byte[] inBuffer = new byte[size];
                System.arraycopy(buffer, 0, inBuffer, 0, size);
                resources.put(name, inBuffer);
                if (name.endsWith(".class")) {
                    analyze(name.substring(0, name.length() - ".class".length()), new ByteArrayInputStream(inBuffer));
                }
            }
        }
        for (String name : resources.keySet()) {
            byte[] inBuffer = resources.get(name);
            byte[] outBuffer = inBuffer;
            if (name.endsWith(".class")) {
                outBuffer = instrument(name, new ByteArrayInputStream(inBuffer), isMidlet);
            }
            jos.putNextEntry(new JarEntry(name));
            jos.write(outBuffer);
        }
    } finally {
        if (jis != null) {
            jis.close();
        }
        if (jos != null) {
            jos.close();
        }
    }
}
Also used : JarInputStream(java.util.jar.JarInputStream) HashMap(java.util.HashMap) Attributes(java.util.jar.Attributes) JarOutputStream(java.util.jar.JarOutputStream) Manifest(java.util.jar.Manifest) JarEntry(java.util.jar.JarEntry) FileInputStream(java.io.FileInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) FileOutputStream(java.io.FileOutputStream)

Example 72 with JarInputStream

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

the class GridUriDeploymentJarVerifier method verify0.

/**
 * Tests whether all files in given JAR file input stream are signed
 * with public key. If manifest is {@code null} than method returns
 * {@code true} if public key is null.
 *
 * @param in JAR file input stream.
 * @param pubKey Public key to be tested with.
 * @param allSigned Hint which means that all files in entry must be signed.
 * @param log Logger.
 * @return {@code true} if JAR file is signed with given public key.
 * @throws IOException Thrown if JAR file or its entries could not be read.
 */
private static boolean verify0(InputStream in, PublicKey pubKey, boolean allSigned, IgniteLogger log) throws IOException {
    assert in != null;
    JarInputStream jin = null;
    try {
        jin = new JarInputStream(in, true);
        Manifest manifest = jin.getManifest();
        // Manifest must be included in signed GAR file.
        if (manifest == null)
            return pubKey == null;
        Set<String> manifestFiles = getSignedFiles(manifest);
        JarEntry jarEntry;
        while ((jarEntry = jin.getNextJarEntry()) != null) {
            if (jarEntry.isDirectory())
                continue;
            // Verify by reading the file if altered.
            // Will return quietly if no problem.
            verifyDigestsImplicitly(jin);
            if (verifyEntry(jarEntry, manifest, pubKey, allSigned, true) == false)
                return false;
            manifestFiles.remove(jarEntry.getName());
        }
        return manifestFiles.size() <= 0;
    } catch (SecurityException e) {
        if (log.isDebugEnabled())
            log.debug("Got security error (ignoring): " + e.getMessage());
    } finally {
        U.close(jin, log);
    }
    return false;
}
Also used : JarInputStream(java.util.jar.JarInputStream) Manifest(java.util.jar.Manifest) JarEntry(java.util.jar.JarEntry)

Example 73 with JarInputStream

use of java.util.jar.JarInputStream in project Lucee by lucee.

the class Pack200Util method jar2pack.

public static void jar2pack(InputStream is, OutputStream os, boolean closeIS, boolean closeOS) throws IOException {
    // Create the Packer object
    Packer packer = Pack200.newPacker();
    // Initialize the state by setting the desired properties
    Map p = packer.properties();
    // take more time choosing codings for better compression
    // default is "5"
    p.put(Packer.EFFORT, "7");
    // use largest-possible archive segments (>10% better compression).
    p.put(Packer.SEGMENT_LIMIT, "-1");
    // reorder files for better compression.
    p.put(Packer.KEEP_FILE_ORDER, Packer.FALSE);
    // smear modification times to a single value.
    p.put(Packer.MODIFICATION_TIME, Packer.LATEST);
    // ignore all JAR deflation requests,
    // transmitting a single request to use "store" mode.
    p.put(Packer.DEFLATE_HINT, Packer.FALSE);
    // discard debug attributes
    p.put(Packer.CODE_ATTRIBUTE_PFX + "LineNumberTable", Packer.STRIP);
    // throw an error if an attribute is unrecognized
    p.put(Packer.UNKNOWN_ATTRIBUTE, Packer.ERROR);
    JarInputStream jis = null;
    os = new GZIPOutputStream(os);
    PrintStream err = System.err;
    try {
        System.setErr(new PrintStream(new DevNullOutputStream()));
        jis = new JarInputStream(is);
        packer.pack(jis, os);
    } finally {
        System.setErr(err);
        if (closeIS)
            Util.closeEL(jis);
        if (closeOS)
            Util.closeEL(os);
    }
}
Also used : PrintStream(java.io.PrintStream) JarInputStream(java.util.jar.JarInputStream) GZIPOutputStream(java.util.zip.GZIPOutputStream) Map(java.util.Map) SortedMap(java.util.SortedMap) Packer(java.util.jar.Pack200.Packer)

Example 74 with JarInputStream

use of java.util.jar.JarInputStream in project atlasmap by atlasmap.

the class DynamicClassLoaderTest method testListClassesInJarFile.

@Test
public void testListClassesInJarFile() {
    List<String> classes = new ArrayList<>();
    String folderName = "target/reference-jars";
    try (DirectoryStream<Path> stream = Files.newDirectoryStream(Paths.get(folderName))) {
        for (Path entry : stream) {
            if (!entry.toFile().isFile()) {
                continue;
            }
            try (JarInputStream jarFile = new JarInputStream(new FileInputStream(entry.toFile()))) {
                JarEntry jarEntry;
                while (true) {
                    jarEntry = jarFile.getNextJarEntry();
                    if (jarEntry == null) {
                        break;
                    }
                    if (jarEntry.getName().endsWith(".class")) {
                        String className = jarEntry.getName().replaceAll("/", "\\.");
                        classes.add(className);
                        System.out.println("ClassName: " + className);
                    } else {
                        System.out.println("Not a class: " + jarEntry.getName());
                    }
                }
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : Path(java.nio.file.Path) JarInputStream(java.util.jar.JarInputStream) ArrayList(java.util.ArrayList) JarEntry(java.util.jar.JarEntry) FileInputStream(java.io.FileInputStream) Test(org.junit.Test)

Example 75 with JarInputStream

use of java.util.jar.JarInputStream in project bnd by bndtools.

the class Domain method domain.

public static Domain domain(File file) throws IOException {
    if (file.getName().endsWith(".mf")) {
        try (InputStream in = IO.stream(file)) {
            Manifest m = new Manifest(in);
            return domain(m);
        }
    }
    if (file.getName().endsWith(".properties") || file.getName().endsWith(".bnd")) {
        Processor p = new Processor();
        p.setProperties(file);
        return domain(p);
    }
    if (file.getName().endsWith(".pom")) {
        try {
            PomParser p = new PomParser();
            p.setProperties(p.getProperties(file));
            return domain(p);
        } catch (Exception e) {
            throw new IllegalStateException(e);
        }
    }
    // default & last. Assume JAR
    try (JarInputStream jin = new JarInputStream(IO.stream(file))) {
        Manifest m = jin.getManifest();
        if (m != null)
            return domain(m);
    }
    try (ZipFile zf = new ZipFile(file)) {
        ZipEntry entry = zf.getEntry("META-INF/MANIFEST.MF");
        if (entry == null)
            return null;
        Manifest m = new Manifest(zf.getInputStream(entry));
        return domain(m);
    } catch (ZipException e) {
        return null;
    }
}
Also used : PomParser(aQute.bnd.maven.PomParser) ZipFile(java.util.zip.ZipFile) JarInputStream(java.util.jar.JarInputStream) JarInputStream(java.util.jar.JarInputStream) InputStream(java.io.InputStream) ZipEntry(java.util.zip.ZipEntry) ZipException(java.util.zip.ZipException) Manifest(java.util.jar.Manifest) ZipException(java.util.zip.ZipException) IOException(java.io.IOException)

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