Search in sources :

Example 86 with JarOutputStream

use of java.util.jar.JarOutputStream in project bytecode-viewer by Konloch.

the class JarArchive method write.

public void write(File target) {
    try (JarOutputStream output = (manifest != null ? new JarOutputStream(new FileOutputStream(target), manifest) : new JarOutputStream(new FileOutputStream(target)))) {
        for (Map.Entry<String, ClassNode> entry : build().entrySet()) {
            output.putNextEntry(new JarEntry(entry.getKey().replaceAll("\\.", "/") + ".class"));
            ClassWriter writer = new ClassWriter(0);
            entry.getValue().accept(writer);
            output.write(writer.toByteArray());
            output.closeEntry();
        }
        output.flush();
    } catch (IOException e) {
        e.printStackTrace();
    }
}
Also used : ClassNode(org.objectweb.asm.tree.ClassNode) FileOutputStream(java.io.FileOutputStream) JarOutputStream(java.util.jar.JarOutputStream) IOException(java.io.IOException) JarEntry(java.util.jar.JarEntry) HashMap(java.util.HashMap) Map(java.util.Map) ClassWriter(org.objectweb.asm.ClassWriter)

Example 87 with JarOutputStream

use of java.util.jar.JarOutputStream 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 88 with JarOutputStream

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

the class Pack200Util method pack2Jar.

public static void pack2Jar(InputStream is, OutputStream os, boolean closeIS, boolean closeOS) throws IOException {
    Unpacker unpacker = Pack200.newUnpacker();
    SortedMap<String, String> p = unpacker.properties();
    p.put(Unpacker.DEFLATE_HINT, Unpacker.TRUE);
    is = new GZIPInputStream(is);
    JarOutputStream jos = null;
    try {
        jos = new JarOutputStream(os);
        unpacker.unpack(is, jos);
        jos.finish();
    } finally {
        if (closeIS)
            IOUtil.closeEL(is);
        if (closeOS)
            IOUtil.closeEL(jos);
    }
}
Also used : GZIPInputStream(java.util.zip.GZIPInputStream) JarOutputStream(java.util.jar.JarOutputStream) Unpacker(java.util.jar.Pack200.Unpacker)

Example 89 with JarOutputStream

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

the class Pack200Util method pack2Jar.

public static void pack2Jar(InputStream is, OutputStream os, boolean closeIS, boolean closeOS) throws IOException {
    Unpacker unpacker = Pack200.newUnpacker();
    SortedMap<String, String> p = unpacker.properties();
    p.put(Unpacker.DEFLATE_HINT, Unpacker.TRUE);
    is = new GZIPInputStream(is);
    JarOutputStream jos = null;
    try {
        jos = new JarOutputStream(os);
        unpacker.unpack(is, jos);
        jos.finish();
    } finally {
        if (closeIS)
            Util.closeEL(is);
        if (closeOS)
            Util.closeEL(jos);
    }
}
Also used : GZIPInputStream(java.util.zip.GZIPInputStream) JarOutputStream(java.util.jar.JarOutputStream) Unpacker(java.util.jar.Pack200.Unpacker)

Example 90 with JarOutputStream

use of java.util.jar.JarOutputStream in project knime-core by knime.

the class StringManipulatorProvider method getJarFile.

/**
 * Give jar file with all *.class files returned by
 * getManipulators(ALL_CATEGORY).
 *
 * @return file object of a jar file with all compiled manipulators
 * @throws IOException if jar file cannot be created
 */
public synchronized File getJarFile() throws IOException {
    if (m_jarFile == null || !m_jarFile.exists()) {
        // Temp file must not be associated with a node therefore we need to specify the base directory
        // explicitly.
        m_jarFile = FileUtil.createTempFile("jsnippet-manipulators", ".jar", new File(KNIMEConstants.getKNIMETempDir()), true);
        Collection<Object> classes = new ArrayList<Object>();
        classes.add(Manipulator.class);
        classes.addAll(m_manipulators.get(ALL_CATEGORY));
        // create tree structure for classes
        DefaultMutableTreeNode root = createTree(classes);
        try (JarOutputStream jar = new JarOutputStream(new FileOutputStream(m_jarFile))) {
            createJar(root, jar, null);
        }
    }
    return m_jarFile;
}
Also used : DefaultMutableTreeNode(javax.swing.tree.DefaultMutableTreeNode) FileOutputStream(java.io.FileOutputStream) ArrayList(java.util.ArrayList) JarOutputStream(java.util.jar.JarOutputStream) 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