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:ee.ria.xroad.opmonitordaemon.QueryRequestHandler.java

static byte[] compress(String data) throws IOException {
    byte[] dataBytes = data.getBytes(StandardCharsets.UTF_8);

    try (ByteArrayOutputStream bos = new ByteArrayOutputStream(dataBytes.length)) {
        GZIPOutputStream gzip = new GZIPOutputStream(bos);
        gzip.write(dataBytes);
        gzip.close();/*from  w w w . j  av  a  2 s  .c o  m*/

        return bos.toByteArray();
    }
}

From source file:com.wbtech.dao.NetworkUitlity.java

/**
 * ?//from  w  w  w .j a  v a 2s. c  o m
 * @param str
 * @return
 */
public static byte[] compressToByte(String str) {
    if (str == null || str.length() == 0) {
        return null;
    }
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    GZIPOutputStream gzip;
    try {
        gzip = new GZIPOutputStream(out);
        gzip.write(str.getBytes("utf-8"));
        gzip.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return out.toByteArray();
}

From source file:it.acubelab.smaph.SmaphUtils.java

/**
 * Compress a string with GZip.// w ww .j a v  a2  s.c  o  m
 * 
 * @param str
 *            the string.
 * @return the compressed string.
 * @throws IOException
 *             if something went wrong during compression.
 */
public static byte[] compress(String str) throws IOException {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    GZIPOutputStream gzip = new GZIPOutputStream(out);
    gzip.write(str.getBytes());
    gzip.close();
    return out.toByteArray();
}

From source file:com.taobao.tair.etc.TranscoderUtil.java

public static byte[] compress(byte[] in) {
    if (in == null) {
        throw new NullPointerException("Can't compress null");
    }/*w  ww. j a va  2  s. c o m*/

    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    GZIPOutputStream gz = null;

    try {
        gz = new GZIPOutputStream(bos);
        gz.write(in);
    } catch (IOException e) {
        throw new RuntimeException("IO exception compressing data", e);
    } finally {
        try {
            gz.close();
            bos.close();
        } catch (Exception e) {
            // should not happen
        }
    }

    byte[] rv = bos.toByteArray();

    if (log.isInfoEnabled()) {
        log.info("compressed value, size from [" + in.length + "] to [" + rv.length + "]");
    }

    return rv;
}

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

/**
 * Compresses the given text and returns it as a byte array.
 * //from ww w .  j a  v  a 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:org.dd4t.core.util.CompressionUtils.java

/**
 * Compresses a given content to a GZipped byte array.
 *
 * @param content the content to encode/*ww  w  .  j a v a  2s  .co  m*/
 * @return byte[] representing the compressed content bytes
 * @throws SerializationException if something goes wrong with the streams
 */
public static byte[] compressGZip(String content) throws SerializationException {
    ByteArrayOutputStream baos = null;
    GZIPOutputStream gos = null;

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

        gos.write(content.getBytes("UTF-8"));
        gos.close();

        return baos.toByteArray();
    } catch (IOException ioe) {
        LOG.error("String compression failed.", ioe);
        throw new SerializationException("Failed to compress String", ioe);
    } finally {
        IOUtils.closeQuietly(gos);
        IOUtils.closeQuietly(baos);
    }
}

From source file:org.apache.juneau.rest.test.GzipTest.java

private static InputStream compress(String contents) throws Exception {
    ByteArrayOutputStream baos = new ByteArrayOutputStream(contents.length() >> 1);
    GZIPOutputStream gos = new GZIPOutputStream(baos);
    gos.write(contents.getBytes());
    gos.finish();/*from   w w w . j  a va2 s.  com*/
    gos.close();
    return new ByteArrayInputStream(baos.toByteArray());
}

From source file:com.facebook.presto.bloomfilter.BloomFilter.java

public static byte[] compress(byte[] b) throws IOException {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    GZIPOutputStream gzip = new GZIPOutputStream(out);
    gzip.write(b);
    gzip.close();/*from   w w w  .j a  v  a 2 s .co  m*/
    return out.toByteArray();
}

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

/**
 * byte[]/*  w w  w  .jav  a  2 s. 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;
}

From source file:VASSAL.tools.image.tilecache.TileUtils.java

/**
 * Write a tile image to a stream.//from  ww w . jav a 2  s.  com
 *
 * @param tile the image
 * @param out the stream
 *
 * @throws ImageIOException if the write fails
 */
public static void write(BufferedImage tile, OutputStream out) throws IOException {
    ByteBuffer bb;

    // write the header
    bb = ByteBuffer.allocate(18);

    bb.put("VASSAL".getBytes()).putInt(tile.getWidth()).putInt(tile.getHeight()).putInt(tile.getType());

    out.write(bb.array());

    // write the tile data
    final DataBufferInt db = (DataBufferInt) tile.getRaster().getDataBuffer();
    final int[] data = db.getData();

    bb = ByteBuffer.allocate(4 * data.length);
    bb.asIntBuffer().put(data);

    final GZIPOutputStream zout = new GZIPOutputStream(out);
    zout.write(bb.array());
    zout.finish();
}