Example usage for java.util.zip GZIPOutputStream finish

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

Introduction

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

Prototype

public void finish() throws IOException 

Source Link

Document

Finishes writing compressed data to the output stream without closing the underlying stream.

Usage

From source file:org.jtalks.poulpe.util.databasebackup.contentprovider.impl.GzipContentProvider.java

@Override
public void writeContent(OutputStream output) throws FileDownloadException {
    try {/*from   w w  w .j  av  a2 s  . com*/
        GZIPOutputStream gzipOutput = getGZIPOutputStream(output);
        contentProvider.writeContent(gzipOutput);
        gzipOutput.finish();

    } catch (IOException e) {
        throw new GzipPackingException(e);
    }
}

From source file:com.zimbra.common.util.ByteUtil.java

/**
 * compress the supplied data using GZIPOutputStream
 * and return the compressed data./*from   w  w  w .  j  a v a 2  s . co m*/
 * @param data data to compress
 * @return compressesd data
 */
public static byte[] compress(byte[] data) throws IOException {
    ByteArrayOutputStream baos = null;
    GZIPOutputStream gos = null;
    try {
        baos = new ByteArrayOutputStream(data.length); //data.length overkill
        gos = new GZIPOutputStream(baos);
        gos.write(data);
        gos.finish();
        return baos.toByteArray();
    } finally {
        if (gos != null) {
            gos.close();
        } else if (baos != null)
            baos.close();
    }
}

From source file:com.epam.wilma.gepard.testclient.compression.gzip.GzipCompressor.java

/**
 * Compress the input stream with GZIP.//from   w ww.j a  va  2  s . com
 *
 * @param inputStream is the input
 * @return with compressed format
 * @throws IOException if problem occurs during the compression
 */
public InputStream compress(final InputStream inputStream) throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    GZIPOutputStream gout = new GZIPOutputStream(baos);
    //... Code to read from your original uncompressed data and write to gout.
    IOUtils.copy(inputStream, gout);
    gout.finish();
    //Convert to InputStream.
    return new ByteArrayInputStream(baos.toByteArray());
}

From source file:com.epam.wilma.test.server.compress.gzip.GzipCompressor.java

/**
 * Compresses an {@link InputStream} object into gzip.
 * @param source the input stream that will be compressed.
 * @return a {@link ByteArrayOutputStream} containing gzipped byte array.
 *///from w w w . ja  va  2 s .  co  m
public ByteArrayOutputStream compress(final InputStream source) {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    try {
        GZIPOutputStream gout = new GZIPOutputStream(baos);
        //... Code to read from your original uncompressed data and write to gout.
        IOUtils.copy(source, gout);
        gout.finish();
        gout.close();
    } catch (IOException e) {
        throw new SystemException("error", e);
    }
    return baos;
}

From source file:org.apache.wink.itest.contentencoding.WinkContentEncodingTest.java

/**
 * Tests sending in small bits of gzip encoded content.
 * /*from www.j a  v a  2  s . c  o m*/
 * @throws HttpException
 * @throws IOException
 */
public void testSendLargeGzipContentEncoded() throws HttpException, IOException {
    ByteArrayOutputStream originalContent = new ByteArrayOutputStream();
    for (int c = 0; c < 5000000; ++c) {
        originalContent.write(c);
    }

    /*
     * gzip the contents
     */
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    GZIPOutputStream gzipOut = new GZIPOutputStream(baos);
    originalContent.writeTo(gzipOut);
    gzipOut.finish();
    byte[] content = baos.toByteArray();

    ClientResponse response = client.resource(BASE_URI + "/bigbook/mirror").accept(MediaType.TEXT_PLAIN)
            .header("Content-Encoding", "gzip").contentType("text/plain; charset=utf-8").post(content);

    assertEquals(200, response.getStatusCode());
    InputStream responseStream = response.getEntity(InputStream.class);
    for (int c = 0; c < 5000000; ++c) {
        assertEquals(c % 256, responseStream.read());
    }
}

From source file:org.apache.wink.itest.contentencoding.WinkContentEncodingTest.java

/**
 * Tests sending in small bits of gzip encoded content.
 * //from   w ww.  ja va2s.  co m
 * @throws HttpException
 * @throws IOException
 */
public void testSendLargeGzipContentEncodedAndReceiveContentEncoded() throws HttpException, IOException {
    ByteArrayOutputStream originalContent = new ByteArrayOutputStream();
    for (int c = 0; c < 5000000; ++c) {
        originalContent.write(c);
    }

    /*
     * gzip the contents
     */
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    GZIPOutputStream gzipOut = new GZIPOutputStream(baos);
    originalContent.writeTo(gzipOut);
    gzipOut.finish();
    byte[] content = baos.toByteArray();

    ClientResponse response = client.resource(BASE_URI + "/bigbook/mirror").header("Accept-Encoding", "gzip")
            .accept(MediaType.TEXT_PLAIN).header("Content-Encoding", "gzip")
            .contentType("text/plain; charset=utf-8").post(content);

    assertEquals(200, response.getStatusCode());
    InputStream responseStream = new GZIPInputStream(response.getEntity(InputStream.class));
    for (int c = 0; c < 5000000; ++c) {
        assertEquals(c % 256, responseStream.read());
    }
}

From source file:ca.uhn.fhir.rest.client.apache.GZipContentInterceptor.java

@Override
public void interceptRequest(IHttpRequest theRequestInterface) {
    HttpRequestBase theRequest = ((ApacheHttpRequest) theRequestInterface).getApacheRequest();
    if (theRequest instanceof HttpEntityEnclosingRequest) {
        Header[] encodingHeaders = theRequest.getHeaders(Constants.HEADER_CONTENT_ENCODING);
        if (encodingHeaders == null || encodingHeaders.length == 0) {
            HttpEntityEnclosingRequest req = (HttpEntityEnclosingRequest) theRequest;

            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            GZIPOutputStream gos;
            try {
                gos = new GZIPOutputStream(bos);
                req.getEntity().writeTo(gos);
                gos.finish();
            } catch (IOException e) {
                ourLog.warn("Failed to GZip outgoing content", e);
                return;
            }//from   ww  w  .j  av  a 2  s.c om

            byte[] byteArray = bos.toByteArray();
            ByteArrayEntity newEntity = new ByteArrayEntity(byteArray);
            req.setEntity(newEntity);
            req.addHeader(Constants.HEADER_CONTENT_ENCODING, "gzip");
        }
    }

}

From source file:org.apache.wink.itest.contentencoding.WinkContentEncodingTest.java

/**
 * Tests sending in small bits of gzip encoded content.
 * /*from www  . j  a v  a 2s .  co m*/
 * @throws HttpException
 * @throws IOException
 */
public void testSendSmallGzipContentEncoded() throws HttpException, IOException {
    ByteArrayOutputStream originalContent = new ByteArrayOutputStream();
    originalContent.write("Hello world".getBytes("UTF-8"));

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ChunkedOutputStream chunkedOut = new ChunkedOutputStream(baos);
    GZIPOutputStream gzipOut = new GZIPOutputStream(chunkedOut);

    originalContent.writeTo(gzipOut);

    gzipOut.finish();
    chunkedOut.finish();
    byte[] content = baos.toByteArray();

    ClientResponse response = client.resource(BASE_URI + "/bigbook").accept(MediaType.TEXT_PLAIN)
            .header("Transfer-Encoding", "chunked").header("Content-Encoding", "gzip")
            .contentType("text/plain; charset=utf-8").post(content);
    assertEquals(200, response.getStatusCode());
    String responseBody = response.getEntity(String.class);
    assertEquals("Hello world" + "helloworld", responseBody);
}

From source file:ezbake.deployer.utilities.ArtifactHelpers.java

/**
 * Append to the given ArchiveInputStream writing to the given outputstream, the given entries to add.
 * This will duplicate the InputStream to the Output.
 *
 * @param inputStream - archive input to append to
 * @param output      - what to copy the modified archive to
 * @param filesToAdd  - what entries to append.
 *//*from ww w  .  j  ava  2s . c  o m*/
private static void appendFilesInTarArchive(ArchiveInputStream inputStream, OutputStream output,
        Iterable<ArtifactDataEntry> filesToAdd) throws DeploymentException {
    ArchiveStreamFactory asf = new ArchiveStreamFactory();

    try {
        HashMap<String, ArtifactDataEntry> newFiles = new HashMap<>();
        for (ArtifactDataEntry entry : filesToAdd) {
            newFiles.put(entry.getEntry().getName(), entry);
        }
        GZIPOutputStream gzs = new GZIPOutputStream(output);
        TarArchiveOutputStream aos = (TarArchiveOutputStream) asf
                .createArchiveOutputStream(ArchiveStreamFactory.TAR, gzs);
        aos.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU);
        // copy the existing entries
        ArchiveEntry nextEntry;
        while ((nextEntry = inputStream.getNextEntry()) != null) {
            //If we're passing in the same file, don't copy into the new archive
            if (!newFiles.containsKey(nextEntry.getName())) {
                aos.putArchiveEntry(nextEntry);
                IOUtils.copy(inputStream, aos);
                aos.closeArchiveEntry();
            }
        }

        for (ArtifactDataEntry entry : filesToAdd) {
            aos.putArchiveEntry(entry.getEntry());
            IOUtils.write(entry.getData(), aos);
            aos.closeArchiveEntry();
        }
        aos.finish();
        gzs.finish();
    } catch (ArchiveException | IOException e) {
        log.error(e.getMessage(), e);
        throw new DeploymentException(e.getMessage());
    }
}

From source file:com.ning.metrics.collector.processing.db.DatabaseFeedStorage.java

@Override
public void addOrUpdateFeed(final String key, final Feed feed) {
    dbi.withHandle(new HandleCallback<Void>() {

        @Override//ww w .j  a va  2 s .co m
        public Void withHandle(Handle handle) throws Exception {
            final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
            final GZIPOutputStream zipStream = new GZIPOutputStream(outputStream);
            mapper.writeValue(zipStream, feed);
            zipStream.finish();

            handle.createStatement(
                    "INSERT INTO feeds (feed_key, feed) VALUES (:key, :feed) ON DUPLICATE KEY UPDATE feed = :feed")
                    .bind("key", key).bind("feed", outputStream.toByteArray()).execute();

            return null;
        }
    });
}