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.eviware.soapui.impl.wsdl.support.CompressionSupport.java

private static byte[] GZIPCompress(byte[] requestContent) throws IOException {
    ByteArrayOutputStream compressedContent = new ByteArrayOutputStream();
    GZIPOutputStream gzipstream = new GZIPOutputStream(compressedContent);
    gzipstream.write(requestContent);
    gzipstream.finish();//from w  w  w  .j av  a2  s  .co m

    // get the compressed content
    return compressedContent.toByteArray();
}

From source file:Main.java

public static byte[] compress(byte[] bytes) throws IOException {
    byte[] result = null;
    ByteArrayOutputStream out = null;
    GZIPOutputStream gzip = null;
    try {/*from   w w w. j a va2s .  c o m*/
        out = new ByteArrayOutputStream(bytes.length);
        gzip = new GZIPOutputStream(out);
        gzip.write(bytes);
        gzip.finish();
        close(gzip);
        result = out.toByteArray();
    } catch (IOException e) {
        throw e;
    } finally {
        close(gzip);
        close(out);
    }
    return result;
}

From source file:net.sf.ehcache.distribution.PayloadUtil.java

/**
 * Gzips a byte[]. For text, approximately 10:1 compression is achieved.
 * @param ungzipped the bytes to be gzipped
 * @return gzipped bytes//  w  ww  .  j a  va 2  s  .com
 */
public static byte[] gzip(byte[] ungzipped) {
    final ByteArrayOutputStream bytes = new ByteArrayOutputStream();
    try {
        final GZIPOutputStream gzipOutputStream = new GZIPOutputStream(bytes);
        gzipOutputStream.write(ungzipped);
        gzipOutputStream.close();
    } catch (IOException e) {
        LOG.fatal("Could not gzip " + ungzipped);
    }
    return bytes.toByteArray();
}

From source file:com.googlesource.gerrit.plugins.github.velocity.VelocityStaticServlet.java

private static byte[] compress(final byte[] raw) throws IOException {
    final ByteArrayOutputStream out = new ByteArrayOutputStream();
    final GZIPOutputStream gz = new GZIPOutputStream(out);
    gz.write(raw);
    gz.finish();/*from   w w  w  .j a  v a 2s . co m*/
    gz.flush();
    return out.toByteArray();
}

From source file:com.netflix.dyno.connectionpool.impl.utils.ZipUtils.java

public static byte[] compressString(String value) throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream(value.length());
    GZIPOutputStream gos = new GZIPOutputStream(baos);
    gos.write(Base64.encode(value.getBytes(StandardCharsets.UTF_8)));
    gos.close();/*from  ww w.ja va  2 s  .c o m*/
    byte[] compressed = baos.toByteArray();
    baos.close();
    return compressed;
}

From source file:com.netflix.dyno.connectionpool.impl.utils.ZipUtils.java

/**
 * Encodes the given byte array and then GZIP compresses it.
 *
 * @param value byte array input/* ww w.j a v a 2  s.  c om*/
 * @return compressed byte array output
 * @throws IOException
 */
public static byte[] compressBytesNonBase64(byte[] value) throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream(value.length);
    GZIPOutputStream gos = new GZIPOutputStream(baos);
    gos.write(value);
    gos.close();
    byte[] compressed = baos.toByteArray();
    baos.close();
    return compressed;
}

From source file:Main.java

public static byte[] compress(byte[] src) throws IOException {

    GZIPOutputStream gzip = null;
    ByteArrayOutputStream baos = null;
    try {//w  ww  .  ja  va2  s  .c om
        baos = new ByteArrayOutputStream();

        gzip = new GZIPOutputStream(baos);
        gzip.write(src);
        gzip.finish();

        return baos.toByteArray();

    } finally {
        if (gzip != null) {
            gzip.close();
        }
        if (baos != null) {
            baos.close();
        }
    }
}

From source file:org.wso2.carbon.das.messageflow.data.publisher.publish.StatisticsPublisher.java

/**
 * Compress the payload// www . ja  v  a2s.  com
 *
 * @param str
 * @return
 */
private static String compress(byte[] str) {
    if (str == null || str.length == 0) {
        return null;
    }

    ByteArrayOutputStream out = new ByteArrayOutputStream();
    try {
        GZIPOutputStream gzip = new GZIPOutputStream(out);
        gzip.write(str);
        gzip.close();
        return DatatypeConverter.printBase64Binary(out.toByteArray());
    } catch (IOException e) {
        log.error("Unable to compress data", e);
    }

    return null;
}

From source file:GZIPUtils.java

/**
 * Returns an gzipped copy of the input array.
 *//*  w  w  w  .ja  v a 2 s  . co m*/
public static final byte[] zip(byte[] in) {
    try {
        // compress using GZIPOutputStream 
        ByteArrayOutputStream byteOut = new ByteArrayOutputStream(in.length / EXPECTED_COMPRESSION_RATIO);

        GZIPOutputStream outStream = new GZIPOutputStream(byteOut);

        try {
            outStream.write(in);
        } catch (Exception e) {

        }

        try {
            outStream.close();
        } catch (IOException e) {

        }

        return byteOut.toByteArray();

    } catch (IOException e) {

        return null;
    }
}

From source file:com.iflytek.spider.util.GZIPUtils.java

/**
 * Returns an gzipped copy of the input array.
 *//*w w  w .ja  v a  2s .  c  o m*/
public static final byte[] zip(byte[] in) {
    try {
        // compress using GZIPOutputStream 
        ByteArrayOutputStream byteOut = new ByteArrayOutputStream(in.length / EXPECTED_COMPRESSION_RATIO);

        GZIPOutputStream outStream = new GZIPOutputStream(byteOut);

        try {
            outStream.write(in);
        } catch (Exception e) {
            e.printStackTrace(LogUtil.getWarnStream(LOG));
        }

        try {
            outStream.close();
        } catch (IOException e) {
            e.printStackTrace(LogUtil.getWarnStream(LOG));
        }

        return byteOut.toByteArray();

    } catch (IOException e) {
        e.printStackTrace(LogUtil.getWarnStream(LOG));
        return null;
    }
}