Example usage for java.util.zip GZIPOutputStream close

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

Introduction

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

Prototype

public void close() throws IOException 

Source Link

Document

Writes remaining compressed data to the output stream and closes the underlying stream.

Usage

From source file:org.opensextant.util.FileUtility.java

/**
 *
 * @param text//from   www .j  a v a  2  s. c  o  m
 *            buffer to write
 * @param filepath
 *            path to file
 * @return status true if file was written
 * @throws IOException
 *             on error
 */
public static boolean writeGzipFile(String text, String filepath) throws IOException {
    if (filepath == null || text == null) {
        return false;
    }

    final FileOutputStream outstream = new FileOutputStream(filepath);
    final GZIPOutputStream gzout = new GZIPOutputStream(new BufferedOutputStream(outstream), ioBufferSize);

    gzout.write(text.getBytes(default_encoding));

    gzout.flush();
    gzout.finish();

    gzout.close();
    outstream.close();
    return true;

}

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

@Test
public void testCompressedSerialization() throws Exception {
    // Check that compressed json serialization works correctly.
    byte[] compressedJson = viewInfo.toCompressedJson();
    GZIPInputStream zipIn = new GZIPInputStream(new ByteArrayInputStream(compressedJson));
    assertEquals(JSON, new String(IOUtils.toByteArray(zipIn)));

    // Check that compressed json deserialization works correctly.
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    GZIPOutputStream zipOut = new GZIPOutputStream(out);
    zipOut.write(JSON.getBytes());//from   w  w  w  .  ja v a 2 s.  c o m
    zipOut.close();
    assertEquals(viewInfo, ViewInfo.fromCompressedJson(out.toByteArray()));
}

From source file:org.hawkular.apm.tests.dist.AbstractITest.java

public byte[] gzipCompression(byte[] bytes) throws IOException {
    if (bytes == null) {
        return null;
    }/*from   ww w  .ja v a2s.c om*/

    ByteArrayOutputStream obj = new ByteArrayOutputStream();
    GZIPOutputStream gzip = new GZIPOutputStream(obj);

    gzip.write(bytes);
    gzip.close();
    return obj.toByteArray();
}

From source file:org.forgerock.openam.cts.utils.blob.strategies.CompressionStrategy.java

/**
 * Compress the Tokens binary object./*from  w  w  w. jav a  2  s . c o m*/
 *
 * @param token Non null Token to modify.
 *
 * @throws org.forgerock.openam.cts.utils.blob.TokenStrategyFailedException {@inheritDoc}
 */
@Override
public void perform(Token token) throws TokenStrategyFailedException {
    bout.reset();
    try {
        GZIPOutputStream out = new GZIPOutputStream(bout);
        out.write(token.getBlob(), 0, token.getBlob().length);
        out.flush();
        out.close();
    } catch (IOException e) {
        throw new TokenStrategyFailedException(e);
    }
    token.setBlob(bout.toByteArray());
}

From source file:v7db.files.mongodb.MongoContentStorageTest.java

public void testReadCompressedData() throws MongoException, IOException {

    byte[] data = "some data we are going to store compressed with gzip".getBytes();
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    GZIPOutputStream gzip = new GZIPOutputStream(baos);
    gzip.write(data);//from w w  w .j av a 2  s . c  om
    gzip.close();
    byte[] compressed = baos.toByteArray();
    byte[] sha = DigestUtils.sha(data);

    prepareMockData("test.v7files.content",
            new BasicBSONObject("_id", sha).append("store", "gz").append("zin", compressed));

    Mongo mongo = getMongo();
    ContentStorage storage = new MongoContentStorage(mongo.getDB("test").getCollection("v7files.content"));

    Content check = storage.getContent(sha);

    assertEquals(new String(data), IOUtils.toString(check.getInputStream()));
    assertEquals(data.length, check.getLength());
    mongo.close();

}

From source file:com.eucalyptus.blockstorage.PutMethodWithProgress.java

@Override
protected boolean writeRequestBody(HttpState state, HttpConnection conn) throws IOException {
    InputStream inputStream;/*from ww w . ja  v  a  2s. c  o  m*/
    if (outFile != null) {
        inputStream = new FileInputStream(outFile);

        ChunkedOutputStream chunkedOut = new ChunkedOutputStream(conn.getRequestOutputStream());
        byte[] buffer = new byte[StorageProperties.TRANSFER_CHUNK_SIZE];
        int bytesRead;
        long totalBytesProcessed = 0;
        while ((bytesRead = inputStream.read(buffer)) > 0) {
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            GZIPOutputStream zip = new GZIPOutputStream(out);
            zip.write(buffer, 0, bytesRead);
            zip.close();
            chunkedOut.write(out.toByteArray());
            totalBytesProcessed += bytesRead;
            callback.update(totalBytesProcessed);
        }
        if (totalBytesProcessed > 0) {
            callback.finish();
        } else {
            callback.failed();
        }
        chunkedOut.finish();
        inputStream.close();
    } else {
        return false;
    }
    return true;
}

From source file:gov.nih.nci.firebird.service.file.FileServiceBean.java

@Override
public FirebirdFile createFile(byte[] content, FileMetadata fileMetadata) throws IOException {
    if (content.length > (long) Integer.MAX_VALUE) {
        throw new IllegalArgumentException("file too large");
    }/* ww w.j a v  a 2  s. com*/
    ByteArrayInputStream bin = new ByteArrayInputStream(content);
    ByteArrayOutputStream bout = new ByteArrayOutputStream();
    GZIPOutputStream zout = new GZIPOutputStream(bout);
    IOUtils.copy(bin, zout);
    zout.close();
    zout.finish();
    return createFirebirdFile(bout.toByteArray(), fileMetadata, content.length);
}

From source file:com.gargoylesoftware.htmlunit.util.DebuggingWebConnectionTest.java

/**
 * Ensures that Content-Encoding headers are removed when JavaScript is uncompressed.
 * (was causing java.io.IOException: Not in GZIP format as of HtmlUnit-2.10).
 * @throws Exception if the test fails/* w  w w  . j  ava  2s .  co  m*/
 */
@Test
public void gzip() throws Exception {
    final ByteArrayOutputStream baos = new ByteArrayOutputStream();
    final GZIPOutputStream gzipOutputStream = new GZIPOutputStream(baos);
    IOUtils.write("alert(1)", gzipOutputStream, "UTF-8");
    gzipOutputStream.close();

    final MockWebConnection mockConnection = new MockWebConnection();
    final List<NameValuePair> responseHeaders = Arrays.asList(new NameValuePair("Content-Encoding", "gzip"));
    mockConnection.setResponse(getDefaultUrl(), baos.toByteArray(), 200, "OK", "application/javascript",
            responseHeaders);

    final String dirName = "test-" + getClass().getSimpleName();
    try (final DebuggingWebConnection dwc = new DebuggingWebConnection(mockConnection, dirName)) {

        final WebRequest request = new WebRequest(getDefaultUrl());
        final WebResponse response = dwc.getResponse(request); // was throwing here
        assertNull(response.getResponseHeaderValue("Content-Encoding"));

        FileUtils.deleteDirectory(dwc.getReportFolder());
    }
}

From source file:edu.ucsb.eucalyptus.cloud.ws.PutMethodWithProgress.java

@Override
protected boolean writeRequestBody(HttpState state, HttpConnection conn) throws IOException {
    InputStream inputStream;//from   www  .  j a v a  2s .  com
    if (outFile != null) {
        inputStream = new FileInputStream(outFile);

        ChunkedOutputStream chunkedOut = new ChunkedOutputStream(conn.getRequestOutputStream());
        byte[] buffer = new byte[StorageProperties.TRANSFER_CHUNK_SIZE];
        int bytesRead;
        int numberProcessed = 0;
        long totalBytesProcessed = 0;
        while ((bytesRead = inputStream.read(buffer)) > 0) {
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            GZIPOutputStream zip = new GZIPOutputStream(out);
            zip.write(buffer, 0, bytesRead);
            zip.close();
            chunkedOut.write(out.toByteArray());
            totalBytesProcessed += bytesRead;
            if (++numberProcessed >= callback.getUpdateThreshold()) {
                callback.run();
                numberProcessed = 0;
            }
        }
        if (totalBytesProcessed > 0) {
            callback.finish();
        } else {
            callback.failed();
        }
        chunkedOut.finish();
        inputStream.close();
    } else {
        return false;
    }
    return true;
}

From source file:org.apache.hadoop.hbase.rest.TestGzipFilter.java

@Test
public void testGzipFilter() throws Exception {
    String path = "/" + TABLE + "/" + ROW_1 + "/" + COLUMN_1;

    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    GZIPOutputStream os = new GZIPOutputStream(bos);
    os.write(VALUE_1);//from   ww  w.  ja  va2  s  .c  om
    os.close();
    byte[] value_1_gzip = bos.toByteArray();

    // input side filter

    Header[] headers = new Header[2];
    headers[0] = new Header("Content-Type", Constants.MIMETYPE_BINARY);
    headers[1] = new Header("Content-Encoding", "gzip");
    Response response = client.put(path, headers, value_1_gzip);
    assertEquals(response.getCode(), 200);

    HTable table = new HTable(TEST_UTIL.getConfiguration(), TABLE);
    Get get = new Get(Bytes.toBytes(ROW_1));
    get.addColumn(Bytes.toBytes(CFA), Bytes.toBytes("1"));
    Result result = table.get(get);
    byte[] value = result.getValue(Bytes.toBytes(CFA), Bytes.toBytes("1"));
    assertNotNull(value);
    assertTrue(Bytes.equals(value, VALUE_1));

    // output side filter

    headers[0] = new Header("Accept", Constants.MIMETYPE_BINARY);
    headers[1] = new Header("Accept-Encoding", "gzip");
    response = client.get(path, headers);
    assertEquals(response.getCode(), 200);
    ByteArrayInputStream bis = new ByteArrayInputStream(response.getBody());
    GZIPInputStream is = new GZIPInputStream(bis);
    value = new byte[VALUE_1.length];
    is.read(value, 0, VALUE_1.length);
    assertTrue(Bytes.equals(value, VALUE_1));
    is.close();
    table.close();

    testScannerResultCodes();
}