Example usage for java.util.zip GZIPOutputStream GZIPOutputStream

List of usage examples for java.util.zip GZIPOutputStream GZIPOutputStream

Introduction

In this page you can find the example usage for java.util.zip GZIPOutputStream GZIPOutputStream.

Prototype

public GZIPOutputStream(OutputStream out) throws IOException 

Source Link

Document

Creates a new output stream with a default buffer size.

Usage

From source file:fr.ens.biologie.genomique.eoulsan.io.CompressionType.java

/**
 * Create a GZip output stream./*from www . ja  v  a2s .c  o  m*/
 * @param os the output stream to compress
 * @return a compressed output stream
 * @throws IOException if an error occurs while creating the output stream
 */
public static OutputStream createGZipOutputStream(final OutputStream os) throws IOException {

    return new GZIPOutputStream(os);
}

From source file:com.pinterest.terrapin.zookeeper.ViewInfo.java

public byte[] toCompressedJson() throws Exception {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    GZIPOutputStream zipOs = new GZIPOutputStream(out);
    zipOs.write(this.toJson());
    zipOs.close();//from   ww w.j a  va  2s . co  m
    byte[] data = out.toByteArray();
    return data;
}

From source file:com.asakusafw.runtime.stage.output.BridgeOutputFormat.java

private static void save(Configuration conf, List<OutputSpec> specs) {
    assert conf != null;
    assert specs != null;
    for (OutputSpec spec : specs) {
        if (spec.resolved) {
            throw new IllegalStateException();
        }/*w w w . ja va 2 s .c  om*/
    }
    ByteArrayOutputStream sink = new ByteArrayOutputStream();
    try (DataOutputStream output = new DataOutputStream(new GZIPOutputStream(new Base64OutputStream(sink)))) {
        WritableUtils.writeVLong(output, SERIAL_VERSION);
        WritableUtils.writeVInt(output, specs.size());
        for (OutputSpec spec : specs) {
            WritableUtils.writeString(output, spec.basePath);
            WritableUtils.writeVInt(output, spec.deletePatterns.size());
            for (String pattern : spec.deletePatterns) {
                WritableUtils.writeString(output, pattern);
            }
        }
    } catch (IOException e) {
        throw new IllegalStateException(e);
    }
    conf.set(KEY, new String(sink.toByteArray(), ASCII));
}

From source file:com.google.cloud.dataflow.sdk.io.CompressedSourceTest.java

private static byte[] compressGzip(byte[] input) throws IOException {
    ByteArrayOutputStream res = new ByteArrayOutputStream();
    try (GZIPOutputStream gzipStream = new GZIPOutputStream(res)) {
        gzipStream.write(input);//from  ww w . j  av a  2  s.c o m
    }
    return res.toByteArray();
}

From source file:com.microsoft.tfs.core.clients.versioncontrol.engines.internal.workers.BaselineUpdaterWorker.java

/**
 * {@inheritDoc}/*from w  w  w  .j a v a2  s  . c  o m*/
 */
@Override
public WorkerStatus call() throws Exception {
    /*
     * Try not to run any code outside this try block, so our Throwable
     * catch below can report every error (even RuntimeExceptions).
     */
    try {
        boolean completedSuccessfully = false;
        final int token = state.getBaselineFolderCollection().lockForRead();

        try {
            // Present the read lock token that we already hold to prevent
            // it from attempting to acquire another read lock for the call
            // to GetNewBaselineLocation.
            String baselineFilePath = state.getBaselineFolderCollection().getNewBaselineLocation(
                    request.getBaselineFileGUID(), request.getBaselinePartitionLocalItem(), token);

            // Get the uncompressed file size.
            final File fi = new File(baselineFilePath);
            final long uncompressedFileSize = fi.length();

            // Set status message for the monitor.
            monitor.setCurrentWorkDescription(
                    MessageFormat.format(Messages.getString("BaselineUpdaterWorker.UpdatingBaselineFormat"), //$NON-NLS-1$
                            baselineFilePath));

            // Add the .gz extension to this baseline.
            baselineFilePath = baselineFilePath + BaselineFolder.getGzipExtension();

            if (uncompressedFileSize < Worker.MAX_GZIP_INPUT_SIZE) {
                final byte[] buffer = new byte[4096];
                byte[] hashValue = null;

                MessageDigest md5Digest = null;
                if (request.getHashValue() != null) {
                    md5Digest = MessageDigest.getInstance("MD5"); //$NON-NLS-1$
                }

                final GZIPOutputStream outputStream = new GZIPOutputStream(
                        new FileOutputStream(baselineFilePath));
                final String sourceLocalItem = request.getSourceLocalItem();
                final FileSystemUtils util = FileSystemUtils.getInstance();
                final FileSystemAttributes attrs = util.getAttributes(sourceLocalItem);

                InputStream inputStream;
                if (attrs.isSymbolicLink()) {
                    final String linkTarget = util.getSymbolicLink(sourceLocalItem);
                    inputStream = new ByteArrayInputStream(linkTarget.getBytes("UTF-8")); //$NON-NLS-1$
                } else {
                    inputStream = new FileInputStream(request.getSourceLocalItem());
                }

                try {
                    int bytesRead;

                    while (true) {
                        bytesRead = inputStream.read(buffer, 0, buffer.length);

                        if (bytesRead <= 0) {
                            break;
                        }

                        if (null != md5Digest) {
                            md5Digest.update(buffer, 0, bytesRead);
                        }

                        outputStream.write(buffer, 0, bytesRead);
                    }

                    if (null != md5Digest) {
                        hashValue = md5Digest.digest();
                    }
                } finally {
                    if (outputStream != null) {
                        IOUtils.closeSafely(outputStream);
                    }
                    if (inputStream != null) {
                        IOUtils.closeSafely(inputStream);
                    }
                }

                if (null != hashValue && 16 == hashValue.length && null != request.getHashValue()
                        && 16 == request.getHashValue().length
                        && !Arrays.equals(request.getHashValue(), hashValue)) {
                    // The hash value didn't match the provided hash value.
                    // Delete the baseline from disk.
                    FileHelpers.deleteFileWithoutException(baselineFilePath);
                } else {
                    completedSuccessfully = true;
                }
            } else {
                // TODO: We didn't attempt to gzip.
            }
        } catch (final Exception ex) {
            log.trace("BaselineUpdater", ex); //$NON-NLS-1$
        } finally {
            state.getBaselineFolderCollection().unlockForRead(token);

            if (!completedSuccessfully) {
                state.addFailedRequest(request);
            }
        }
    } catch (final Throwable t) {
        /*
         * An actual error happened. We have to communicate this problem to
         * the thread submitting tasks so it can take the correct action
         * (shut down other workers).
         */
        state.setFatalError(t);
        return new WorkerStatus(this, FinalState.ERROR);
    }

    return new WorkerStatus(this, FinalState.NORMAL);
}

From source file:com.flurry.proguard.UploadProGuardMapping.java

/**
 * Create a gzipped tar archive containing the ProGuard mapping file
 *
 * @param file the mapping.txt file/*from  w  w  w .j  a  v a2s  .  com*/
 * @param uuid the build uuid
 * @return the tar-gzipped archive
 */
private static File createArchive(File file, String uuid) {
    try {
        File tarZippedFile = File.createTempFile("tar-zipped-file", ".tgz");
        TarArchiveOutputStream taos = new TarArchiveOutputStream(
                new GZIPOutputStream(new BufferedOutputStream(new FileOutputStream(tarZippedFile))));
        taos.putArchiveEntry(new TarArchiveEntry(file, uuid + ".txt"));
        IOUtils.copy(new FileInputStream(file), taos);
        taos.closeArchiveEntry();
        taos.finish();
        taos.close();
        return tarZippedFile;
    } catch (IOException e) {
        failWithError("IO Exception while trying to tar and zip the file.", e);
        return null;
    }
}

From source file:com.moss.simpledeb.core.DebWriter.java

private byte[] buildGzipTar(List<ArchivePath> paths) throws Exception {

    byte[] tarData;
    {/*from  w ww .j av  a 2  s .co m*/
        ByteArrayOutputStream tarOut = new ByteArrayOutputStream();
        TarArchiveOutputStream tar = new TarArchiveOutputStream(tarOut);

        Set<String> writtenPaths = new HashSet<String>();
        for (ArchivePath path : paths) {
            String name = path.entry().getName();

            if (writtenPaths.contains(name)) {
                throw new RuntimeException("Duplicate archive entry: " + name);
            }

            writtenPaths.add(name);

            tar.putArchiveEntry(path.entry());

            if (!path.entry().isDirectory()) {
                InputStream in = path.read();
                byte[] buffer = new byte[1024 * 10];
                for (int numRead = in.read(buffer); numRead != -1; numRead = in.read(buffer)) {
                    tar.write(buffer, 0, numRead);
                }
                in.close();
            }

            tar.closeArchiveEntry();
        }

        tar.close();
        tarData = tarOut.toByteArray();
    }

    byte[] gzipData;
    {
        ByteArrayOutputStream gzipOut = new ByteArrayOutputStream();
        GZIPOutputStream gzip = new GZIPOutputStream(gzipOut);
        gzip.write(tarData);
        gzip.close();

        gzipData = gzipOut.toByteArray();
    }

    return gzipData;
}

From source file:de.mpg.escidoc.services.fledgeddata.webservice.oaiServlet.java

/**
 * Get a response Writer depending on acceptable encodings
 * @param request the servlet's request information
 * @param response the servlet's response information
 * @exception IOException an I/O error occurred
 *///from  w w  w . j a  va  2s. c  o m
public static Writer getWriter(HttpServletRequest request, HttpServletResponse response) throws IOException {
    Writer out;
    String encodings = request.getHeader("Accept-Encoding");

    if (encodings != null && encodings.indexOf("gzip") != -1) {
        response.setHeader("Content-Encoding", "gzip");
        out = new OutputStreamWriter(new GZIPOutputStream(response.getOutputStream()), "UTF-8");
    } else if (encodings != null && encodings.indexOf("deflate") != -1) {
        response.setHeader("Content-Encoding", "deflate");
        out = new OutputStreamWriter(new DeflaterOutputStream(response.getOutputStream()), "UTF-8");
    } else {
        out = response.getWriter();
    }
    return out;
}

From source file:edu.cornell.med.icb.goby.modes.CountsArchiveToBedGraphMode.java

/**
 * Run the map2text mode.//  ww w . j av  a2 s  .c  om
 *
 * @throws java.io.IOException error reading / writing
 */
@Override
public void execute() throws IOException {
    PrintWriter writer = null;
    try {
        writer = new PrintWriter(
                new GZIPOutputStream(new FastBufferedOutputStream(new FileOutputStream(outputFile + ".gz"))));
        writer.write("track type=bedGraph name=" + label + " visibility=full viewLimits=1:200\n");
        final AlignmentReaderImpl alignment = new AlignmentReaderImpl(inputBasename);
        alignment.readHeader();
        alignment.close();
        final IndexedIdentifier referenceIds = alignment.getTargetIdentifiers();
        final DoubleIndexedIdentifier backwards = new DoubleIndexedIdentifier(referenceIds);
        final CountsArchiveReader reader = new CountsArchiveReader(inputBasename,
                alternativeCountArchiveExtension);

        for (int referenceIndex = 0; referenceIndex < reader.getNumberOfIndices(); referenceIndex++) {
            String referenceId = backwards.getId(referenceIndex).toString();
            boolean processThisSequence = true;
            // prepare reference ID for UCSC genome browser.
            if ("MT".equalsIgnoreCase(referenceId)) {
                // patch chromosome name for UCSC genome browser:
                referenceId = "M";
            }

            // ignore c22_H2, c5_H2, and other contigs but not things like chr1 (mm9)
            if (referenceId.startsWith("c") && !referenceId.startsWith("chr")) {
                processThisSequence = false;
            }

            // ignore NT_*
            if (referenceId.startsWith("NT_")) {
                processThisSequence = false;
            }

            if (filterByReferenceNames && !includeReferenceNames.contains(referenceId)) {
                processThisSequence = false;
            }

            if (processThisSequence) {
                // prepend the reference id with "chr" if it doesn't use that already
                final String chromosome;
                if (referenceId.startsWith("chr")) {
                    chromosome = referenceId;
                } else {
                    chromosome = "chr" + referenceId;
                }

                long sumCount = 0;
                int numCounts = 0;

                final CountsReader counts = reader.getCountReader(referenceIndex);
                int writePosition;
                while (counts.hasNextTransition()) {
                    counts.nextTransition();
                    final int length = counts.getLength();

                    final int count = counts.getCount();
                    final int position = counts.getPosition();
                    writePosition = position + 1;
                    if (count != 0) {
                        writer.printf("%s %d %d %d\n", chromosome, writePosition, writePosition + length,
                                count);
                    }
                    sumCount += count;
                    numCounts++;
                }
                final double averageCount = sumCount / (double) numCounts;
                System.out.println("average count for sequence " + referenceId + " " + averageCount);
            }
        }
    } finally {
        IOUtils.closeQuietly(writer);
    }
}

From source file:uk.ac.bbsrc.tgac.miso.integration.util.IntegrationUtils.java

public static byte[] compress(byte[] content) throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    Base64OutputStream b64os = new Base64OutputStream(baos);
    GZIPOutputStream gzip = new GZIPOutputStream(b64os);
    gzip.write(content);/*ww w .j a v  a2s . c o  m*/
    gzip.close();
    return baos.toByteArray();
}