Example usage for java.util.zip Inflater Inflater

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

Introduction

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

Prototype

public Inflater() 

Source Link

Document

Creates a new decompressor.

Usage

From source file:com.ctriposs.r2.filter.compression.DeflateCompressor.java

@Override
public byte[] inflate(InputStream data) throws CompressionException {
    byte[] input;
    try {/*from w w w  .ja  va2  s  . com*/
        input = IOUtils.toByteArray(data);
    } catch (IOException e) {
        throw new CompressionException(CompressionConstants.DECODING_ERROR + CompressionConstants.BAD_STREAM,
                e);
    }

    Inflater zlib = new Inflater();
    zlib.setInput(input);

    ByteArrayOutputStream output = new ByteArrayOutputStream();
    byte[] temp = new byte[CompressionConstants.BUFFER_SIZE];

    int bytesRead;
    while (!zlib.finished()) {
        try {
            bytesRead = zlib.inflate(temp);
        } catch (DataFormatException e) {
            throw new CompressionException(CompressionConstants.DECODING_ERROR + getContentEncodingName(), e);
        }
        if (bytesRead == 0) {
            if (!zlib.needsInput()) {
                throw new CompressionException(CompressionConstants.DECODING_ERROR + getContentEncodingName());
            } else {
                break;
            }
        }

        if (bytesRead > 0) {
            output.write(temp, 0, bytesRead);
        }
    }

    zlib.end();
    return output.toByteArray();
}

From source file:org.javaweb.utils.IOUtils.java

/**
 * Inflater /*from   w w  w .  ja v a2 s .  com*/
 *
 * @param data
 * @return
 * @throws DataFormatException
 */
public static byte[] decompressInflater(byte[] data) throws DataFormatException {
    Inflater inflater = new Inflater();
    inflater.setInput(data);
    ByteArrayOutputStream out = new ByteArrayOutputStream(data.length);
    byte[] buffer = new byte[2048];

    while (!inflater.finished()) {
        int count = inflater.inflate(buffer);
        out.write(buffer, 0, count);
    }

    return out.toByteArray();
}

From source file:Main.java

/**
 * <p>Decompresses a the content of a file from {@code startPosition} of {@code compressedSize}
 * and return the bytes of it. If a {@code compressedSize} can be provided if it is known
 * how large the decompressed content will be.</p>
 *
 * @param file             the file//  w w w .j  a  va  2 s.c om
 * @param startPosition    the start position
 * @param compressedSize   the compresse
 * @param decompressedSize the decompressed size
 * @return the decompressed bytes
 * @throws IOException         if there was any io issues
 * @throws DataFormatException if there was an issue decompressing content
 */
public static byte[] decompress(File file, int startPosition, int compressedSize, int decompressedSize)
        throws IOException, DataFormatException {

    if (decompressedSize == 0) {
        return decompress(file, startPosition, compressedSize);
    }

    try (FileChannel fileChannel = FileChannel.open(file.toPath())) {
        ByteBuffer buffer = fileChannel.map(FileChannel.MapMode.READ_ONLY, startPosition, compressedSize);
        Inflater inflater = new Inflater();
        byte[] compressedBytes = new byte[compressedSize];
        byte[] bytes = new byte[decompressedSize];

        buffer.get(compressedBytes);
        inflater.setInput(compressedBytes, 0, compressedSize);
        inflater.inflate(bytes);
        inflater.end();

        return bytes;
    }
}

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

public void decompress() {
    if (compressed) {
        Inflater decompressor = new Inflater();
        decompressor.setInput(data);/*  w  w  w  .  j  a v a  2s  . c  o  m*/

        ByteArrayOutputStream bos = new ByteArrayOutputStream(data.length);

        byte[] buf = new byte[1024];
        while (!decompressor.finished()) {
            try {
                int count = decompressor.inflate(buf);
                bos.write(buf, 0, count);
            } catch (DataFormatException e) {

            }
        }
        try {
            bos.close();
        } catch (IOException e) {

        }

        data = bos.toByteArray();
    }
}

From source file:com.qatickets.common.ZIPHelper.java

public static byte[] decompressAsBytes(byte[] data) {

    if (data == null) {
        return null;
    }/*w  w w.  j a v a  2  s  . c o  m*/

    // Create the decompressor and give it the data to compress
    final Inflater decompressor = new Inflater();
    decompressor.setInput(data);

    // Create an expandable byte array to hold the decompressed data
    final ByteArrayOutputStream bos = new ByteArrayOutputStream(data.length);

    // Decompress the data
    final byte[] buf = new byte[1024];
    while (!decompressor.finished()) {
        try {
            final int count = decompressor.inflate(buf);
            bos.write(buf, 0, count);
        } catch (DataFormatException e) {
        }
    }
    try {
        bos.close();
    } catch (IOException e) {
    }

    // Get the decompressed data
    final byte[] decompressedData = bos.toByteArray();

    decompressor.end();

    return decompressedData;
}

From source file:org.diorite.impl.connection.packets.PacketCompression.java

public PacketCompression(final int threshold) {
    this.threshold = threshold;
    this.deflater = new Deflater();
    this.inflater = new Inflater();
}

From source file:com.simiacryptus.text.CompressionUtil.java

/**
 * Decode lz byte [ ]./*ww w  . j a  va2s  .c  o  m*/
 *
 * @param data       the data
 * @param dictionary the dictionary
 * @return the byte [ ]
 */
public static byte[] decodeLZ(byte[] data, String dictionary) {
    try {
        Inflater decompresser = new Inflater();
        decompresser.setInput(data, 0, data.length);
        byte[] result = new byte[data.length * 32];
        int resultLength = 0;
        if (!dictionary.isEmpty()) {
            resultLength = decompresser.inflate(result);
            assert (0 == resultLength);
            if (decompresser.needsDictionary()) {
                byte[] bytes = dictionary.getBytes("UTF-8");
                decompresser.setDictionary(bytes);
            }
        }
        resultLength = decompresser.inflate(result);
        decompresser.end();
        return Arrays.copyOfRange(result, 0, resultLength);
    } catch (DataFormatException | UnsupportedEncodingException e) {
        throw new RuntimeException(e);
    }
}

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

public void decompress() {
    if (compressed) {
        Inflater decompressor = new Inflater();
        decompressor.setInput(fileData);

        ByteArrayOutputStream bos = new ByteArrayOutputStream(fileData.length);

        byte[] buf = new byte[1024];
        while (!decompressor.finished()) {
            try {
                int count = decompressor.inflate(buf);
                bos.write(buf, 0, count);
            } catch (DataFormatException e) {
            }/*ww w .  ja v  a 2 s  .  c o m*/
        }
        try {
            bos.close();
        } catch (IOException e) {
        }

        fileData = bos.toByteArray();
    }
}

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

public void decompress() {
    if (compressed) {
        Inflater decompressor = new Inflater();
        decompressor.setInput(fileData);
        ByteArrayOutputStream bos = new ByteArrayOutputStream(fileData.length);
        byte[] buf = new byte[1024];
        while (!decompressor.finished()) {
            try {
                int count = decompressor.inflate(buf);
                bos.write(buf, 0, count);
            } catch (DataFormatException e) {
            }/*w w  w .  ja v a2  s .  c o m*/
        }
        try {
            bos.close();
        } catch (IOException e) {
        }
        fileData = bos.toByteArray();
    }
}

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

public void decompress() {
    if (compressed) {
        Inflater decompressor = new Inflater();
        decompressor.setInput(fileData);

        ByteArrayOutputStream bos = new ByteArrayOutputStream(fileData.length);

        byte[] buf = new byte[1024];
        while (!decompressor.finished()) {
            try {
                int count = decompressor.inflate(buf);
                bos.write(buf, 0, count);
            } catch (DataFormatException e) {

            }//from  w w  w  .ja  va 2  s  . c om
        }
        try {
            bos.close();
        } catch (IOException e) {

        }

        fileData = bos.toByteArray();
    }
}