Example usage for java.util.zip Inflater setInput

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

Introduction

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

Prototype

public void setInput(ByteBuffer input) 

Source Link

Document

Sets input data for decompression.

Usage

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

@Override
protected void readImpl() throws BufferUnderflowException, RuntimeException {

    this.ClientBuild = readD();
    skip(4);//  w w w  .ja v a 2s. c om
    this.accountName = readS();
    skip(4);
    this.clientSeed = readB(4);
    skip(20);
    this.digest = readB(20);

    if (getAvaliableBytes() < 4) {
        return;
    }

    final int UncopressedSize = readD();
    final byte[] compressedData = readB(getAvaliableBytes());
    final Inflater decompressor = new Inflater();
    decompressor.setInput(compressedData);

    final ByteArrayOutputStream bos = new ByteArrayOutputStream(compressedData.length);

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

    final byte[] decompressedData = bos.toByteArray();
    if (UncopressedSize != decompressedData.length) {
        logger.warn("Somesing wrong with compressed addonInfo");
        return;
    }
    final ChannelBuffer addonInfo = ChannelBuffers.wrappedBuffer(ByteOrder.LITTLE_ENDIAN, decompressedData);

    final int addonsCount = addonInfo.readInt();

    this.addonLists = new ArrayList<AddonInfo>(addonsCount);
    for (int i = 0; i < addonsCount; i++) {
        final TextBuilder tb = TextBuilder.newInstance();
        for (byte c; (c = addonInfo.readByte()) != 0;) {
            tb.append((char) c);
        }
        final String addonName = tb.toString();
        TextBuilder.recycle(tb);
        final byte enabled = addonInfo.readByte();
        final int crc = addonInfo.readInt();
        /* int unk1 = */addonInfo.readInt();
        this.addonLists.add(new AddonInfo(addonName, enabled, crc));
    }

    /* int unk2 = */addonInfo.readInt();
}

From source file:org.openteufel.file.mpq.MPQFileSector.java

public int getDecompressed(ByteBuffer out) throws DataFormatException, IOException {
    // If the file is encrypted, each sector (after compression/implosion, if applicable) is encrypted with the file's key.
    // Each sector is encrypted using the key + the 0-based index of the sector in the file.
    // NOTE compression type byte (if existing) is encrypted as well!
    ByteBuffer dataDecrypted;/*from w  w w. jav a 2s  . c  o  m*/
    if (this.encryptionSeed != null)
        dataDecrypted = MPQEncryptionUtils.decrypt(dataRaw, encryptionSeed);
    else
        dataDecrypted = dataRaw;
    dataDecrypted.rewind();

    switch (compression) {
    case Uncompressed: {
        out.put(dataDecrypted);
        return dataDecrypted.capacity();
    }
    case Imploded: {
        byte[] buf = new byte[sizeUncompressed];
        int numDecompressed = Exploder.pkexplode(dataDecrypted.array(), buf);
        if (numDecompressed != this.sizeUncompressed)
            throw new IllegalStateException();
        out.put(buf, 0, sizeUncompressed);
        return sizeUncompressed;
    }
    case ZLib: {
        int numDecompressed = 0;
        byte[] buf = new byte[1024];
        Inflater inflater = new Inflater();
        inflater.setInput(dataDecrypted.array());
        while (!inflater.finished()) {
            int decompressedBytes = inflater.inflate(buf);
            numDecompressed += decompressedBytes;
            out.put(buf, 0, decompressedBytes);
        }
        inflater.end();
        if (numDecompressed != this.sizeUncompressed)
            throw new IllegalStateException();
        return numDecompressed;
    }
    case BZip2: {
        int numDecompressed = 0;
        byte[] buf = new byte[1024];
        InputStream inputStream = new ByteArrayInputStream(dataDecrypted.array());
        BZip2CompressorInputStream uncompressStream = new BZip2CompressorInputStream(inputStream);
        while (true) {
            int decompressedBytes = uncompressStream.read(buf);
            if (decompressedBytes < 0)
                break;
            numDecompressed += decompressedBytes;
            out.put(buf, 0, decompressedBytes);
        }
        uncompressStream.close();
        inputStream.close();
        if (numDecompressed != sizeUncompressed)
            throw new IllegalStateException();
        return numDecompressed;
    }
    default:
        throw new IllegalStateException("Unknown Compression");
    }
}

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

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

        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) {

            }/*from  w w  w  .j  av a2  s  . co  m*/
        }
        try {
            bos.close();
        } catch (IOException e) {

        }

        data = bos.toByteArray();
        compressed = false;
    }
}

From source file:Comman.Tool.java

public byte[] Image_decompress(final byte[] data) {
    if (data == null || data.length == 0) {
        return new byte[0];
    }/*from   www. j a  v  a2 s .c om*/

    final Inflater inflater = new Inflater();
    inflater.setInput(data);

    try (final ByteArrayOutputStream out = new ByteArrayOutputStream(data.length)) {
        final byte[] buffer = new byte[1024];
        while (!inflater.finished()) {
            out.write(buffer, 0, inflater.inflate(buffer));
        }

        return out.toByteArray();
    } catch (final IOException | DataFormatException e) {
        System.err.println("Decompression failed! Returning the compressed data...");
        return data;
    }
}

From source file:spartanfinal.ProcessFiles.java

public byte[] decompress(byte[] data) throws IOException, DataFormatException {
    Inflater inflater = new Inflater();
    inflater.setInput(data);
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream(data.length);
    byte[] buffer = new byte[1024];
    while (!inflater.finished()) {
        int count = inflater.inflate(buffer);
        outputStream.write(buffer, 0, count);

    }//ww w . j a va2s . c o m
    outputStream.close();
    byte[] output = outputStream.toByteArray();

    return output;
}

From source file:org.apache.qpid.multiconsumer.AMQTest.java

private String inflateString(String string) throws Exception {
    byte[] input = string.getBytes();

    // First convert Base64 string back to binary array
    byte[] bytes = Base64.decodeBase64(input);

    // Set string as input data for decompressor
    Inflater decompressor = new Inflater();
    decompressor.setInput(bytes);

    // Decompress the data
    ByteArrayOutputStream bos = new ByteArrayOutputStream(input.length);
    byte[] buf = new byte[1024];
    while (!decompressor.finished()) {
        int count = decompressor.inflate(buf);
        bos.write(buf, 0, count);/*from  w  ww. j av  a  2 s .  com*/
    }
    bos.close();
    byte[] output = bos.toByteArray();

    // Get the decompressed data
    return new String(output, UTF8);
}

From source file:de.hofuniversity.iisys.neo4j.websock.query.encoding.safe.TSafeDeflateJsonQueryHandler.java

@Override
public boolean willDecode(ByteBuffer buff) {
    boolean valid = true;

    //TODO: actually check whether it's a query
    try {/*from w  ww .ja v a  2  s  .c  om*/
        //decompress
        final Inflater inflater = new Inflater(true);
        inflater.setInput(buff.array());

        int read = 0;
        int totalSize = 0;
        final List<byte[]> buffers = new LinkedList<byte[]>();

        byte[] buffer = new byte[BUFFER_SIZE];
        read = inflater.inflate(buffer);
        while (read > 0) {
            totalSize += read;
            buffers.add(buffer);
            buffer = new byte[BUFFER_SIZE];
            read = inflater.inflate(buffer);
        }

        final byte[] data = fuse(buffers, totalSize).array();
        new JSONObject(new String(data));
    } catch (Exception e) {
        valid = false;
    }

    return valid;
}

From source file:de.hofuniversity.iisys.neo4j.websock.query.encoding.safe.TSafeDeflateJsonQueryHandler.java

@Override
public WebsockQuery decode(ByteBuffer buff) throws DecodeException {
    WebsockQuery query = null;//from www .ja v a  2 s .co  m

    try {
        //decompress
        final byte[] incoming = buff.array();
        final Inflater inflater = new Inflater(true);
        inflater.setInput(incoming);

        int read = 0;
        int totalSize = 0;
        final List<byte[]> buffers = new LinkedList<byte[]>();

        final byte[] buffer = new byte[BUFFER_SIZE];
        read = inflater.inflate(buffer);
        while (read > 0) {
            totalSize += read;
            buffers.add(Arrays.copyOf(buffer, read));
            read = inflater.inflate(buffer);
        }

        final byte[] data = fuse(buffers, totalSize).array();
        final JSONObject obj = new JSONObject(new String(data));

        if (fDebug) {
            fTotalBytesIn += incoming.length;
            fLogger.log(Level.FINEST, "received compressed JSON message: " + incoming.length + " bytes\n"
                    + "total bytes received: " + fTotalBytesIn);
        }

        query = JsonConverter.fromJson(obj);
    } catch (Exception e) {
        e.printStackTrace();
        throw new DecodeException(buff, "failed to decode JSON", e);
    }

    return query;
}

From source file:nl.nn.adapterframework.util.JdbcUtil.java

public static String getBlobAsString(Blob blob, String column, String charset, boolean xmlEncode,
        boolean blobIsCompressed, boolean blobSmartGet, boolean encodeBlobBase64)
        throws IOException, JdbcException, SQLException, JMSException {
    if (encodeBlobBase64) {
        InputStream blobStream = JdbcUtil.getBlobInputStream(blob, column, blobIsCompressed);
        return Misc.streamToString(new Base64InputStream(blobStream, true), null, false);
    }/*from  ww  w . j a  v  a  2 s  .  c  o  m*/
    if (blobSmartGet) {
        if (blob == null) {
            log.debug("no blob found in column [" + column + "]");
            return null;
        }
        int bl = (int) blob.length();

        InputStream is = blob.getBinaryStream();
        byte[] buf = new byte[bl];
        int bl1 = is.read(buf);

        Inflater decompressor = new Inflater();
        decompressor.setInput(buf);
        ByteArrayOutputStream bos = new ByteArrayOutputStream(buf.length);
        byte[] bufDecomp = new byte[1024];
        boolean decompresOK = true;
        while (!decompressor.finished()) {
            try {
                int count = decompressor.inflate(bufDecomp);
                if (count == 0) {
                    break;
                }
                bos.write(bufDecomp, 0, count);
            } catch (DataFormatException e) {
                log.debug("message in column [" + column + "] is not compressed");
                decompresOK = false;
                break;
            }
        }
        bos.close();
        if (decompresOK)
            buf = bos.toByteArray();

        Object result = null;
        ObjectInputStream ois = null;
        boolean objectOK = true;
        try {
            ByteArrayInputStream bis = new ByteArrayInputStream(buf);
            ois = new ObjectInputStream(bis);
            result = ois.readObject();
        } catch (Exception e) {
            log.debug("message in column [" + column + "] is probably not a serialized object: "
                    + e.getClass().getName());
            objectOK = false;
        }
        if (ois != null)
            ois.close();

        String rawMessage;
        if (objectOK) {
            if (result instanceof IMessageWrapper) {
                rawMessage = ((IMessageWrapper) result).getText();
            } else if (result instanceof TextMessage) {
                rawMessage = ((TextMessage) result).getText();
            } else {
                rawMessage = (String) result;
            }
        } else {
            rawMessage = new String(buf, charset);
        }

        String message = XmlUtils.encodeCdataString(rawMessage);
        return message;
    }
    return Misc.readerToString(getBlobReader(blob, column, charset, blobIsCompressed), null, xmlEncode);
}