Example usage for java.util.zip GZIPOutputStream flush

List of usage examples for java.util.zip GZIPOutputStream flush

Introduction

In this page you can find the example usage for java.util.zip GZIPOutputStream flush.

Prototype

public void flush() throws IOException 

Source Link

Document

Flushes the compressed output stream.

Usage

From source file:Main.java

static byte[] compress(byte[] decompressed) throws IOException {
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    GZIPOutputStream gzip = new GZIPOutputStream(bos);
    gzip.write(decompressed);/*from w  w  w .j  a  va  2s.  c om*/
    gzip.flush();
    gzip.close();
    bos.flush();
    bos.close();
    return bos.toByteArray();
}

From source file:Main.java

public static byte[] compressGzip(byte[] content) throws IOException {
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    GZIPOutputStream gos = new GZIPOutputStream(bos);
    try {//www  . j  a  va  2 s  .com
        gos.write(content);
        gos.flush();
    } finally {
        gos.close();
    }

    return bos.toByteArray();
}

From source file:com.giacomodrago.immediatecrypt.messagecipher.Compression.java

public static byte[] compress(byte[] plaintext) {
    try {//  w w  w .  j a  v  a  2 s. com
        ByteArrayOutputStream writer = new ByteArrayOutputStream();
        GZIPOutputStream gzipStream = new GZIPOutputStream(writer);
        gzipStream.write(plaintext);
        gzipStream.flush();
        gzipStream.close();
        writer.close();
        return writer.toByteArray();
    } catch (IOException ex) {
        throw new RuntimeException(ex);
    }
}

From source file:Main.java

public static byte[] compressInGzip(byte[] originalData, int offset, int length) throws Exception {

    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    GZIPOutputStream gzipOutStream = new GZIPOutputStream(bos);
    gzipOutStream.write(originalData, offset, length);
    gzipOutStream.finish();// w  ww.  j  a v a  2 s . c  om
    gzipOutStream.flush();
    gzipOutStream.close();
    byte[] compressData = bos.toByteArray();
    bos.close();

    return compressData;
}

From source file:org.linguafranca.pwdb.kdbx.Helpers.java

public static byte[] zipBinaryContent(byte[] value) {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    // zip up the content
    try {/*from www. j a va2 s. c  o  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: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);//from www .  j a  v a2 s. co m
    gz.finish();
    gz.flush();
    return out.toByteArray();
}

From source file:Main.java

public static void zip(InputStream is, OutputStream os) {
    GZIPOutputStream gzip = null;
    try {//from   ww w  . j  a  va  2  s  .  c o  m
        gzip = new GZIPOutputStream(os);
        byte[] buf = new byte[1024];
        int len;
        while ((len = is.read(buf)) != -1) {
            gzip.write(buf, 0, len);
            gzip.flush();
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        closeIO(is, gzip);
    }
}

From source file:Main.java

public static void zip(InputStream is, OutputStream os) {
    GZIPOutputStream gzip = null;
    try {//from   ww  w.j  ava2 s  .  com
        gzip = new GZIPOutputStream(os);
        byte[] buf = new byte[1024];
        int len;
        while ((len = is.read(buf)) != -1) {
            gzip.write(buf, 0, len);
            gzip.flush();
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        closeIO(is);
        closeIO(gzip);
    }
}

From source file:de.zib.scalaris.examples.wikipedia.data.Revision.java

/**
 * Compresses the given text and returns it as a byte array.
 * //from   w  w w  . j  a  va 2s .co m
 * @param text
 *            the un-compressed text
 * 
 * @return the compressed text
 * 
 * @throws RuntimeException
 *             if compressing the text did not work
 */
protected static byte[] packText(String text) throws RuntimeException {
    try {
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        GZIPOutputStream gos = new GZIPOutputStream(bos);
        gos.write(text.getBytes("UTF-8"));
        gos.flush();
        gos.close();
        return bos.toByteArray();
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:com.spstudio.common.image.ImageUtils.java

/**
 * byte[]//w  w w  .  j  a va2s. c  o  m
 *
 * @param ??
 * @return ??
 */
public static byte[] compress(byte[] data) {
    System.out.println("before:" + data.length);

    GZIPOutputStream gzip = null;
    ByteArrayOutputStream baos = null;
    byte[] newData = null;

    try {
        baos = new ByteArrayOutputStream();
        gzip = new GZIPOutputStream(baos);

        gzip.write(data);
        gzip.finish();
        gzip.flush();

        newData = baos.toByteArray();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            gzip.close();
            baos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    System.out.println("after:" + newData.length);
    return newData;
}