Example usage for java.util.zip DeflaterOutputStream DeflaterOutputStream

List of usage examples for java.util.zip DeflaterOutputStream DeflaterOutputStream

Introduction

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

Prototype

public DeflaterOutputStream(OutputStream out, boolean syncFlush) 

Source Link

Document

Creates a new output stream with a default compressor, a default buffer size and the specified flush mode.

Usage

From source file:Main.java

public static String compressString(String str, String encoding) {
    String compressedString = "";

    try {/*  ww w  . j a v a 2 s.co  m*/
        byte[] input = str.getBytes(encoding);
        Deflater d = new Deflater();
        d.setLevel(Deflater.BEST_COMPRESSION);
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        DeflaterOutputStream dout = new DeflaterOutputStream(out, d);
        dout.write(input);
        dout.close();
        compressedString = new String(Hex.encodeHex(out.toByteArray()));
    } catch (Exception e) {
        e.printStackTrace();
    }

    return compressedString;
}

From source file:zipB64.java

protected static String encodeMessage(String messageStr) {
    try {// w  w  w . j a  v  a 2 s .co m
        ByteArrayOutputStream bytesOut = new ByteArrayOutputStream();
        Deflater deflater = new Deflater(Deflater.DEFLATED);
        DeflaterOutputStream deflaterStream = new DeflaterOutputStream(bytesOut, deflater);
        deflaterStream.write(messageStr.getBytes("UTF-8"));
        deflaterStream.finish();

        Base64 b = new Base64(-1);
        return new String(b.encode(bytesOut.toByteArray()));
    } catch (Exception e) {
        return "crotte";
    }
}

From source file:com.barrybecker4.common.util.Base64Codec.java

/**
 * take a String and compress it./*from w  w  w . j a va2s  .  c  om*/
 * See @decompress for reversing the compression.
 * @param data a string to compress.
 * @return compressed string representation.
 */
public static synchronized String compress(final String data) {

    ByteArrayOutputStream byteOut = new ByteArrayOutputStream(512);
    Deflater deflater = new Deflater();
    DeflaterOutputStream oStream = new DeflaterOutputStream(byteOut, deflater);

    try {
        oStream.write(data.getBytes(CONVERTER_UTF8));
        oStream.flush();
        oStream.close();
    } catch (UnsupportedEncodingException e) {
        throw new IllegalArgumentException("Unsupported encoding exception :" + e.getMessage(), e);
    } catch (IOException e) {
        throw new IllegalStateException("io error :" + e.getMessage(), e);
    }

    return new String(Base64.encodeBase64(byteOut.toByteArray()));
}

From source file:Main.java

/**
 * Creates a SyncFlush output stream, even if the current device does not support doing so
 * natively.//w  ww .j  a va2s  .  c  o  m
 *
 * @param rawOut the raw output stream to be wrapped
 * @return The wrapping output stream
 */
@NonNull
public static DeflaterOutputStream createDeflaterOutputStream(@Nullable OutputStream rawOut) {
    if (deviceSupportsCompression())
        return new DeflaterOutputStream(rawOut, true);
    else
        return new DeflaterOutputStream(rawOut, createSyncFlushDeflater());
}

From source file:com.hundsun.jresplus.web.nosession.cookie.HessianZipSerializer.java

public static byte[] encode(Object object) throws SerializationException {
    if (object == null) {
        return null;
    }/*from w w w . j a v  a2 s .  c o  m*/
    ByteArrayOutputStream baos = new ByteArrayOutputStream();

    Deflater def = new Deflater(Deflater.BEST_COMPRESSION, false);
    DeflaterOutputStream dos = new DeflaterOutputStream(baos, def);

    Hessian2Output ho = null;

    try {
        ho = new Hessian2Output(dos);
        ho.writeObject(object);
    } catch (Exception e) {
        throw new SerializationException("Failed to encode date", e);
    } finally {
        if (ho != null) {
            try {
                ho.close();
            } catch (IOException e) {
            }
        }
        try {
            dos.close();
        } catch (IOException e) {
        }

        def.end();
    }

    return baos.toByteArray();
}

From source file:org.apache.pig.impl.util.ObjectSerializer.java

public static String serialize(Serializable obj) throws IOException {
    if (obj == null)
        return "";
    try {/*from  w ww  .j ava  2 s . c o m*/
        ByteArrayOutputStream serialObj = new ByteArrayOutputStream();
        Deflater def = new Deflater(Deflater.BEST_COMPRESSION);
        ObjectOutputStream objStream = new ObjectOutputStream(new DeflaterOutputStream(serialObj, def));
        objStream.writeObject(obj);
        objStream.close();
        return encodeBytes(serialObj.toByteArray());
    } catch (Exception e) {
        throw new IOException("Serialization error: " + e.getMessage(), e);
    }
}

From source file:org.openmrs.module.shr.contenthandler.api.ContentTest.java

private static byte[] compressDeflate(String content) throws IOException {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    DeflaterOutputStream deflateOut = new DeflaterOutputStream(out, new Deflater(0, true));
    deflateOut.write(content.getBytes());
    IOUtils.closeQuietly(deflateOut);/*w  ww .  ja v a 2  s  .com*/
    return out.toByteArray();
}

From source file:rascal.storage.loose.LooseStorageNodeWritableChannel.java

private void initTempObjectFileChannel() throws IOException {
    Deflater deflater = new Deflater(storageConfiguration.getLooseCompressionLevel());
    DeflaterOutputStream output = new DeflaterOutputStream(new FileOutputStream(tempObjectFile), deflater);
    tempObjectFileChannel = Channels.newChannel(output);
}

From source file:rascal.storage.loose.AbstractLooseStorageNodeChannelIntegrationTest.java

@Before
public void setUpTestData() throws IOException {
    testData = RandomTestDataUtils.createRandomData();
    ByteArrayOutputStream outBuffer = new ByteArrayOutputStream();
    DeflaterOutputStream out = new DeflaterOutputStream(outBuffer, new Deflater(DEFLATED_DATA_COMPRESS_LEVER));
    out.write(testData);/* ww  w . ja v  a 2 s.c  o m*/
    out.close();
    deflatedTestData = outBuffer.toByteArray();
}

From source file:org.apache.tez.common.TezUtils.java

/**
 * Convert a Configuration to compressed ByteString using Protocol buffer
 *
 * @param conf/* w  w  w  .j  av  a2 s .com*/
 *          : Configuration to be converted
 * @return PB ByteString (compressed)
 * @throws java.io.IOException
 */
public static ByteString createByteStringFromConf(Configuration conf) throws IOException {
    Preconditions.checkNotNull(conf, "Configuration must be specified");
    ByteString.Output os = ByteString.newOutput();
    DeflaterOutputStream compressOs = new DeflaterOutputStream(os, new Deflater(Deflater.BEST_SPEED));
    try {
        writeConfInPB(compressOs, conf);
    } finally {
        if (compressOs != null) {
            compressOs.close();
        }
    }
    return os.toByteString();
}