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:net.yacy.cora.protocol.http.GzipCompressingEntity.java

@Override
public void writeTo(final OutputStream outstream) throws IOException {
    if (outstream == null) {
        throw new IllegalArgumentException("Output stream may not be null");
    }/*from   w w  w  . j  a v a  2 s  .c o  m*/
    GZIPOutputStream gzip = new GZIPOutputStream(outstream, 65536) {
        {
            def.setLevel(Deflater.BEST_SPEED);
        }
    };
    wrappedEntity.writeTo(gzip);
    gzip.finish();
}

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");
    }//w ww.ja v a 2 s  .c o m
    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:org.apache.wink.itest.contentencoding.ContentEncodingTest.java

/**
 * Tests sending in small bits of gzip encoded content.
 * /*from  w  w w  . j ava 2s. c o m*/
 * @throws HttpException
 * @throws IOException
 */
public void testSendLargeGzipContentEncoded() throws HttpException, IOException {
    PostMethod postMethod = new PostMethod(BASE_URI + "/bigbook/mirror");
    postMethod.setContentChunked(true);
    postMethod.addRequestHeader("Accept", "text/plain");
    postMethod.setRequestHeader("Content-Encoding", "gzip");
    // postMethod.setRequestHeader("Content-)
    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();

    postMethod.setRequestEntity(new ByteArrayRequestEntity(content, "text/plain; charset=utf-8"));
    try {
        int result = client.executeMethod(postMethod);
        assertEquals(200, result);
        InputStream responseStream = postMethod.getResponseBodyAsStream();
        for (int c = 0; c < 5000000; ++c) {
            assertEquals(c % 256, responseStream.read());
        }
    } finally {
        postMethod.releaseConnection();
    }
}

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

/**
 * Tests sending in small bits of gzip encoded content.
 * /*from   w  ww  .  j a  v  a2  s  . com*/
 * @throws HttpException
 * @throws IOException
 */
public void testSendLargeGzipContentEncodedAndReceiveContentEncoded() throws HttpException, IOException {
    PostMethod postMethod = new PostMethod(BASE_URI + "/bigbook/mirror");
    postMethod.setContentChunked(true);
    postMethod.setRequestHeader("Accept", "text/plain");
    postMethod.setRequestHeader("Accept-Encoding", "gzip");
    postMethod.setRequestHeader("Content-Encoding", "gzip");
    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();

    postMethod.setRequestEntity(new ByteArrayRequestEntity(content, "text/plain; charset=utf-8"));
    try {
        int result = client.executeMethod(postMethod);
        assertEquals(200, result);
        InputStream responseStream = new GZIPInputStream(postMethod.getResponseBodyAsStream());
        for (int c = 0; c < 5000000; ++c) {
            assertEquals(c % 256, responseStream.read());
        }
    } finally {
        postMethod.releaseConnection();
    }
}

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

/**
 * Tests sending in small bits of gzip encoded content.
 * //from   ww  w .java 2  s  .  com
 * @throws HttpException
 * @throws IOException
 */
public void testSendSmallGzipContentEncoded() throws HttpException, IOException {
    PostMethod postMethod = new PostMethod(BASE_URI + "/bigbook");

    postMethod.addRequestHeader("Accept", "text/plain");
    postMethod.setRequestHeader("Content-Encoding", "gzip");
    postMethod.setRequestHeader("Transfer-Encoding", "chunked");
    // postMethod.setRequestHeader("Content-)
    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();

    postMethod.setRequestEntity(new ByteArrayRequestEntity(content, "text/plain; charset=utf-8"));
    try {
        int result = client.executeMethod(postMethod);
        assertEquals(200, result);
        String response = postMethod.getResponseBodyAsString();
        assertEquals("Hello world" + "helloworld", response);
    } finally {
        postMethod.releaseConnection();
    }
}

From source file:com.epam.wilma.compression.gzip.GzipCompressionService.java

/**
 * Compresses an {@link InputStream} object into gzip.
 * @param inputStream the inputstream that will be compressed
 * @return a {@link ByteArrayOutputStream} containing gzipped byte array
 *//*from   ww w  . j  av a2s . c o  m*/
@Override
public ByteArrayOutputStream compress(final InputStream inputStream) {
    InputStream source = inputStream;
    ByteArrayOutputStream baos = outputStreamFactory.createByteArrayOutputStream();
    try {
        GZIPOutputStream gout = gzipOutpuStreamFactory.createOutputStream(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("Could not gzip message body!", e);
    }
    return baos;
}

From source file:org.apache.xmlrpc.CommonsXmlRpcTransport.java

public InputStream sendXmlRpc(byte[] request) throws IOException, XmlRpcClientException {
    method = new PostMethod(url.toString());
    method.setHttp11(http11);//from  w  ww. j  a va  2  s  .  co  m
    method.setRequestHeader(new Header("Content-Type", "text/xml"));

    if (rgzip)
        method.setRequestHeader(new Header("Content-Encoding", "gzip"));

    if (gzip)
        method.setRequestHeader(new Header("Accept-Encoding", "gzip"));

    method.setRequestHeader(userAgentHeader);

    if (rgzip) {
        ByteArrayOutputStream lBo = new ByteArrayOutputStream();
        GZIPOutputStream lGzo = new GZIPOutputStream(lBo);
        lGzo.write(request);
        lGzo.finish();
        lGzo.close();
        byte[] lArray = lBo.toByteArray();
        method.setRequestBody(new ByteArrayInputStream(lArray));
        method.setRequestContentLength(-1);
    } else
        method.setRequestBody(new ByteArrayInputStream(request));

    URI hostURI = new URI(url.toString());
    HostConfiguration hostConfig = new HostConfiguration();
    hostConfig.setHost(hostURI);
    client.executeMethod(hostConfig, method);

    boolean lgzipo = false;

    Header lHeader = method.getResponseHeader("Content-Encoding");
    if (lHeader != null) {
        String lValue = lHeader.getValue();
        if (lValue != null)
            lgzipo = (lValue.indexOf("gzip") >= 0);
    }

    if (lgzipo)
        return (new GZIPInputStream(method.getResponseBodyAsStream()));
    else
        return method.getResponseBodyAsStream();
}

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

@Override
public void outputBody(HttpServletResponse response) throws IOException {
    if (statusMessage == null)
        response.setStatus(status);//from  ww w. j a  v  a 2  s.  c  o 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();
}

From source file:com.epam.wilma.test.client.HttpRequestSender.java

private InputStream encode(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);//w  ww.j a v  a2  s. c  o  m
    gout.finish();
    //Convert to InputStream.
    return new ByteArrayInputStream(baos.toByteArray());
}

From source file:io.aino.agents.core.Sender.java

private byte[] getRequestContent() {
    if (!agentConfig.isGzipEnabled()) {
        return stringToSend.getBytes();
    }//from   ww  w  .ja  v a  2s. c o  m

    try {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        GZIPOutputStream gzipStream = new GZIPOutputStream(baos);
        gzipStream.write(stringToSend.getBytes());

        gzipStream.finish();
        baos.flush();
        byte[] returnBytes = baos.toByteArray();
        baos.close();
        gzipStream.close();

        return returnBytes;
    } catch (IOException e) {
        throw new AgentCoreException("Failed to compress Aino log message using gzip.");
    }
}