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.broad.igv.cbio.GeneNetwork.java

public static int writeEncodedString(String string, OutputStream outputStream, boolean gzip,
        boolean base64encode) throws IOException {

    byte[] byteData;

    if (gzip) {/* w  ww.  java2  s  .co m*/
        ByteArrayOutputStreamChild gmlByteStream = new ByteArrayOutputStreamChild(string.length() / 20);
        GZIPOutputStream gzipOutputStream = new GZIPOutputStream(gmlByteStream);
        gzipOutputStream.write(string.getBytes());
        gzipOutputStream.finish();
        byteData = gmlByteStream.getBuf();
        gmlByteStream.close();
    } else {
        byteData = string.getBytes();
    }

    int count = 0;
    if (base64encode) {
        char[] gmlData = Base64Coder.encode(byteData);
        for (char c : gmlData) {
            outputStream.write(c);
            count++;
        }
    } else {
        outputStream.write(byteData);
        outputStream.flush();
        count += byteData.length;
    }
    outputStream.flush();
    return count;
}

From source file:org.ofbiz.webapp.event.RestEventHandler.java

/** 
* ? /*from w w w. j  a v a 2  s .c o  m*/
*  
* @param is 
* @param os 
* @throws Exception 
*/
public static void compress(InputStream is, OutputStream os) throws Exception {

    GZIPOutputStream gos = new GZIPOutputStream(os);

    int count;
    byte data[] = new byte[BUFFER];
    while ((count = is.read(data, 0, BUFFER)) != -1) {
        gos.write(data, 0, count);
    }

    gos.finish();

    gos.flush();
    gos.close();
}

From source file:gov.nih.nci.caarray.util.CaArrayUtils.java

/**
 * Serializes the given object (zipped) to a byte array.
 * //from   w  ww  .  ja v a  2s . c o m
 * @param serializable object to serialize
 * @return the serialized object as a byte array.
 */
public static byte[] serialize(Serializable serializable) {
    final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    GZIPOutputStream gZipOutputStream = null;
    ObjectOutputStream objectOutputStream = null;
    try {
        gZipOutputStream = new GZIPOutputStream(byteArrayOutputStream);
        objectOutputStream = new ObjectOutputStream(gZipOutputStream);
        objectOutputStream.writeObject(serializable);
        objectOutputStream.flush();
        gZipOutputStream.finish();
        gZipOutputStream.flush();
        byteArrayOutputStream.flush();
    } catch (final IOException e) {
        throw new IllegalStateException("Couldn't serialize object", e);
    } finally {
        IOUtils.closeQuietly(objectOutputStream);
        IOUtils.closeQuietly(gZipOutputStream);
        IOUtils.closeQuietly(byteArrayOutputStream);
    }
    return byteArrayOutputStream.toByteArray();
}

From source file:com.wxxr.nirvana.json.JSONUtil.java

public static void writeJSONToResponse(SerializationParams serializationParams) throws IOException {
    StringBuilder stringBuilder = new StringBuilder();
    if (StringUtils.isNotBlank(serializationParams.getSerializedJSON()))
        stringBuilder.append(serializationParams.getSerializedJSON());

    if (StringUtils.isNotBlank(serializationParams.getWrapPrefix()))
        stringBuilder.insert(0, serializationParams.getWrapPrefix());
    else if (serializationParams.isWrapWithComments()) {
        stringBuilder.insert(0, "/* ");
        stringBuilder.append(" */");
    } else if (serializationParams.isPrefix())
        stringBuilder.insert(0, "{}&& ");

    if (StringUtils.isNotBlank(serializationParams.getWrapSuffix()))
        stringBuilder.append(serializationParams.getWrapSuffix());

    String json = stringBuilder.toString();

    if (LOG.isDebugEnabled()) {
        LOG.debug("[JSON]" + json);
    }//w ww .  ja v a2  s. co  m

    HttpServletResponse response = serializationParams.getResponse();

    // status or error code
    if (serializationParams.getStatusCode() > 0)
        response.setStatus(serializationParams.getStatusCode());
    else if (serializationParams.getErrorCode() > 0)
        response.sendError(serializationParams.getErrorCode());

    // content type
    response.setContentType(
            serializationParams.getContentType() + ";charset=" + serializationParams.getEncoding());

    if (serializationParams.isNoCache()) {
        response.setHeader("Cache-Control", "no-cache");
        response.setHeader("Expires", "0");
        response.setHeader("Pragma", "No-cache");
    }

    if (serializationParams.isGzip()) {
        response.addHeader("Content-Encoding", "gzip");
        GZIPOutputStream out = null;
        InputStream in = null;
        try {
            out = new GZIPOutputStream(response.getOutputStream());
            in = new ByteArrayInputStream(json.getBytes(serializationParams.getEncoding()));
            byte[] buf = new byte[1024];
            int len;
            while ((len = in.read(buf)) > 0) {
                out.write(buf, 0, len);
            }
        } finally {
            if (in != null)
                in.close();
            if (out != null) {
                out.finish();
                out.close();
            }
        }

    } else {
        response.setContentLength(json.getBytes(serializationParams.getEncoding()).length);
        PrintWriter out = response.getWriter();
        out.print(json);
    }
}

From source file:com.struts2ext.json.JSONUtil.java

public static void writeJSONToResponse(SerializationParams serializationParams) throws IOException {
    StringBuilder stringBuilder = new StringBuilder();
    if (StringUtils.isNotBlank(serializationParams.getSerializedJSON()))
        stringBuilder.append(serializationParams.getSerializedJSON());

    if (StringUtils.isNotBlank(serializationParams.getWrapPrefix()))
        stringBuilder.insert(0, serializationParams.getWrapPrefix());
    else if (serializationParams.isWrapWithComments()) {
        stringBuilder.insert(0, "/* ");
        stringBuilder.append(" */");
    } else if (serializationParams.isPrefix())
        stringBuilder.insert(0, "{}&& ");

    if (StringUtils.isNotBlank(serializationParams.getWrapSuffix()))
        stringBuilder.append(serializationParams.getWrapSuffix());

    String json = stringBuilder.toString();

    if (LOG.isDebugEnabled()) {
        LOG.debug("[JSON]" + json);
    }/*from  w  w w  . jav a 2s . c o  m*/

    HttpServletResponse response = serializationParams.getResponse();

    // status or error code
    if (serializationParams.getStatusCode() > 0)
        response.setStatus(serializationParams.getStatusCode());
    else if (serializationParams.getErrorCode() > 0)
        response.sendError(serializationParams.getErrorCode());

    // content type
    if (serializationParams.isSmd())
        response.setContentType("application/json-rpc;charset=" + serializationParams.getEncoding());
    else
        response.setContentType(
                serializationParams.getContentType() + ";charset=" + serializationParams.getEncoding());

    if (serializationParams.isNoCache()) {
        response.setHeader("Cache-Control", "no-cache");
        response.setHeader("Expires", "0");
        response.setHeader("Pragma", "No-cache");
    }

    if (serializationParams.isGzip()) {
        response.addHeader("Content-Encoding", "gzip");
        GZIPOutputStream out = null;
        InputStream in = null;
        try {
            out = new GZIPOutputStream(response.getOutputStream());
            in = new ByteArrayInputStream(json.getBytes(serializationParams.getEncoding()));
            byte[] buf = new byte[1024];
            int len;
            while ((len = in.read(buf)) > 0) {
                out.write(buf, 0, len);
            }
        } finally {
            if (in != null)
                in.close();
            if (out != null) {
                out.finish();
                out.close();
            }
        }

    } else {
        response.setContentLength(json.getBytes(serializationParams.getEncoding()).length);
        PrintWriter out = response.getWriter();
        out.print(json);
    }
}

From source file:org.makersoft.mvc.json.JSONUtil.java

public static void writeJSONToResponse(SerializationParams serializationParams) throws IOException {
    StringBuilder stringBuilder = new StringBuilder();
    if (StringUtils.isNotBlank(serializationParams.getSerializedJSON()))
        stringBuilder.append(serializationParams.getSerializedJSON());

    if (StringUtils.isNotBlank(serializationParams.getWrapPrefix()))
        stringBuilder.insert(0, serializationParams.getWrapPrefix());
    else if (serializationParams.isWrapWithComments()) {
        stringBuilder.insert(0, "/* ");
        stringBuilder.append(" */");
    } else if (serializationParams.isPrefix())
        stringBuilder.insert(0, "{}&& ");

    if (StringUtils.isNotBlank(serializationParams.getWrapSuffix()))
        stringBuilder.append(serializationParams.getWrapSuffix());

    String json = stringBuilder.toString();

    if (LOG.isDebugEnabled()) {
        LOG.debug("[JSON]" + json);
    }//  w ww  .  j  a v  a  2s  .c om

    HttpServletResponse response = serializationParams.getResponse();

    // status or error code
    if (serializationParams.getStatusCode() > 0)
        response.setStatus(serializationParams.getStatusCode());
    else if (serializationParams.getErrorCode() > 0)
        response.sendError(serializationParams.getErrorCode());

    // content type
    //        response.setContentType(serializationParams.getContentType() + ";charset="
    //                + serializationParams.getEncoding());

    // character encoding  
    response.setCharacterEncoding(serializationParams.getEncoding());
    // content type
    response.setContentType(serializationParams.getContentType());

    if (serializationParams.isNoCache()) {
        response.setHeader("Cache-Control", "no-cache");
        response.setHeader("Expires", "0");
        response.setHeader("Pragma", "No-cache");
    }

    if (serializationParams.isGzip()) {
        response.addHeader("Content-Encoding", "gzip");
        GZIPOutputStream out = null;
        InputStream in = null;
        try {
            out = new GZIPOutputStream(response.getOutputStream());
            in = new ByteArrayInputStream(json.getBytes(serializationParams.getEncoding()));
            byte[] buf = new byte[1024];
            int len;
            while ((len = in.read(buf)) > 0) {
                out.write(buf, 0, len);
            }
        } finally {
            if (in != null)
                in.close();
            if (out != null) {
                out.finish();
                out.close();
            }
        }

    } else {
        response.setContentLength(json.getBytes(serializationParams.getEncoding()).length);
        PrintWriter out = response.getWriter();
        out.print(json);
    }
}

From source file:org.jboss.capedwarf.connect.io.GzipContentProducer.java

public void writeTo(OutputStream outstream) throws IOException {
    if (GzipOptionalSerializator.isGzipEnabled()) {
        GZIPOutputStream gzip = new GZIPOutputStream(outstream);
        doWriteTo(gzip);//from ww w.  j  a va2s  .c  o  m
        gzip.finish();
    } else {
        doWriteTo(outstream);
    }
}

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

/**
 *
 * @param text/*from ww w. j  a  v a2s. c om*/
 *            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:org.apache.wink.itest.contentencoding.WinkApacheContentEncodingTest.java

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

    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").accept(MediaType.TEXT_PLAIN)
            .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:com.ltasks.GZipPostMethod.java

@Override
protected RequestEntity generateRequestEntity() {
    if (mIsGzip) {
        try {/*from   w  w w . j a v  a2  s.com*/
            String contentStr = EncodingUtil.formUrlEncode(getParameters(), getRequestCharSet());

            ByteArrayOutputStream originalContent = new ByteArrayOutputStream();
            originalContent.write(EncodingUtil.getAsciiBytes(contentStr));

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

            originalContent.writeTo(gzipOut);

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

            ByteArrayRequestEntity entity = new ByteArrayRequestEntity(content, FORM_URL_ENCODED_CONTENT_TYPE);
            return entity;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    } else {
        return super.generateRequestEntity();
    }
}