Example usage for java.util.zip Deflater finish

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

Introduction

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

Prototype

boolean finish

To view the source code for java.util.zip Deflater finish.

Click Source Link

Usage

From source file:com.android.server.wifi.WifiLogger.java

private static String compressToBase64(byte[] input) {
    String result;/*w  ww  .  j  av  a 2s  .  c  om*/
    //compress
    Deflater compressor = new Deflater();
    compressor.setLevel(Deflater.BEST_COMPRESSION);
    compressor.setInput(input);
    compressor.finish();
    ByteArrayOutputStream bos = new ByteArrayOutputStream(input.length);
    final byte[] buf = new byte[1024];

    while (!compressor.finished()) {
        int count = compressor.deflate(buf);
        bos.write(buf, 0, count);
    }

    try {
        compressor.end();
        bos.close();
    } catch (IOException e) {
        Log.e(TAG, "ByteArrayOutputStream close error");
        result = android.util.Base64.encodeToString(input, Base64.DEFAULT);
        return result;
    }

    byte[] compressed = bos.toByteArray();
    if (DBG) {
        Log.d(TAG, " length is:" + (compressed == null ? "0" : compressed.length));
    }

    //encode
    result = android.util.Base64.encodeToString(compressed.length < input.length ? compressed : input,
            Base64.DEFAULT);

    if (DBG) {
        Log.d(TAG, "FwMemoryDump length is :" + result.length());
    }

    return result;
}

From source file:com.nary.Debug.java

public static void saveClass(OutputStream _out, Object _class, boolean _compress) throws IOException {
    if (_compress) {
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        ObjectOutputStream OOS = new ObjectOutputStream(bos);
        OOS.writeObject(_class);

        byte[] dataArray = bos.toByteArray();
        byte[] test = new byte[dataArray.length]; // this is where the byte array gets compressed to
        Deflater def = new Deflater(Deflater.BEST_COMPRESSION);
        def.setInput(dataArray);//ww  w  . ja v a2 s. c  om
        def.finish();
        def.deflate(test);
        _out.write(test, 0, def.getTotalOut());
    } else {
        ObjectOutputStream OS = new ObjectOutputStream(_out);
        OS.writeObject(_class);
    }
}

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   www  .j  a  v  a 2s  .  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:com.simiacryptus.text.CompressionUtil.java

/**
 * Encode lz byte [ ]./*from w  w  w. j av a  2s.co m*/
 *
 * @param bytes      the bytes
 * @param dictionary the dictionary
 * @return the byte [ ]
 */
public static byte[] encodeLZ(byte[] bytes, String dictionary) {
    byte[] output = new byte[(int) (bytes.length * 1.05 + 32)];
    Deflater compresser = new Deflater();
    try {
        compresser.setInput(bytes);
        if (null != dictionary && !dictionary.isEmpty()) {
            byte[] bytes2 = dictionary.getBytes("UTF-8");
            compresser.setDictionary(bytes2);
        }
    } catch (UnsupportedEncodingException e) {
        throw new RuntimeException(e);
    }
    compresser.finish();
    int compressedDataLength = compresser.deflate(output);
    compresser.end();
    return Arrays.copyOf(output, compressedDataLength);
}

From source file:org.apache.geode.management.internal.cli.CliUtil.java

public static DeflaterInflaterData compressBytes(byte[] input) {
    Deflater compresser = new Deflater();
    compresser.setInput(input);/* www.ja  v a  2s  .  c o  m*/
    compresser.finish();
    byte[] buffer = new byte[100];
    byte[] result = new byte[0];
    int compressedDataLength = 0;
    int totalCompressedDataLength = 0;
    do {
        byte[] newResult = new byte[result.length + buffer.length];
        System.arraycopy(result, 0, newResult, 0, result.length);

        compressedDataLength = compresser.deflate(buffer);
        totalCompressedDataLength += compressedDataLength;
        System.arraycopy(buffer, 0, newResult, result.length, buffer.length);
        result = newResult;
    } while (compressedDataLength != 0);
    return new DeflaterInflaterData(totalCompressedDataLength, result);
}

From source file:fr.mby.saml2.sp.impl.helper.SamlHelper.java

/**
 * Encode a SAML2 request for the HTTP-redirect binding. The encoded message is not URL encoded !
 * /*from   www .  j av a2s. c om*/
 * @param request
 *            the request
 * @return the encoded request
 * @throws IOException
 */
public static String httpRedirectEncode(final String samlMessage) throws IOException {
    String deflatedRequest = null;
    ByteArrayOutputStream byteArrayOutputStream = null;
    DeflaterOutputStream deflaterOutputStream = null;

    try {
        final Deflater deflater = new Deflater(Deflater.DEFLATED, true);
        byteArrayOutputStream = new ByteArrayOutputStream();
        deflaterOutputStream = new DeflaterOutputStream(byteArrayOutputStream, deflater);

        // Deflated then Base 64 encoded then Url Encoded for HTTP REDIRECT Binding
        deflaterOutputStream.write(samlMessage.getBytes());
        deflaterOutputStream.finish();
        deflater.finish();

        deflatedRequest = Base64.encodeBytes(byteArrayOutputStream.toByteArray(), Base64.DONT_BREAK_LINES);

        if (SamlHelper.LOGGER.isDebugEnabled()) {
            SamlHelper.LOGGER.debug(String.format("SAML 2.0 Request: %s", samlMessage));
            SamlHelper.LOGGER.debug(String.format("Encoded HTTP-Redirect Request: %s", deflatedRequest));
        }
    } finally {
        if (byteArrayOutputStream != null) {
            byteArrayOutputStream.close();
        }
        if (deflaterOutputStream != null) {
            deflaterOutputStream.close();
        }
    }

    return deflatedRequest;
}

From source file:org.apache.marmotta.kiwi.io.KiWiIO.java

/**
 * Write a string to the data output. In case the string length exceeds LITERAL_COMPRESS_LENGTH, uses a LZW
 * compressed format, otherwise writes the plain bytes.
 *
 * @param out      output destination to write to
 * @param content  string to write/*from w  ww.j a  va2s  . c o  m*/
 * @throws IOException
 */
private static void writeContent(DataOutput out, String content) throws IOException {
    if (content.length() > LITERAL_COMPRESS_LENGTH) {
        // temporary buffer of the size of bytes in the content string (assuming that the compressed data will fit into it)
        byte[] data = content.getBytes("UTF-8");
        byte[] buffer = new byte[data.length];

        Deflater compressor = new Deflater(Deflater.BEST_COMPRESSION, true);
        compressor.setInput(data);
        compressor.finish();

        int length = compressor.deflate(buffer);

        // only use compressed version if it is smaller than the number of bytes used by the string
        if (length < buffer.length) {
            log.debug("compressed string with {} bytes; compression ratio {}", data.length,
                    (double) length / data.length);

            out.writeByte(MODE_COMPRESSED);
            out.writeInt(data.length);
            out.writeInt(length);
            out.write(buffer, 0, length);
        } else {
            log.warn("compressed length exceeds string buffer: {} > {}", length, buffer.length);

            out.writeByte(MODE_DEFAULT);
            DataIO.writeString(out, content);
        }

        compressor.end();
    } else {
        out.writeByte(MODE_DEFAULT);
        DataIO.writeString(out, content);
    }
}

From source file:NCDSearch.DistributedNCDSearch.java

public static float NCD(byte[] file, byte[] target, int compression) {

    Deflater compressor = new Deflater(compression);

    //This is where we dump our compressed bytes.  All we need is the size of this thing.  
    //TODO: In theory the compressed bytes could exceed the length of the target files...
    byte[] outputtrash = new byte[file.length + target.length];

    int bothcompressedsize;
    int filecompressedsize;
    int targetcompressedsize;

    //puts the target file and the searched file together.
    byte[] both = new byte[file.length + target.length];
    for (int i = 0; i < file.length; i++) {
        both[i] = file[i];/*from  w  ww . j a va  2  s .c  om*/
    }
    for (int i = 0; i < target.length; i++) {
        both[i + file.length] = target[i];
    }

    compressor.setInput(file);
    compressor.finish();
    filecompressedsize = compressor.deflate(outputtrash);
    compressor.reset();
    compressor.setInput(target);
    compressor.finish();
    targetcompressedsize = compressor.deflate(outputtrash);
    compressor.reset();
    compressor.setInput(both);
    compressor.finish();
    bothcompressedsize = compressor.deflate(outputtrash);
    compressor.reset();

    return (float) (bothcompressedsize - filecompressedsize) / (float) targetcompressedsize;
}

From source file:acp.sdk.SecureUtil.java

/**
 * ./*from ww  w  .  j  a va  2s  .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:com.kactech.otj.Utils.java

public static byte[] zlibCompress(byte[] data) {
    Deflater deflater = new Deflater();
    deflater.setInput(data);/*from   w ww  .  ja  v a 2 s  .c o 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();
}