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:Main.java

static public byte[] gzip(byte src[], byte default_value[]) {
    try {/* w  ww.  ja v  a 2  s  . c om*/
        if (src == null)
            return default_value;

        ByteArrayOutputStream out_raw = new ByteArrayOutputStream();
        GZIPOutputStream out = new GZIPOutputStream(out_raw);

        ByteArrayInputStream in = new ByteArrayInputStream(src);

        IOUtils.copy(in, out);

        in.close();
        out.close();

        return out_raw.toByteArray();
    } catch (Exception e) {
        return default_value;
    }
}

From source file:Main.java

public static final byte[] compress(byte[] bytes) {
    if (bytes == null) {
        throw new NullPointerException("byte[] is NULL !");
    }//  ww  w.  j ava 2 s.c  o m
    ByteArrayOutputStream bos = new ByteArrayOutputStream(8192);
    try {
        GZIPOutputStream gzip = new GZIPOutputStream(bos);
        gzip.write(bytes, 0, bytes.length);
        gzip.finish();
        byte[] fewerBytes = bos.toByteArray();
        gzip.close();
        bos.close();
        gzip = null;
        bos = null;
        return fewerBytes;
    } catch (IOException e) {
        return null;
    }
}

From source file:net.orpiske.tcs.utils.compression.Compressor.java

/**
 * Compress a string in GZIP format//from w  w w.  j av a2s . c  o m
 * @param text the string to compress
 * @return An array of compressed bytes
 * @throws IOException if unable to compress it
 */
public static byte[] compress(final String text) throws IOException {
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    GZIPOutputStream gzipOutputStream = null;

    try {
        gzipOutputStream = new GZIPOutputStream(outputStream);
        gzipOutputStream.write(text.getBytes());
        gzipOutputStream.close();

        return outputStream.toByteArray();
    } finally {
        IOUtils.closeQuietly(gzipOutputStream);
        IOUtils.closeQuietly(outputStream);
    }
}

From source file:pro.foundev.GzipCompressionOfColumnExample.java

private static ByteBuffer toByteBuffer(String obj) {
    if (obj == null) {
        return null;
    }//from w  w  w. j  a  v a  2s . c  om
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    GZIPOutputStream gzip;
    try {
        gzip = new GZIPOutputStream(out);
        gzip.write(StringUtils.getBytesUtf8(obj));
        gzip.close();
        return ByteBuffer.wrap(out.toByteArray());
    } catch (IOException e) {
        throw new RuntimeException("Error compressing column data", e);
    }
}

From source file:fr.dutra.confluence2wordpress.util.CodecUtils.java

public static String compressAndEncode(String text) throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    GZIPOutputStream gzos = new GZIPOutputStream(new Base64OutputStream(baos));
    try {//from   w ww  .ja va  2  s.  c  om
        gzos.write(text.getBytes(UTF_8));
    } finally {
        baos.close();
        gzos.close();
    }
    return new String(baos.toByteArray(), UTF_8);
}

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//from   ww  w  .  j  a  v a  2 s. c  o m
 * @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:org.linguafranca.pwdb.kdbx.Helpers.java

public static byte[] zipBinaryContent(byte[] value) {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    // zip up the content
    try {//w  ww .ja  v a2s  .co m
        GZIPOutputStream g = new GZIPOutputStream(baos);
        g.write(value, 0, value.length);
        g.flush();
        g.close();
    } catch (IOException e) {
        throw new IllegalStateException(e);
    }
    return baos.toByteArray();
}

From source file:Compress.java

public static void gzipFile(String from, String to) throws IOException {
    FileInputStream in = new FileInputStream(from);
    GZIPOutputStream out = new GZIPOutputStream(new FileOutputStream(to));
    byte[] buffer = new byte[4096];
    int bytesRead;
    while ((bytesRead = in.read(buffer)) != -1)
        out.write(buffer, 0, bytesRead);
    in.close();/*from w ww. j  a v  a 2s  .  c om*/
    out.close();
}

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

/**
 * Compress the payload/*  w w  w. j  ava  2 s .  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: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 .  ja  v  a2 s. c om*/
 */
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();
}