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:nl.nn.adapterframework.util.Misc.java

public static byte[] gzip(byte[] input) throws IOException {

    // Create an expandable byte array to hold the compressed data.
    // You cannot use an array that's the same size as the orginal because
    // there is no guarantee that the compressed data will be smaller than
    // the uncompressed data.
    ByteArrayOutputStream bos = new ByteArrayOutputStream(input.length);
    GZIPOutputStream gz = new GZIPOutputStream(bos);
    gz.write(input);//from w  w  w .  ja  v a2s  .c om
    gz.close();
    bos.close();

    // Get the compressed data
    return bos.toByteArray();
}

From source file:com.googlecode.jsonplugin.JSONUtil.java

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

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

    if (TextUtils.stringSet(serializationParams.getWrapSuffix()))
        stringBuilder.append(serializationParams.getWrapSuffix());

    String json = stringBuilder.toString();

    if (log.isDebugEnabled()) {
        log.debug("[JSON]" + json);
    }//from   w ww  . ja v a 2 s .  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());
            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:forge.quest.io.QuestDataIO.java

private static void savePacked(final String f, final XStream xStream, final QuestData qd) throws IOException {
    final BufferedOutputStream bout = new BufferedOutputStream(new FileOutputStream(f));
    final GZIPOutputStream zout = new GZIPOutputStream(bout);
    xStream.toXML(qd, zout);//from w w w . ja v a  2 s  .c  o  m
    zout.flush();
    zout.close();
}

From source file:org.apache.myfaces.shared.util.StateUtils.java

public static final byte[] compress(byte[] bytes) {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    try {/*w  w  w . j  a  v a2 s.com*/
        GZIPOutputStream gzip = new GZIPOutputStream(baos);
        gzip.write(bytes, 0, bytes.length);
        gzip.finish();
        byte[] fewerBytes = baos.toByteArray();
        gzip.close();
        baos.close();
        gzip = null;
        baos = null;
        return fewerBytes;
    } catch (IOException e) {
        throw new FacesException(e);
    }
}

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

/** 
* ? //from  ww  w  . j  av  a2  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:com.sxit.crawler.utils.ArchiveUtils.java

/**
 * Gzip passed bytes./*from   www.  jav  a  2 s . c  o  m*/
 * Use only when bytes is small.
 * @param bytes What to gzip.
 * @return A gzip member of bytes.
 * @throws IOException
 */
public static byte[] gzip(byte[] bytes) throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    GZIPOutputStream gzipOS = new GZIPOutputStream(baos);
    gzipOS.write(bytes, 0, bytes.length);
    gzipOS.close();
    return baos.toByteArray();
}

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

/**
 * compress the supplied data using GZIPOutputStream and return the
 * compressed data./*  w  ww  .  j  a  v  a  2s . c o  m*/
 * 
 * @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:io.restassured.internal.http.GZIPDecompressingEntityTest.java

private byte[] gzipCompress(String string) throws IOException {
    byte[] bytes = string.getBytes("UTF-8");
    ByteArrayOutputStream bos = new ByteArrayOutputStream(bytes.length);
    GZIPOutputStream gzip = new GZIPOutputStream(bos);
    gzip.write(bytes);// w  w w  . j  av a 2  s .c o m
    gzip.close();
    byte[] compressed = bos.toByteArray();
    bos.close();
    return compressed;
}

From source file:it.evilsocket.dsploit.core.System.java

public static String saveSession(String sessionName) throws IOException {
    StringBuilder builder = new StringBuilder();
    String filename = mStoragePath + '/' + sessionName + ".dss", session = null;

    builder.append(SESSION_MAGIC + "\n");

    // skip the network target
    builder.append((mTargets.size() - 1) + "\n");
    for (Target target : mTargets) {
        if (target.getType() != Target.Type.NETWORK)
            target.serialize(builder);/*ww w  .j a v  a  2 s  .  c  o m*/
    }
    builder.append(mCurrentTarget + "\n");

    session = builder.toString();

    FileOutputStream ostream = new FileOutputStream(filename);
    GZIPOutputStream gzip = new GZIPOutputStream(ostream);

    gzip.write(session.getBytes());

    gzip.close();

    mSessionName = sessionName;

    return filename;
}

From source file:it.evilsocket.dsploit.core.System.java

public static String saveHijackerSession(String sessionName, Session session) throws IOException {
    StringBuilder builder = new StringBuilder();
    String filename = mStoragePath + '/' + sessionName + ".dhs", buffer = null;

    builder.append(SESSION_MAGIC + "\n");

    builder.append((session.mUserName == null ? "null" : session.mUserName) + "\n");
    builder.append(session.mHTTPS + "\n");
    builder.append(session.mAddress + "\n");
    builder.append(session.mDomain + "\n");
    builder.append(session.mUserAgent + "\n");
    builder.append(session.mCookies.size() + "\n");
    for (BasicClientCookie cookie : session.mCookies.values()) {
        builder.append(cookie.getName() + "=" + cookie.getValue() + "; domain=" + cookie.getDomain()
                + "; path=/" + (session.mHTTPS ? ";secure" : "") + "\n");
    }/* w w w .  j a  va 2s  .c om*/

    buffer = builder.toString();

    FileOutputStream ostream = new FileOutputStream(filename);
    GZIPOutputStream gzip = new GZIPOutputStream(ostream);

    gzip.write(buffer.getBytes());

    gzip.close();

    mSessionName = sessionName;

    return filename;
}