Example usage for java.util.zip Deflater finished

List of usage examples for java.util.zip Deflater finished

Introduction

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

Prototype

public boolean finished() 

Source Link

Document

Returns true if the end of the compressed data output stream has been reached.

Usage

From source file:com.bigdata.dastor.utils.FBUtilities.java

public static void compressToStream(byte[] input, ByteArrayOutputStream bos) throws IOException {
    // Create the compressor with highest level of compression
    Deflater compressor = new Deflater();
    compressor.setLevel(Deflater.BEST_COMPRESSION);

    // Give the compressor the data to compress
    compressor.setInput(input);/*from   w  w  w  .ja  v  a 2 s  .  c  o m*/
    compressor.finish();

    // Write the compressed data to the stream
    byte[] buf = new byte[1024];
    while (!compressor.finished()) {
        int count = compressor.deflate(buf);
        bos.write(buf, 0, count);
    }
}

From source file:org.ow2.proactive.utils.ObjectByteConverter.java

/**
 * Convert the given Serializable Object into a byte array.
 * <p>/*w  w  w  .  j a va  2s. c  o  m*/
 * The returned byteArray can be compressed by setting compress boolean argument value to <code>true</code>.
 * 
 * @param obj the Serializable object to be compressed
 * @param compress true if the returned byteArray must be also compressed, false if no compression is required.
 * @return a compressed (or not) byteArray representing the Serialization of the given object.
 * @throws IOException if an I/O exception occurs when writing the output byte array
 */
public static final byte[] objectToByteArray(Object obj, boolean compress) throws IOException {
    ByteArrayOutputStream baos = null;
    ObjectOutputStream oos = null;
    if (obj == null) {
        return null;
    }
    try {
        baos = new ByteArrayOutputStream();
        oos = new ObjectOutputStream(baos);
        oos.writeObject(obj);
        oos.flush();
        if (!compress) {
            // Return the UNCOMPRESSED data
            return baos.toByteArray();
        } else {
            // Compressor with highest level of compression
            Deflater compressor = new Deflater();
            compressor.setLevel(Deflater.BEST_COMPRESSION);
            // Give the compressor the data to compress
            compressor.setInput(baos.toByteArray());
            compressor.finish();

            ByteArrayOutputStream bos = null;
            try {
                // Create an expandable byte array to hold the compressed data.
                bos = new ByteArrayOutputStream();
                // Compress the data
                byte[] buf = new byte[512];
                while (!compressor.finished()) {
                    int count = compressor.deflate(buf);
                    bos.write(buf, 0, count);
                }
                // Return the COMPRESSED data
                return bos.toByteArray();
            } finally {
                if (bos != null) {
                    bos.close();
                }
            }
        }
    } finally {
        if (oos != null) {
            oos.close();
        }
        if (baos != null) {
            baos.close();
        }
    }
}

From source file:edu.stanford.junction.addon.JSONObjWrapper.java

private static String compressString(String str) {
    byte[] input;
    try {/* w ww  . ja  va  2s. c  o  m*/
        input = str.getBytes("UTF-8");
    } catch (UnsupportedEncodingException e) {
        input = str.getBytes();
    }
    // Create the compressor with highest level of compression 
    Deflater compressor = new Deflater();
    compressor.setLevel(Deflater.BEST_COMPRESSION);
    // Give the compressor the data to compress 
    compressor.setInput(input);
    compressor.finish();

    // Create an expandable byte array to hold the compressed data. 
    // You cannot use an array that's the same size as the orginal because 
    // there is no guarantee that the compressed data will be smaller than 
    // the uncompressed data. 
    ByteArrayOutputStream bos = new ByteArrayOutputStream(input.length);
    // Compress the data 
    byte[] buf = new byte[1024];
    while (!compressor.finished()) {
        int count = compressor.deflate(buf);
        bos.write(buf, 0, count);
    }
    try {
        bos.close();
    } catch (IOException e) {
    }
    // Get the compressed data 
    byte[] compressedData = bos.toByteArray();
    return Base64.encodeBytes(compressedData);
}

From source file:com.kactech.otj.Utils.java

public static byte[] zlibCompress(byte[] data) {
    Deflater deflater = new Deflater();
    deflater.setInput(data);//w  ww  . j  ava2s  . co m

    ByteArrayOutputStream outputStream = new ByteArrayOutputStream(data.length);

    deflater.finish();
    byte[] buffer = new byte[1024];
    while (!deflater.finished()) {
        int count = deflater.deflate(buffer);
        outputStream.write(buffer, 0, count);
    }
    try {
        outputStream.close();
    } catch (IOException e) {
        // don't be silly
        throw new RuntimeException(e);
    }
    return outputStream.toByteArray();
}

From source file:acp.sdk.SecureUtil.java

/**
 * .//from  ww  w. ja  v  a2  s  . c o m
 * 
 * @param inputByte
 *            ?byte[]
 * @return ??
 * @throws IOException
 */
public static byte[] deflater(final byte[] inputByte) throws IOException {
    int compressedDataLength = 0;
    Deflater compresser = new Deflater();
    compresser.setInput(inputByte);
    compresser.finish();
    ByteArrayOutputStream o = new ByteArrayOutputStream(inputByte.length);
    byte[] result = new byte[1024];
    try {
        while (!compresser.finished()) {
            compressedDataLength = compresser.deflate(result);
            o.write(result, 0, compressedDataLength);
        }
    } finally {
        o.close();
    }
    compresser.end();
    return o.toByteArray();
}

From source file:org.hyperic.hq.livedata.agent.commands.LiveData_result.java

private String compress(String s) throws IOException {
    Deflater compressor = new Deflater();
    compressor.setInput(s.getBytes("UTF-8"));
    compressor.finish();/*from   w w  w. ja v  a2 s  . c o m*/

    ByteArrayOutputStream bos = new ByteArrayOutputStream();

    byte[] buf = new byte[1024];
    while (!compressor.finished()) {
        int count = compressor.deflate(buf);
        bos.write(buf, 0, count);
    }

    bos.close();

    byte[] compressedData = bos.toByteArray();
    return Base64.encode(compressedData);
}

From source file:com.ctriposs.r2.filter.compression.DeflateCompressor.java

@Override
public byte[] deflate(InputStream data) throws CompressionException {
    byte[] input;
    try {/*from   ww  w . j ava  2 s  . c  om*/
        input = IOUtils.toByteArray(data);
    } catch (IOException e) {
        throw new CompressionException(CompressionConstants.DECODING_ERROR + CompressionConstants.BAD_STREAM,
                e);
    }

    Deflater zlib = new Deflater();
    zlib.setInput(input);
    zlib.finish();

    ByteArrayOutputStream output = new ByteArrayOutputStream();
    byte[] temp = new byte[CompressionConstants.BUFFER_SIZE];

    int bytesRead;
    while (!zlib.finished()) {
        bytesRead = zlib.deflate(temp);

        if (bytesRead == 0) {
            if (!zlib.needsInput()) {
                throw new CompressionException(CompressionConstants.DECODING_ERROR + getContentEncodingName());
            } else {
                break;
            }
        }
        output.write(temp, 0, bytesRead);
    }
    zlib.end();

    return output.toByteArray();
}

From source file:Comman.Tool.java

public byte[] Image_compress(final byte[] data) {
    if (data == null || data.length == 0) {
        return new byte[0];
    }//from  w  w  w .  j a  v a 2 s  .co  m

    try (final ByteArrayOutputStream out = new ByteArrayOutputStream(data.length)) {
        final Deflater deflater = new Deflater();
        deflater.setInput(data);

        deflater.finish();
        final byte[] buffer = new byte[1024];
        while (!deflater.finished()) {
            out.write(buffer, 0, deflater.deflate(buffer));
        }

        return out.toByteArray();
    } catch (final IOException e) {
        System.err.println("Compression failed! Returning the original data...");
        return data;
    }
}

From source file:org.sonar.microbenchmark.SerializationBenchmarkTest.java

private File zipFile(File input) throws Exception {
    File zipFile = new File(input.getAbsolutePath() + ".zip");
    Deflater deflater = new Deflater();
    byte[] content = FileUtils.readFileToByteArray(input);
    deflater.setInput(content);//from   ww  w .ja va2 s.  c  o  m
    try (OutputStream outputStream = new FileOutputStream(zipFile)) {
        deflater.finish();
        byte[] buffer = new byte[1024];
        while (!deflater.finished()) {
            int count = deflater.deflate(buffer); // returns the generated code... index
            outputStream.write(buffer, 0, count);
        }
    }
    deflater.end();

    return zipFile;
}

From source file:org.getspout.spout.packet.PacketBlockData.java

public void compress() {
    if (!compressed) {
        Deflater deflater = new Deflater();
        deflater.setInput(data);/*from  w w  w  . j a  v  a  2s  .  c  o  m*/
        deflater.setLevel(Deflater.BEST_COMPRESSION);
        deflater.finish();
        ByteArrayOutputStream bos = new ByteArrayOutputStream(data.length);
        byte[] buffer = new byte[1024];
        while (!deflater.finished()) {
            int bytesCompressed = deflater.deflate(buffer);
            bos.write(buffer, 0, bytesCompressed);
        }
        try {
            bos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        data = bos.toByteArray();
        compressed = true;
    }
}