Example usage for java.util.zip GZIPOutputStream write

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

Introduction

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

Prototype

public void write(int b) throws IOException 

Source Link

Document

Writes a byte to the compressed output stream.

Usage

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

/**
 * compress the supplied data using GZIPOutputStream
 * and return the compressed data.//from  www. jav  a  2  s.com
 * @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:org.hawkular.apm.tests.dist.AbstractITest.java

public byte[] gzipCompression(byte[] bytes) throws IOException {
    if (bytes == null) {
        return null;
    }//from  w ww.  j  a v a 2  s  . c o m

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

    gzip.write(bytes);
    gzip.close();
    return obj.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);
    gzip.close();/* ww  w  .  j  a v  a2  s  . c o  m*/
    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:de.iai.ilcd.model.common.XmlFile.java

/**
 * Compress the content of XML file prior to persist/merge events in order to save database space and be compatible
 * with MySQL server default configurations
 * as long as possible (1MB max package size)
 * /*  w  ww  .j a va 2s  .  c o  m*/
 * @throws Exception
 *             if anything goes wrong, just in-memory IO operations, should not happen
 * @see #decompressContent()
 */
@PrePersist
protected void compressContent() throws Exception {
    if (this.content == null) {
        this.compressedContent = null;
    } else {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        GZIPOutputStream gzipOut = new GZIPOutputStream(out);
        gzipOut.write(this.content.getBytes("UTF-8"));
        gzipOut.flush();
        gzipOut.close();
        this.compressedContent = out.toByteArray();
    }
}

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 w  w w .  j av a 2  s  .  c o m*/
    byte[] data = out.toByteArray();
    return data;
}

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);
    os.close();//from w w  w .j  a  v a2 s  . c o m
    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();
}

From source file:org.apache.ignite.console.agent.handlers.AbstractListener.java

/** {@inheritDoc} */
@SuppressWarnings("unchecked")
@Override/* w w w. j a  v a 2  s . co m*/
public final void call(Object... args) {
    final Ack cb = safeCallback(args);

    args = removeCallback(args);

    try {
        final Map<String, Object> params;

        if (args == null || args.length == 0)
            params = Collections.emptyMap();
        else if (args.length == 1)
            params = fromJSON(args[0], Map.class);
        else
            throw new IllegalArgumentException("Wrong arguments count, must be <= 1: " + Arrays.toString(args));

        if (pool == null)
            pool = newThreadPool();

        pool.submit(new Runnable() {
            @Override
            public void run() {
                try {
                    Object res = execute(params);

                    // TODO IGNITE-6127 Temporary solution until GZip support for socket.io-client-java.
                    // See: https://github.com/socketio/socket.io-client-java/issues/312
                    // We can GZip manually for now.
                    if (res instanceof RestResult) {
                        RestResult restRes = (RestResult) res;

                        ByteArrayOutputStream baos = new ByteArrayOutputStream(4096);
                        Base64OutputStream b64os = new Base64OutputStream(baos);
                        GZIPOutputStream gzip = new GZIPOutputStream(b64os);

                        gzip.write(restRes.getData().getBytes());

                        gzip.close();

                        restRes.zipData(baos.toString());
                    }

                    cb.call(null, toJSON(res));
                } catch (Exception e) {
                    cb.call(e, null);
                }
            }
        });
    } catch (Exception e) {
        cb.call(e, null);
    }
}

From source file:com.nextdoor.bender.ipc.es.ElasticSearchTransporterTest.java

@Test(expected = TransportException.class)
public void testGzipErrorsResponse() throws TransportException, IOException {
    byte[] respPayload = getResponse().getBytes(StandardCharsets.UTF_8);

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    GZIPOutputStream os = new GZIPOutputStream(baos);
    os.write(respPayload);
    os.close();/*from  ww w.  j a v a  2  s  . c  om*/
    byte[] compressedResponse = baos.toByteArray();

    HttpClient client = getMockClientWithResponse(compressedResponse, ContentType.DEFAULT_BINARY,
            HttpStatus.SC_OK);
    ElasticSearchTransport transport = new ElasticSearchTransport(client, true);

    try {
        transport.sendBatch("foo".getBytes());
    } catch (Exception e) {
        assertEquals("es call failed because expected failure", e.getCause().getMessage());
        throw e;
    }
}

From source file:ca.farrelltonsolar.classic.PVOutputService.java

private byte[] serializeBundle(final Bundle bundle) {
    byte[] rval = null;
    final Parcel parcel = Parcel.obtain();
    try {//from   w ww  .ja va2s. c o  m
        parcel.writeBundle(bundle);
        final ByteArrayOutputStream bos = new ByteArrayOutputStream();
        final GZIPOutputStream zos = new GZIPOutputStream(new BufferedOutputStream(bos));
        zos.write(parcel.marshall());
        zos.close();
        rval = bos.toByteArray();
    } catch (IOException ex) {
        Log.w(getClass().getName(), String.format("serializeBundle failed ex: %s", ex));

    } finally {
        parcel.recycle();
    }
    return rval;
}

From source file:com.rest4j.impl.ApiResponseImpl.java

@Override
public void outputBody(HttpServletResponse response) throws IOException {
    if (statusMessage == null)
        response.setStatus(status);//from w  ww  . j  a  va 2  s  .co m
    else
        response.setStatus(status, statusMessage);
    headers.outputHeaders(response);
    if (this.response == null)
        return;
    response.addHeader("Content-type", this.response.getContentType());
    if (addEtag) {
        String etag = this.response.getETag();
        if (etag != null)
            response.addHeader("ETag", etag);
    }

    OutputStream outputStream;
    byte[] resourceBytes = ((JSONResource) this.response).getJSONObject().toString().getBytes();
    int contentLength = resourceBytes.length;
    if (compress) {
        response.addHeader("Content-encoding", "gzip");
        ByteArrayOutputStream outputByteStream = new ByteArrayOutputStream();
        GZIPOutputStream gzipOutputStream = new GZIPOutputStream(outputByteStream);
        gzipOutputStream.write(resourceBytes);
        gzipOutputStream.finish(); //     ??!
        contentLength = outputByteStream.toByteArray().length;
        gzipOutputStream.close();
        outputByteStream.close();

        outputStream = new GZIPOutputStream(response.getOutputStream());
    } else {
        outputStream = response.getOutputStream();
    }
    response.addHeader("Content-Length", String.valueOf(contentLength));

    if (this.response instanceof JSONResource) {
        ((JSONResource) this.response).setPrettify(prettify);
    }
    if (callbackFunctionName == null) {
        this.response.write(outputStream);
    } else {
        this.response.writeJSONP(outputStream, callbackFunctionName);
    }
    outputStream.close();
}