Example usage for java.util.zip Deflater Deflater

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

Introduction

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

Prototype

public Deflater() 

Source Link

Document

Creates a new compressor with the default compression level.

Usage

From source file:de.tudarmstadt.ukp.wikipedia.revisionmachine.difftool.data.codec.RevisionEncoder.java

@Override
public String encodeDiff(final RevisionCodecData codecData, final Diff diff)
        throws UnsupportedEncodingException, EncodingException {

    String sEncoding;/*from w  w  w.  j a va2  s . c om*/
    byte[] bData = encode(codecData, diff);
    if (MODE_ZIP_COMPRESSION) {

        Deflater compresser = new Deflater();
        compresser.setInput(bData);
        compresser.finish();

        byte[] output = new byte[1000];
        ByteArrayOutputStream stream = new ByteArrayOutputStream();

        int cLength;
        do {
            cLength = compresser.deflate(output);
            stream.write(output, 0, cLength);
        } while (cLength == 1000);

        output = stream.toByteArray();

        if (bData.length + 1 < output.length) {
            sEncoding = Base64.encodeBase64String(bData);
        } else {
            sEncoding = "_" + Base64.encodeBase64String(output);
        }
    } else {
        sEncoding = Base64.encodeBase64String(bData);
    }

    return sEncoding;
}

From source file:org.apache.fop.render.pdf.ImageRawPNGAdapter.java

/** {@inheritDoc} */
public void outputContents(OutputStream out) throws IOException {
    InputStream in = ((ImageRawStream) image).createInputStream();

    try {/*from  w ww . j  a va2  s  .  c  o  m*/
        if (numberOfInterleavedComponents == 1 || numberOfInterleavedComponents == 3) {
            // means we have Gray, RGB, or Palette
            IOUtils.copy(in, out);
        } else {
            // means we have Gray + alpha or RGB + alpha
            // TODO: since we have alpha here do this when the alpha channel is extracted
            int numBytes = numberOfInterleavedComponents - 1; // 1 for Gray, 3 for RGB
            int numColumns = image.getSize().getWidthPx();
            InflaterInputStream infStream = new InflaterInputStream(in, new Inflater());
            DataInputStream dataStream = new DataInputStream(infStream);
            int offset = 0;
            int bytesPerRow = numberOfInterleavedComponents * numColumns;
            int filter;
            // here we need to inflate the PNG pixel data, which includes alpha, separate the alpha
            // channel and then deflate the RGB channels back again
            DeflaterOutputStream dos = new DeflaterOutputStream(out, new Deflater());
            while ((filter = dataStream.read()) != -1) {
                byte[] bytes = new byte[bytesPerRow];
                dataStream.readFully(bytes, 0, bytesPerRow);
                dos.write((byte) filter);
                for (int j = 0; j < numColumns; j++) {
                    dos.write(bytes, offset, numBytes);
                    offset += numberOfInterleavedComponents;
                }
                offset = 0;
            }
            dos.close();
        }
    } finally {
        IOUtils.closeQuietly(in);
    }
}

From source file:de.unidue.inf.is.ezdl.dlcore.utils.StringUtils.java

/**
 * Compresses a string.//from   w ww .  j a va 2s.  c o  m
 * 
 * @param s
 *            The string to compress
 * @return The compressed string
 */
public static String compress(String s) {
    ByteArrayOutputStream baos = null;
    try {
        byte[] input = s.getBytes("UTF-8");
        Deflater compresser = new Deflater();
        compresser.setLevel(Deflater.BEST_COMPRESSION);
        compresser.setInput(input);
        compresser.finish();
        baos = new ByteArrayOutputStream();
        while (!compresser.finished()) {
            byte[] output = new byte[1024];
            int compressedDataLength = compresser.deflate(output);
            baos.write(output, 0, compressedDataLength);
        }
        baos.flush();
        return Base64.encodeBase64String(baos.toByteArray());

    } catch (UnsupportedEncodingException e) {
        logger.error(e.getMessage(), e);
    } catch (IOException e) {
        logger.error(e.getMessage(), e);
    } finally {
        ClosingUtils.close(baos);
    }
    return "";
}

From source file:com.sixt.service.framework.registry.consul.RegistrationManager.java

private String binaryEncode(String tag) throws IOException {
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    Deflater deflater = new Deflater();
    deflater.setInput(tag.getBytes());/*from  ww  w  .ja v  a 2s . c  o m*/
    deflater.finish();
    byte[] buffer = new byte[1024];
    while (!deflater.finished()) {
        int count = deflater.deflate(buffer);
        outputStream.write(buffer, 0, count);
    }
    outputStream.close();
    byte compressed[] = outputStream.toByteArray();
    return bytesToHex(compressed);
}

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

public static DeflaterInflaterData compressBytes(byte[] input) {
    Deflater compresser = new Deflater();
    compresser.setInput(input);/* ww w. j av  a 2 s  . co 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:com.android.server.wifi.WifiLogger.java

private static String compressToBase64(byte[] input) {
    String result;//from w  w w  . jav a  2 s .  com
    //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:nl.nn.adapterframework.util.Misc.java

public static byte[] compress(byte[] input) 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   ww  w. j a  va2  s  .com*/
    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);
    }
    bos.close();

    // Get the compressed data
    return bos.toByteArray();
}

From source file:org.apache.oozie.workflow.lite.LiteWorkflowAppParser.java

/**
 * Write the GlobalSectionData to a Base64 string.
 * @param globalSectionData//from  ww w. j  av a 2  s.c o  m
 * @return String
 * @throws WorkflowException
 */
private String getGlobalString(GlobalSectionData globalSectionData) throws WorkflowException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    DataOutputStream oos = null;
    try {
        Deflater def = new Deflater();
        oos = new DataOutputStream(new DeflaterOutputStream(baos, def));
        globalSectionData.write(oos);
        oos.close();
    } catch (IOException e) {
        throw new WorkflowException(ErrorCode.E0700, "Error while processing global section conf");
    }
    return Base64.encodeBase64String(baos.toByteArray());
}

From source file:com.giri.target.svr.SeleniumTestRunner.java

private String toB64Text(final String text, final boolean compress) throws Exception {
    final byte[] inputbs = text.getBytes(Charset.forName("UTF-8"));

    final byte[] bytesToConvert;
    if (compress) {
        final ByteArrayOutputStream bout = new ByteArrayOutputStream();
        final Deflater d = new Deflater();
        final DeflaterOutputStream dout = new DeflaterOutputStream(bout, d);
        dout.write(inputbs);//from w  w  w.ja  va  2s.  c  om
        dout.close();
        bout.flush();
        bytesToConvert = bout.toByteArray();
    } else {
        bytesToConvert = inputbs;
    }

    final byte[] s64encBts = Base64.encodeBase64(bytesToConvert);

    return new String(s64encBts);
}

From source file:org.jwebsocket.util.Tools.java

/**
 * Deflate a byte array with Zip compression
 *
 * @param aUncompressedData The uncompressed data
 * @return The compressed data/*  ww  w.  ja v a  2  s.  c  om*/
 * @throws Exception
 */
public static byte[] deflate(byte[] aUncompressedData) throws Exception {
    Deflater lDeflater = new Deflater();
    lDeflater.setInput(aUncompressedData);
    lDeflater.finish();
    byte[] lOut = new byte[1024 * 1000 * 5];
    int lWritten = lDeflater.deflate(lOut);
    byte[] lResult = new byte[lWritten];

    System.arraycopy(lOut, 0, lResult, 0, lWritten);

    return lResult;
}