Example usage for java.util.jar JarOutputStream flush

List of usage examples for java.util.jar JarOutputStream flush

Introduction

In this page you can find the example usage for java.util.jar JarOutputStream flush.

Prototype

public void flush() throws IOException 

Source Link

Document

Flushes the compressed output stream.

Usage

From source file:org.eclipse.gemini.blueprint.test.internal.util.ManifestUtilsTest.java

private void createJar(Manifest mf) throws Exception {
    mf.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION, "1.0");
    JarOutputStream out = new JarOutputStream(storage.getOutputStream(), mf);
    out.flush();
    out.close();//from   ww  w .  j  a v  a  2s  .  co m
}

From source file:org.apache.brooklyn.rest.resources.BundleAndTypeResourcesTest.java

private static File createJar(Map<String, String> files) throws Exception {
    File f = Os.newTempFile("osgi", "jar");

    JarOutputStream zip = new JarOutputStream(new FileOutputStream(f));

    for (Map.Entry<String, String> entry : files.entrySet()) {
        JarEntry ze = new JarEntry(entry.getKey());
        zip.putNextEntry(ze);/*from w ww. j  a  v  a  2 s . c  o  m*/
        zip.write(entry.getValue().getBytes());
    }

    zip.closeEntry();
    zip.flush();
    zip.close();

    return f;
}

From source file:org.eclipse.gemini.blueprint.test.internal.util.jar.JarUtils.java

/**
 * Creates a jar based on the given entries and manifest. This method will
 * always close the given output stream.
 * /*from   w w w .  j a v a2s .c  om*/
 * @param manifest jar manifest
 * @param entries map of resources keyed by the jar entry named
 * @param outputStream output stream for writing the jar
 * @return number of byte written to the jar
 */
public static int createJar(Manifest manifest, Map entries, OutputStream outputStream) throws IOException {
    int writtenBytes = 0;

    // load manifest
    // add it to the jar
    JarOutputStream jarStream = null;

    try {
        // add a jar stream on top
        jarStream = (manifest != null ? new JarOutputStream(outputStream, manifest)
                : new JarOutputStream(outputStream));

        // select fastest level (no compression)
        jarStream.setLevel(Deflater.NO_COMPRESSION);

        // add deps
        for (Iterator iter = entries.entrySet().iterator(); iter.hasNext();) {
            Map.Entry element = (Map.Entry) iter.next();

            String entryName = (String) element.getKey();

            // safety check - all entries must start with /
            if (!entryName.startsWith(SLASH))
                entryName = SLASH + entryName;

            Resource entryValue = (Resource) element.getValue();

            // skip special/duplicate entries (like MANIFEST.MF)
            if (MANIFEST_JAR_LOCATION.equals(entryName)) {
                iter.remove();
            } else {
                // write jar entry
                writtenBytes += JarUtils.writeToJar(entryValue, entryName, jarStream);
            }
        }
    } finally {
        try {
            jarStream.flush();
        } catch (IOException ex) {
            // ignore
        }
        try {
            jarStream.finish();
        } catch (IOException ex) {
            // ignore
        }

    }

    return writtenBytes;
}

From source file:com.glaf.core.util.ZipUtils.java

public static byte[] getZipBytes(Map<String, InputStream> dataMap) {
    byte[] bytes = null;
    try {//www.  jav  a  2s. co  m
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        BufferedOutputStream bos = new BufferedOutputStream(baos);
        JarOutputStream jos = new JarOutputStream(bos);
        if (dataMap != null) {
            Set<Entry<String, InputStream>> entrySet = dataMap.entrySet();
            for (Entry<String, InputStream> entry : entrySet) {
                String name = entry.getKey();
                InputStream inputStream = entry.getValue();
                if (name != null && inputStream != null) {
                    BufferedInputStream bis = new BufferedInputStream(inputStream);
                    JarEntry jarEntry = new JarEntry(name);
                    jos.putNextEntry(jarEntry);

                    while ((len = bis.read(buf)) >= 0) {
                        jos.write(buf, 0, len);
                    }

                    bis.close();
                    jos.closeEntry();
                }
            }
        }
        jos.flush();
        jos.close();
        bos.flush();
        bos.close();
        bytes = baos.toByteArray();
        baos.close();
        return bytes;
    } catch (Exception ex) {
        throw new RuntimeException(ex);
    }
}

From source file:JarHelper.java

/**
 * Recursively jars up the given path under the given directory.
 *//*  ww  w.j  a  v a 2s  .  c  om*/
private void jarDir(File dirOrFile2jar, JarOutputStream jos, String path) throws IOException {
    if (mVerbose)
        System.out.println("checking " + dirOrFile2jar);
    if (dirOrFile2jar.isDirectory()) {
        String[] dirList = dirOrFile2jar.list();
        String subPath = (path == null) ? "" : (path + dirOrFile2jar.getName() + SEP);
        if (path != null) {
            JarEntry je = new JarEntry(subPath);
            je.setTime(dirOrFile2jar.lastModified());
            jos.putNextEntry(je);
            jos.flush();
            jos.closeEntry();
        }
        for (int i = 0; i < dirList.length; i++) {
            File f = new File(dirOrFile2jar, dirList[i]);
            jarDir(f, jos, subPath);
        }
    } else {
        if (dirOrFile2jar.getCanonicalPath().equals(mDestJarName)) {
            if (mVerbose)
                System.out.println("skipping " + dirOrFile2jar.getPath());
            return;
        }

        if (mVerbose)
            System.out.println("adding " + dirOrFile2jar.getPath());
        FileInputStream fis = new FileInputStream(dirOrFile2jar);
        try {
            JarEntry entry = new JarEntry(path + dirOrFile2jar.getName());
            entry.setTime(dirOrFile2jar.lastModified());
            jos.putNextEntry(entry);
            while ((mByteCount = fis.read(mBuffer)) != -1) {
                jos.write(mBuffer, 0, mByteCount);
                if (mVerbose)
                    System.out.println("wrote " + mByteCount + " bytes");
            }
            jos.flush();
            jos.closeEntry();
        } catch (IOException ioe) {
            throw ioe;
        } finally {
            fis.close();
        }
    }
}

From source file:com.glaf.core.util.ZipUtils.java

public static byte[] toZipBytes(Map<String, byte[]> zipMap) {
    byte[] bytes = null;
    InputStream inputStream = null;
    BufferedInputStream bis = null;
    ByteArrayOutputStream baos = null;
    BufferedOutputStream bos = null;
    JarOutputStream jos = null;
    try {/*from ww w. ja va2s  . c o m*/
        baos = new ByteArrayOutputStream();
        bos = new BufferedOutputStream(baos);
        jos = new JarOutputStream(bos);
        if (zipMap != null) {
            Set<Entry<String, byte[]>> entrySet = zipMap.entrySet();
            for (Entry<String, byte[]> entry : entrySet) {
                String name = entry.getKey();
                byte[] x_bytes = entry.getValue();
                inputStream = new ByteArrayInputStream(x_bytes);
                if (name != null && inputStream != null) {
                    bis = new BufferedInputStream(inputStream);
                    JarEntry jarEntry = new JarEntry(name);
                    jos.putNextEntry(jarEntry);
                    while ((len = bis.read(buf)) >= 0) {
                        jos.write(buf, 0, len);
                    }
                    IOUtils.closeStream(bis);
                    jos.closeEntry();
                }
                IOUtils.closeStream(inputStream);
            }
        }
        jos.flush();
        jos.close();

        bytes = baos.toByteArray();
        IOUtils.closeStream(baos);
        IOUtils.closeStream(bos);
        return bytes;
    } catch (Exception ex) {
        throw new RuntimeException(ex);
    } finally {
        IOUtils.closeStream(inputStream);
        IOUtils.closeStream(baos);
        IOUtils.closeStream(bos);
    }
}

From source file:gov.nih.nci.restgen.util.JarHelper.java

/**
 * Recursively jars up the given path under the given directory.
 *///from w w w. j a v a2 s .  co  m
private void jarDir(File dirOrFile2jar, JarOutputStream jos, String path) throws IOException {
    //if (mVerbose)
    {
        //System.out.println("checking " + dirOrFile2jar);
    }
    if (dirOrFile2jar.isDirectory()) {
        String[] dirList = dirOrFile2jar.list();
        String subPath = (path == null) ? "" : (path + dirOrFile2jar.getName() + SEP);
        if (path != null) {
            JarEntry je = new JarEntry(subPath);
            je.setTime(dirOrFile2jar.lastModified());
            jos.putNextEntry(je);
            jos.flush();
            jos.closeEntry();
        }
        for (int i = 0; i < dirList.length; i++) {
            File f = new File(dirOrFile2jar, dirList[i]);
            jarDir(f, jos, subPath);
        }
    } else {
        if (dirOrFile2jar.getCanonicalPath().equals(mDestJarName)) {
            //if (mVerbose)
            {
                //System.out.println("skipping " + dirOrFile2jar.getPath());
            }
            return;
        }

        if (mVerbose) {
            //System.out.println("adding " + dirOrFile2jar.getPath());
        }
        FileInputStream fis = new FileInputStream(dirOrFile2jar);
        try {
            JarEntry entry = new JarEntry(path + dirOrFile2jar.getName());
            entry.setTime(dirOrFile2jar.lastModified());
            jos.putNextEntry(entry);
            while ((mByteCount = fis.read(mBuffer)) != -1) {
                jos.write(mBuffer, 0, mByteCount);
                if (mVerbose) {
                    //System.out.println("wrote " + mByteCount + " bytes");
                }
            }
            jos.flush();
            jos.closeEntry();
        } catch (IOException ioe) {
            throw ioe;
        } finally {
            fis.close();
        }
    }
}

From source file:edu.stanford.muse.email.JarDocCache.java

@Override
public synchronized void saveContents(String contents, String prefix, int msgNum)
        throws IOException, GeneralSecurityException {
    JarOutputStream jos = getContentsJarOS(prefix);

    // create the bytes
    ZipEntry ze = new ZipEntry(msgNum + ".content");
    ze.setMethod(ZipEntry.DEFLATED);
    //      byte[] buf = CryptoUtils.getEncryptedBytes(contents.getBytes("UTF-8"));
    byte[] buf = contents.getBytes("UTF-8");
    jos.putNextEntry(ze);/*from  w  ww  .  j av a 2s  . co  m*/
    jos.write(buf, 0, buf.length);
    jos.closeEntry();
    jos.flush();
}

From source file:edu.stanford.muse.email.JarDocCache.java

@Override
public synchronized void saveHeader(Document d, String prefix, int msgNum)
        throws FileNotFoundException, IOException {
    JarOutputStream jos = getHeadersJarOS(prefix);

    // create the bytes
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ObjectOutputStream headerOOS = new ObjectOutputStream(baos);
    headerOOS.writeObject(d);// w w w.j a  va  2s  .c o  m
    byte buf[] = baos.toByteArray();

    ZipEntry ze = new ZipEntry(msgNum + ".header");
    ze.setMethod(ZipEntry.DEFLATED);
    jos.putNextEntry(ze);
    jos.write(buf, 0, buf.length);
    jos.closeEntry();
    jos.flush();
}

From source file:be.wimsymons.intellij.polopolyimport.PPImporter.java

private byte[] makeJar(VirtualFile[] files) throws IOException {
    JarOutputStream jarOS = null;
    ByteArrayOutputStream byteOS = null;
    try {/*from ww  w . j  ava2s . co  m*/
        progressIndicator.setIndeterminate(true);

        // build list of files to process
        Collection<VirtualFile> filesToProcess = new LinkedHashSet<VirtualFile>();
        for (VirtualFile virtualFile : files) {
            filesToProcess.addAll(getFileList(virtualFile));
        }

        progressIndicator.setIndeterminate(false);
        progressIndicator.setFraction(0.0D);

        totalCount = filesToProcess.size();

        byteOS = new ByteArrayOutputStream();
        jarOS = new JarOutputStream(byteOS);
        int counter = 0;
        for (VirtualFile file : filesToProcess) {
            if (progressIndicator.isCanceled()) {
                break;
            }

            counter++;
            progressIndicator.setFraction((double) counter / (double) totalCount * 0.5D);
            progressIndicator.setText("Adding " + file.getName() + " ...");

            if (canImport(file)) {
                LOGGER.info("Adding file " + (counter + 1) + "/" + totalCount);
                addToJar(jarOS, file);
            } else {
                skippedCount++;
            }
        }
        jarOS.flush();
        return progressIndicator.isCanceled() ? null : byteOS.toByteArray();
    } finally {
        Closeables.close(jarOS, true);
        Closeables.close(byteOS, true);
    }
}