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:org.dragonet.proxy.protocol.packet.LoginPacket.java

@Override
public void encode() {
    try {//from ww  w  . j  a  v a2 s .c o  m
        setChannel(NetworkChannel.CHANNEL_PRIORITY);

        // Basic info
        JSONObject jsonBasic = new JSONObject();
        String b64Signature;
        String b64User;
        {
            JSONObject jsonSignature = new JSONObject();
            jsonSignature.put("alg", "ES384");
            jsonSignature.put("x5u",
                    "MHYwEAYHKoZIzj0CAQYFK4EEACIDYgAE+SKWsy2I5ecBTPZEfNgwgvuin2/iqqi3haMxXPpknsVKINpp5E7jlygXqBmSJad5VG+1uq75V9irGEtmpUINP5xhYiOrlEma+2aBJIUe17UT/r50yTnDhDrPoOY/eAHL");
            b64Signature = Base64.getEncoder().encodeToString(jsonSignature.toString().getBytes("UTF-8"));
        }
        {
            JSONObject jsonUser = new JSONObject();
            jsonUser.put("exp", System.currentTimeMillis() / 1000L + (60 * 10));
            JSONObject jsonUserInfo = new JSONObject();
            jsonUserInfo.put("displayName", username);
            jsonUserInfo.put("identity", clientUuid.toString());
            jsonUser.put("extraData", jsonUserInfo);
            jsonUser.put("identityPublicKey", ""); // publicKey
            jsonUser.put("nbf", System.currentTimeMillis() / 1000L);
            b64User = Base64.getEncoder().encodeToString(jsonUser.toString().getBytes("UTF-8"));
            System.out.println(jsonUser.toString());
        }
        String b64Basic = b64Signature + "." + b64User;

        // Meta info
        JSONObject jsonMeta = new JSONObject();
        String strMeta;
        {
            jsonMeta.put("ClientRandomId", clientID);
            jsonMeta.put("ServerAddress", serverAddress);
            jsonMeta.put("SkinId", skin.getModel());
            jsonMeta.put("SkinData", Base64.getEncoder().encodeToString(skin.getData()));
            strMeta = Base64.getEncoder().encodeToString(jsonMeta.toString().getBytes("UTF-8"));
        }
        String b64Meta = b64Signature + "." + strMeta;

        byte[] chainData;
        {
            byte[] dataBasic = b64Basic.getBytes("UTF-8");
            byte[] dataMeta = b64Meta.getBytes("UTF-8");
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            PEBinaryWriter writer = new PEBinaryWriter(bos);
            writer.switchEndianness();
            writer.writeInt(dataBasic.length);
            writer.switchEndianness();
            writer.write(dataBasic);
            writer.switchEndianness();
            writer.writeInt(dataMeta.length);
            writer.switchEndianness();
            writer.write(dataMeta);

            chainData = bos.toByteArray();
        }

        JSONObject jsonChain = new JSONObject();
        jsonChain.put("chain", chainData);
        String strChain = jsonChain.toString();
        byte[] b64Chain = Base64.getEncoder().encode(strChain.getBytes("UTF-8"));
        Deflater deflater = new Deflater(7);
        deflater.setInput(b64Chain);
        deflater.finish();
        byte[] buff = new byte[40960];
        int deflated = deflater.deflate(buff);
        buff = ArrayUtils.subarray(buff, 0, deflated);
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        PEBinaryWriter writer = new PEBinaryWriter(bos);
        writer.writeByte((byte) (this.pid() & 0xFF));
        writer.writeInt(protocol);
        writer.writeInt(buff.length);
        writer.write(buff);
        this.setData(bos.toByteArray());
    } catch (IOException | JSONException e) {
    }
}

From source file:com.hichinaschool.flashcards.libanki.Utils.java

/**
 * Compress data.//  w w  w . jav a  2s  .c  om
 * @param bytesToCompress is the byte array to compress.
 * @return a compressed byte array.
 * @throws java.io.IOException
 */
public static byte[] compress(byte[] bytesToCompress, int comp) throws IOException {
    // Compressor with highest level of compression.
    Deflater compressor = new Deflater(comp, true);
    // Give the compressor the data to compress.
    compressor.setInput(bytesToCompress);
    compressor.finish();

    // Create an expandable byte array to hold the compressed data.
    // It is not necessary that the compressed data will be smaller than
    // the uncompressed data.
    ByteArrayOutputStream bos = new ByteArrayOutputStream(bytesToCompress.length);

    // Compress the data
    byte[] buf = new byte[65536];
    while (!compressor.finished()) {
        bos.write(buf, 0, compressor.deflate(buf));
    }

    bos.close();

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

From source file:com.itude.mobile.android.util.DataUtil.java

/**
 * Compress byte array /*from   www  .  j av  a2  s  .  c  o  m*/
 * 
 * @param uncompressed byte array
 * @return compressed byte array
 */
public byte[] compress(byte[] uncompressed) {
    byte[] result = null;

    Deflater deflater = new Deflater();
    deflater.setInput(uncompressed);
    deflater.finish();

    // Create an expandable byte array to hold the compressed data. 
    // You cannot use an array that's the same size as the original because 
    // there is no guarantee that the compressed data will be smaller than 
    // the uncompressed data. 

    ByteArrayOutputStream bos = new ByteArrayOutputStream(uncompressed.length);

    // Compress the data 
    byte[] buf = new byte[1024];
    while (!deflater.finished()) {
        int count = deflater.deflate(buf);
        bos.write(buf, 0, count);
    }
    deflater.end();

    try {
        bos.close();
    } catch (IOException e) {
        MBLog.w(TAG, "Unable to close stream");
    }

    // Get the compressed data 

    result = bos.toByteArray();

    return result;
}

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);/*from   w  w  w  .  ja v  a 2 s.  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.phpmaven.phar.PharJavaPackager.java

private void packFile(final ByteArrayOutputStream fileEntriesBaos,
        final ByteArrayOutputStream compressedFilesBaos, final File fileToPack, String filePath)
        throws IOException {
    if (DEBUG) {/*w  ww.j  av a 2 s .  c o  m*/
        System.out.println("Packing file " + fileToPack + " with " + fileToPack.length() + " bytes.");
    }

    final byte[] fileBytes = filePath.getBytes("UTF-8");
    writeIntLE(fileEntriesBaos, fileBytes.length);
    fileEntriesBaos.write(fileBytes);
    // TODO Complain with files larger than 4 bytes file length
    writeIntLE(fileEntriesBaos, (int) fileToPack.length());
    writeIntLE(fileEntriesBaos, (int) (fileToPack.lastModified() / 1000));

    final byte[] uncompressed = FileUtils.readFileToByteArray(fileToPack);
    if (DEBUG) {
        System.out.println("read " + uncompressed.length + " bytes from file.");
    }
    final ByteArrayOutputStream compressedStream = new ByteArrayOutputStream();
    //        final GZIPOutputStream gzipStream = new GZIPOutputStream(compressedStream);
    //        gzipStream.write(uncompressed);
    //        gzipStream.flush();
    final CRC32 checksum = new CRC32();
    checksum.update(uncompressed);
    final Deflater deflater = new Deflater(Deflater.DEFAULT_COMPRESSION, true);
    deflater.setInput(uncompressed);
    deflater.finish();
    final byte[] buf = new byte[Short.MAX_VALUE];
    while (!deflater.needsInput()) {
        final int bytesRead = deflater.deflate(buf);
        compressedStream.write(buf, 0, bytesRead);
    }

    final byte[] compressed = compressedStream.toByteArray();
    if (DEBUG) {
        System.out.println("compressed to " + compressed.length + " bytes.");
    }

    //        final Inflater decompresser = new Inflater();
    //        decompresser.setInput(compressed);
    //        byte[] result = new byte[5000];
    //        try {
    //            int resultLength = decompresser.inflate(result);
    //            final String str = new String(result, 0, resultLength);
    //            int i = 42;
    //        } catch (DataFormatException e) {
    //            // TODO Auto-generated catch block
    //            e.printStackTrace();
    //        }
    //        decompresser.end();

    compressedFilesBaos.write(compressed);
    writeIntLE(fileEntriesBaos, compressed.length);

    writeIntLE(fileEntriesBaos, checksum.getValue());

    // bits: 0x00001000, gzip
    fileEntriesBaos.write(0);
    fileEntriesBaos.write(0x10);
    fileEntriesBaos.write(0);
    fileEntriesBaos.write(0);

    // 0 bytes manifest
    writeIntLE(fileEntriesBaos, 0);
}

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 .j  a va2 s.  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.jwebsocket.util.Tools.java

/**
 * Deflate a byte array with Zip compression
 *
 * @param aUncompressedData The uncompressed data
 * @return The compressed data//from w  ww .jav a  2  s. c o m
 * @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.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);//  w  ww.  jav a2 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: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  ww  .j  a va  2  s .c o  m
    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:de.tudarmstadt.ukp.wikipedia.revisionmachine.difftool.data.codec.RevisionEncoder.java

@Override
public byte[] binaryDiff(final RevisionCodecData codecData, final Diff diff)
        throws UnsupportedEncodingException, EncodingException {

    byte[] bData = encode(codecData, diff);
    if (MODE_ZIP_COMPRESSION) {

        Deflater compresser = new Deflater();
        compresser.setInput(bData);/*from  ww w.  j av a2  s .  c  o m*/
        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) {
            return bData;
        } else {

            stream = new ByteArrayOutputStream();
            stream.write(new byte[] { -128 }, 0, 1);
            stream.write(output, 0, output.length);

            return stream.toByteArray();
        }
    }

    return bData;
}