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:com.zotoh.core.io.StreamUte.java

/**
 * @param bits/*from w  ww.ja v  a  2s.c om*/
 * @return
 * @throws IOException
 */
public static byte[] gzip(byte[] bits) throws IOException {

    ByteOStream baos = new ByteOStream();
    GZIPOutputStream g = new GZIPOutputStream(baos);

    if (bits != null && bits.length > 0) {
        g.write(bits, 0, bits.length);
        g.close();
    }

    return baos.asBytes();
}

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//from 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;
        }
    });
}

From source file:io.scigraph.services.jersey.dynamic.SwaggerFilter.java

@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
        throws IOException, ServletException {
    // Capture the output of the filter chain
    ByteArrayResponseWrapper wrappedResp = new ByteArrayResponseWrapper((HttpServletResponse) response);
    chain.doFilter(request, wrappedResp);
    if (isGzip(request)) {
        try (InputStream is = new ByteArrayInputStream(wrappedResp.getBytes());
                GZIPInputStream gis = new GZIPInputStream(is);
                ByteArrayOutputStream bs = new ByteArrayOutputStream();
                GZIPOutputStream gzos = new GZIPOutputStream(bs)) {
            byte[] newApi = writeDynamicResource(gis);
            gzos.write(newApi);/* w  w w .  j  a v  a 2 s  .  c o m*/
            gzos.close();
            byte[] output = bs.toByteArray();
            response.setContentLength(output.length);
            response.getOutputStream().write(output);
        }
    } else {
        try (InputStream is = new ByteArrayInputStream(wrappedResp.getBytes());
                ByteArrayOutputStream bs = new ByteArrayOutputStream()) {
            byte[] newApi = writeDynamicResource(is);
            response.setContentLength(newApi.length);
            response.getOutputStream().write(newApi);
        }

    }
}

From source file:io.druid.storage.s3.S3DataSegmentPullerTest.java

@Test
public void testGZUncompress() throws S3ServiceException, IOException, SegmentLoadingException {
    final String bucket = "bucket";
    final String keyPrefix = "prefix/dir/0";
    final RestS3Service s3Client = EasyMock.createStrictMock(RestS3Service.class);
    final byte[] value = bucket.getBytes("utf8");

    final File tmpFile = Files.createTempFile("gzTest", ".gz").toFile();
    tmpFile.deleteOnExit();// ww w  .  ja v a  2s.  c om
    try (OutputStream outputStream = new GZIPOutputStream(new FileOutputStream(tmpFile))) {
        outputStream.write(value);
    }

    S3Object object0 = new S3Object();

    object0.setBucketName(bucket);
    object0.setKey(keyPrefix + "/renames-0.gz");
    object0.setLastModifiedDate(new Date(0));
    object0.setDataInputStream(new FileInputStream(tmpFile));

    File tmpDir = Files.createTempDirectory("gzTestDir").toFile();

    try {
        EasyMock.expect(
                s3Client.getObjectDetails(EasyMock.<S3Bucket>anyObject(), EasyMock.eq(object0.getKey())))
                .andReturn(null).once();
        EasyMock.expect(s3Client.getObject(EasyMock.eq(bucket), EasyMock.eq(object0.getKey())))
                .andReturn(object0).once();
        S3DataSegmentPuller puller = new S3DataSegmentPuller(s3Client);

        EasyMock.replay(s3Client);
        FileUtils.FileCopyResult result = puller
                .getSegmentFiles(new S3DataSegmentPuller.S3Coords(bucket, object0.getKey()), tmpDir);
        EasyMock.verify(s3Client);

        Assert.assertEquals(value.length, result.size());
        File expected = new File(tmpDir, "renames-0");
        Assert.assertTrue(expected.exists());
        Assert.assertEquals(value.length, expected.length());
    } finally {
        org.apache.commons.io.FileUtils.deleteDirectory(tmpDir);
    }
}

From source file:byps.BWire.java

/**
 * Writes a ByteBuffer to an OutputStream.
 * Closes the OutputStream./*  w w w  . j a v  a 2  s. c o m*/
 * @param buf
 * @param os
 * @throws IOException
 */
public static void bufferToStream(ByteBuffer buf, boolean gzip, OutputStream os) throws IOException {
    if (gzip) {
        os = new GZIPOutputStream(os);
    }

    WritableByteChannel wch = null;
    try {
        wch = Channels.newChannel(os);
        wch.write(buf);
    } finally {
        if (wch != null)
            wch.close();
    }
}

From source file:edu.sdsc.scigraph.services.jersey.dynamic.SwaggerFilter.java

@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
        throws IOException, ServletException {
    boolean gzip = isGzip(request);

    // Capture the output of the filter chain
    ByteArrayResponseWrapper wrappedResp = new ByteArrayResponseWrapper((HttpServletResponse) response);
    chain.doFilter(request, wrappedResp);
    if (gzip) {/*from  w ww  .j ava  2 s .co  m*/
        try (InputStream is = new ByteArrayInputStream(wrappedResp.getBytes());
                GZIPInputStream gis = new GZIPInputStream(is);
                ByteArrayOutputStream bs = new ByteArrayOutputStream();
                GZIPOutputStream gzos = new GZIPOutputStream(bs)) {
            byte[] newApi = writeDynamicResource(gis);
            gzos.write(newApi);
            gzos.close();
            byte[] output = bs.toByteArray();
            response.setContentLength(output.length);
            response.getOutputStream().write(output);
        }
    } else {
        try (InputStream is = new ByteArrayInputStream(wrappedResp.getBytes());
                ByteArrayOutputStream bs = new ByteArrayOutputStream()) {
            byte[] newApi = writeDynamicResource(is);
            response.setContentLength(newApi.length);
            response.getOutputStream().write(newApi);
        }

    }
}

From source file:com.shopzilla.hadoop.repl.commands.util.ClusterStateManager.java

public static void compressFiles(Collection<File> files, File output) throws IOException {
    // Create the output stream for the output file
    FileOutputStream fos = new FileOutputStream(output);
    // Wrap the output file stream in streams that will tar and gzip everything
    TarArchiveOutputStream taos = new TarArchiveOutputStream(
            new GZIPOutputStream(new BufferedOutputStream(fos)));
    // TAR has an 8 gig file limit by default, this gets around that
    taos.setBigNumberMode(TarArchiveOutputStream.BIGNUMBER_STAR); // to get past the 8 gig limit
    // TAR originally didn't support long file names, so enable the support for it
    taos.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU);

    // Get to putting all the files in the compressed output file
    for (File f : files) {
        addFilesToCompression(taos, f, ".");
    }/*  w  w w . j a  v a2s  .  co m*/

    // Close everything up
    taos.close();
    fos.close();
}

From source file:com.eviware.soapui.impl.wsdl.support.CompressionSupport.java

private static byte[] GZIPCompress(byte[] requestContent) throws IOException {
    ByteArrayOutputStream compressedContent = new ByteArrayOutputStream();
    GZIPOutputStream gzipstream = new GZIPOutputStream(compressedContent);
    gzipstream.write(requestContent);/*from   w ww.j a  va 2  s  . co  m*/
    gzipstream.finish();

    // get the compressed content
    return compressedContent.toByteArray();
}

From source file:edu.emory.mathcs.nlp.util.IOUtilsTest.java

@Test
public void fileNonStdFileSystem() throws Exception {
    FileSystem fs = Jimfs.newFileSystem(Configuration.unix());
    Path testFile = fs.getPath("/foo.txt");
    try (Writer writer = Files.newBufferedWriter(testFile)) {
        writer.write(THIS_IS_THE_CEREAL_SHOT_FROM_GUNS);
    }//from   w  ww  .  ja v a2  s . c  om

    try (InputStream is = IOUtils.createArtifactInputStream(testFile)) {
        String contents = org.apache.commons.io.IOUtils.toString(is, "utf-8");
        assertEquals(THIS_IS_THE_CEREAL_SHOT_FROM_GUNS, contents);
    }

    String uri = testFile.toUri().toString();
    try (InputStream is = IOUtils.createArtifactInputStream(uri)) {
        String contents = org.apache.commons.io.IOUtils.toString(is, "utf-8");
        assertEquals(THIS_IS_THE_CEREAL_SHOT_FROM_GUNS, contents);
    }

    Path testFilexz = fs.getPath("/foo.txt.xz");
    try (Writer writer = new OutputStreamWriter(
            new XZOutputStream(Files.newOutputStream(testFilexz), new LZMA2Options()), UTF_8)) {
        writer.write(THIS_IS_THE_CEREAL_SHOT_FROM_GUNS);
    }

    try (InputStream is = IOUtils.createArtifactInputStream(testFilexz)) {
        String contents = org.apache.commons.io.IOUtils.toString(is, "utf-8");
        assertEquals(THIS_IS_THE_CEREAL_SHOT_FROM_GUNS, contents);
    }

    uri = testFilexz.toUri().toString();
    try (InputStream is = IOUtils.createArtifactInputStream(uri)) {
        String contents = org.apache.commons.io.IOUtils.toString(is, "utf-8");
        assertEquals(THIS_IS_THE_CEREAL_SHOT_FROM_GUNS, contents);
    }

    Path testFilegz = fs.getPath("/foo.txt.gz");
    try (Writer writer = new OutputStreamWriter(new GZIPOutputStream(Files.newOutputStream(testFilegz)),
            UTF_8)) {
        writer.write(THIS_IS_THE_CEREAL_SHOT_FROM_GUNS);
    }

    try (InputStream is = IOUtils.createArtifactInputStream(testFilegz)) {
        String contents = org.apache.commons.io.IOUtils.toString(is, "utf-8");
        assertEquals(THIS_IS_THE_CEREAL_SHOT_FROM_GUNS, contents);
    }

    uri = testFilegz.toUri().toString();
    try (InputStream is = IOUtils.createArtifactInputStream(uri)) {
        String contents = org.apache.commons.io.IOUtils.toString(is, "utf-8");
        assertEquals(THIS_IS_THE_CEREAL_SHOT_FROM_GUNS, contents);
    }

    // test plain old file system
    try (InputStream is = IOUtils.createArtifactInputStream("src/test/resources/a/test/some.txt")) {
        String contents = org.apache.commons.io.IOUtils.toString(is, "utf-8");
        assertEquals(THIS_IS_THE_CEREAL_SHOT_FROM_GUNS, contents);
    }

    // test classpath
    try (InputStream is = IOUtils.createArtifactInputStream("a/test/some.txt")) {
        String contents = org.apache.commons.io.IOUtils.toString(is, "utf-8");
        assertEquals(THIS_IS_THE_CEREAL_SHOT_FROM_GUNS, contents);
    }
}

From source file:uk.ac.ebi.eva.test.utils.JobTestUtils.java

public static void makeGzipFile(String content, String file) throws IOException {
    try (FileOutputStream output = new FileOutputStream(file)) {
        try (Writer writer = new OutputStreamWriter(new GZIPOutputStream(output), "UTF-8")) {
            writer.write(content);//from  w ww . jav a2 s  .co m
        }
    }
}