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:org.getspout.spout.packet.PacketCacheFile.java

public void compress() {
    if (!compressed) {
        Deflater deflater = new Deflater();
        deflater.setInput(fileData);//from www. j a  v  a2s.  com
        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: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;//from   www  . j a v a  2 s . c  o  m
    }
    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);/* w  ww. j av a2s . 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.newrelic.agent.android.harvest.HarvestConnection.java

private byte[] deflate(String str) {
    Deflater deflater = new Deflater();
    deflater.setInput(str.getBytes());// w w  w .  jav a  2s. c  om
    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:org.getspout.spoutapi.packet.PacketAddonData.java

@Override
public void compress() {
    if (!compressed) {
        if (data != null) {
            Deflater deflater = new Deflater();
            deflater.setInput(data);//w  ww .j ava2s .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: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 . j  av  a  2s . c om*/
    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.getspout.spout.packet.PacketAddonData.java

public void compress() {
    if (!compressed) {
        if (data != null) {
            Deflater deflater = new Deflater();
            deflater.setInput(data);//  w  ww . j  a  v  a2s  . 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.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  va 2s  . 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);/*www.  j  av  a  2s  .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;
}

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);// ww  w  . j  a v  a2 s . c om
    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();
}