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:org.projectforge.common.GZIPHelper.java

/**
 * @param str/*  ww  w .  j  a va 2 s . c  o m*/
 * @return Base64 encoded byte array.
 */
public static String compress(final String str) {
    if (str == null || str.length() == 0) {
        return str;
    }
    final ByteArrayOutputStream out = new ByteArrayOutputStream();
    try {
        final GZIPOutputStream gzip = new GZIPOutputStream(out);
        gzip.write(str.getBytes());
        gzip.close();
        return Base64Helper.encodeObject(out.toByteArray());
    } catch (final IOException ex) {
        log.error("Error while compressing string: " + ex.getMessage(), ex);
        return null;
    }
}

From source file:Main.java

static byte[] getCompressedData(byte[] data) {
    ByteArrayOutputStream ser = new ByteArrayOutputStream();
    GZIPOutputStream gs;
    try {//from   w  ww  .j a v a  2 s.  com
        gs = new GZIPOutputStream(ser);
        gs.write(data);
        gs.close();
    } catch (IOException e) {
        throw new RuntimeException("Cannot compress data", e);
    }

    byte[] compressed = ser.toByteArray();
    return compressed;
}

From source file:com.eviware.soapui.impl.wsdl.support.CompressedStringSupport.java

public static void setString(CompressedStringConfig compressedStringConfig, String value) {
    synchronized (compressedStringConfig) {
        long limit = SoapUI.getSettings().getLong(WsdlSettings.COMPRESSION_LIMIT, 0);
        if (limit > 0 && value.length() >= limit) {
            try {
                ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
                GZIPOutputStream out = new GZIPOutputStream(byteOut);
                out.write(value.getBytes());
                out.finish();/* www.  j  a v  a  2 s  .c o m*/
                value = new String(Base64.encodeBase64(byteOut.toByteArray()));
                compressedStringConfig.setCompression("gzip");
            } catch (IOException e) {
                SoapUI.logError(e);
                compressedStringConfig.unsetCompression();
            }
        } else if (compressedStringConfig.isSetCompression()) {
            compressedStringConfig.unsetCompression();
        }

        compressedStringConfig.setStringValue(value);
    }
}

From source file:Main.java

public static byte[] compressStringToByteArray(String uncompressedString) {
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    GZIPOutputStream gzipOutputStream;
    try {/*from  w ww . j  a  v  a 2s .c  o  m*/
        gzipOutputStream = new GZIPOutputStream(byteArrayOutputStream);
        gzipOutputStream.write(uncompressedString.getBytes("UTF-8"));
        gzipOutputStream.close();
    } catch (IOException e) {
        e.printStackTrace();
    }

    return byteArrayOutputStream.toByteArray();
}

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 w  w . j a va2s  . c om*/
        gzos.write(text.getBytes(UTF_8));
    } finally {
        baos.close();
        gzos.close();
    }
    return new String(baos.toByteArray(), UTF_8);
}

From source file:org.geppetto.frontend.messaging.CompressionUtils.java

public static byte[] gzipCompress(String message) throws IOException {

    long startTime = System.currentTimeMillis();

    ByteArrayOutputStream compressedMessageStream = new ByteArrayOutputStream();
    GZIPOutputStream gzipOutputStream = new GZIPOutputStream(compressedMessageStream);

    gzipOutputStream.write(message.getBytes());
    gzipOutputStream.close();/*from w  w w.  j  a v  a  2  s  .c o m*/

    byte[] compressedMessage = compressedMessageStream.toByteArray();
    compressedMessageStream.close();

    long elapsedTime = System.currentTimeMillis() - startTime;

    logger.info(String.format("Compressed message from %d to %d bytes in %dms", message.length(),
            compressedMessage.length, elapsedTime));

    return compressedMessage;
}

From source file:org.geppetto.frontend.messaging.CompressionUtils.java

public static byte[] gzipCompress(byte[] message) throws IOException {

    long startTime = System.currentTimeMillis();

    ByteArrayOutputStream compressedMessageStream = new ByteArrayOutputStream();
    GZIPOutputStream gzipOutputStream = new GZIPOutputStream(compressedMessageStream);

    gzipOutputStream.write(message);
    gzipOutputStream.close();//from   w  ww .  ja  v a 2 s  . c om

    byte[] compressedMessage = compressedMessageStream.toByteArray();
    compressedMessageStream.close();

    long elapsedTime = System.currentTimeMillis() - startTime;

    logger.info(String.format("Compressed message from %d to %d bytes in %dms", message.length,
            compressedMessage.length, elapsedTime));

    return compressedMessage;
}

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

/**
 * Compress a string in GZIP format/*from  w w  w .j  a va  2s.  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;
    }//  w w w.  jav  a 2s  . com
    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:net.ulno.jpunch.util.Util.java

public static byte[] zip(byte[] data) throws IOException {
    ByteArrayOutputStream os = new ByteArrayOutputStream();
    GZIPOutputStream gz = new GZIPOutputStream(os);
    gz.write(data);
    gz.finish();//from  w  w  w.j a  v a2s  .  c  o  m
    return os.toByteArray();
}