Example usage for java.util.zip Deflater deflate

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

Introduction

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

Prototype

public int deflate(ByteBuffer output) 

Source Link

Document

Compresses the input data and fills specified buffer with compressed data.

Usage

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

public void compress() {
    if (!compressed) {
        Deflater deflater = new Deflater();
        deflater.setInput(fileData);// ww w  .  j av  a 2  s .c  o  m
        deflater.setLevel(Deflater.BEST_COMPRESSION);
        deflater.finish();
        ByteArrayOutputStream bos = new ByteArrayOutputStream(fileData.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();
        }
        fileData = bos.toByteArray();
        compressed = true;
    }
}

From source file:com.newrelic.agent.android.harvest.HarvestConnection.java

private byte[] deflate(String str) {
    Deflater deflater = new Deflater();
    deflater.setInput(str.getBytes());/*from  w w  w . j a va2  s . c  o  m*/
    deflater.finish();
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    byte[] bArr = new byte[AccessibilityNodeInfoCompat.ACTION_SCROLL_BACKWARD];
    while (!deflater.finished()) {
        int deflate = deflater.deflate(bArr);
        if (deflate <= 0) {
            this.log.error("HTTP request contains an incomplete payload");
        }
        byteArrayOutputStream.write(bArr, 0, deflate);
    }
    deflater.end();
    return byteArrayOutputStream.toByteArray();
}

From source file:spartanfinal.ProcessFiles.java

public byte[] compress(byte[] data) throws IOException {
    Deflater deflater = new Deflater();
    deflater.setLevel(Deflater.BEST_COMPRESSION);
    deflater.setInput(data);// www  . j  a v a 2s  .  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);
    }
    byte[] output = outputStream.toByteArray();

    return output;
}

From source file:org.jmangos.realm.network.packet.wow.client.CMSG_REQUEST_ACCOUNT_DATA.java

@Override
protected void runImpl() {

    if (this.type > AccountDataType.NUM_ACCOUNT_DATA_TYPES.getValue()) {
        return;// w  w w.ja  v  a 2s  .  c om
    }
    final HashMap<Integer, AccountData> adata = new HashMap<Integer, AccountData>(); // getAccount().getAccountData();
                                                                                     // /*
                                                                                     // disabled
                                                                                     // by
                                                                                     // paalgyula
                                                                                     // */
    if (adata.containsKey(this.type)) {
        final Deflater compressor = new Deflater();
        final byte[] dataToCompress = adata.get(this.type).getData().getBytes(Charset.forName("UTF-8"));

        compressor.setInput(dataToCompress);
        compressor.finish();
        final ByteArrayOutputStream bos = new ByteArrayOutputStream(dataToCompress.length);

        final byte[] buf = new byte[1024];
        while (!compressor.finished()) {
            final int count = compressor.deflate(buf);
            bos.write(buf, 0, count);
        }
        try {
            bos.close();
        } catch (final IOException e) {
        }
        // FIXME NEED COMPLETE
        @SuppressWarnings("unused")
        final byte[] compressedData = bos.toByteArray();

    }
}

From source file:org.getspout.spoutapi.packet.PacketCustomBlockChunkOverride.java

@Override
public void compress() {
    if (!compressed) {
        if (data != null && hasData) {
            Deflater deflater = new Deflater();
            deflater.setInput(data);/*from  w w  w  . j  ava2  s  . 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;
    }
}

From source file:com.gargoylesoftware.htmlunit.WebClient3Test.java

private void doTestDeflateCompression(final boolean gzipCompatibleCompression) throws Exception {
    final byte[] input = "document.title = 'modified';".getBytes("UTF-8");

    final byte[] buffer = new byte[100];
    final Deflater deflater = new Deflater(Deflater.DEFAULT_COMPRESSION, gzipCompatibleCompression);
    deflater.setInput(input);// www  . java  2  s.c o  m
    deflater.finish();

    final int compressedDataLength = deflater.deflate(buffer);
    final byte[] content = new byte[compressedDataLength];
    System.arraycopy(buffer, 0, content, 0, compressedDataLength);

    final List<NameValuePair> headers = new ArrayList<>();
    headers.add(new NameValuePair("Content-Encoding", "deflate"));
    headers.add(new NameValuePair("Content-Length", String.valueOf(compressedDataLength)));

    final MockWebConnection conn = getMockWebConnection();
    conn.setResponse(URL_SECOND, content, 200, "OK", "text/javascript", headers);

    final String html = "<html><head>" + "<title>Hello world</title>" + "<script src='" + URL_SECOND
            + "'></script>" + "</head><body><script>alert(document.title)</script></body></html>";
    loadPageWithAlerts2(html);
}

From source file:org.getspout.spoutapi.packet.PacketAddonData.java

@Override
public void compress() {
    if (!compressed) {
        if (data != null) {
            Deflater deflater = new Deflater();
            deflater.setInput(data);/*from  w w  w  .  j a va2  s . co  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;
    }
}

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

/**
 * Deflate a byte array with Zip compression
 *
 * @param aUncompressedData The uncompressed data
 * @return The compressed data/*from  w ww.ja  v  a2 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;
}

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   w w  w  . ja v  a  2  s  .  co 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.qpid.multiconsumer.AMQTest.java

private String compressString(String string) throws Exception {
    long start = System.currentTimeMillis();
    byte[] input = string.getBytes();
    Deflater compressor = new Deflater(Deflater.BEST_COMPRESSION);
    compressor.setInput(input);/*from w w w.  j a  va  2  s . c o m*/
    compressor.finish();

    // Get byte array from output of compressor
    ByteArrayOutputStream baos = new ByteArrayOutputStream(input.length);
    byte[] buf = new byte[1024];
    while (!compressor.finished()) {
        int cnt = compressor.deflate(buf);
        baos.write(buf, 0, cnt);
    }
    baos.close();
    byte[] output = baos.toByteArray();

    // Convert byte array into String
    byte[] base64 = Base64.encodeBase64(output);
    String sComp = new String(base64, UTF8);

    long diff = System.currentTimeMillis() - start;
    System.out.println(
            "Compressed text from " + input.length + " to " + sComp.getBytes().length + " in " + diff + " ms");
    System.out.println("Compressed text = '" + sComp + "'");

    return sComp;
}