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.atlassian.extras.decoder.v2.Version2LicenseDecoder.java

private Reader unzipText(byte[] licenseText) {
    ByteArrayInputStream in = new ByteArrayInputStream(licenseText);
    in.skip(LICENSE_PREFIX.length);//from  w w w .  ja  v  a 2 s.  co  m
    InflaterInputStream zipIn = new InflaterInputStream(in, new Inflater());
    try {
        return new InputStreamReader(zipIn, "UTF-8");
    } catch (UnsupportedEncodingException e) {
        throw new LicenseException(e);
    }
}

From source file:org.apache.fop.render.ps.ImageEncoderPNG.java

/** {@inheritDoc} */
public void writeTo(OutputStream out) throws IOException {
    // TODO: refactor this code with equivalent PDF code
    InputStream in = ((ImageRawStream) image).createInputStream();
    try {//from  ww w.j ava2s.c  o m
        if (numberOfInterleavedComponents == 1 || numberOfInterleavedComponents == 3) {
            // means we have Gray, RGB, or Palette
            IOUtils.copy(in, out);
        } else {
            // means we have Gray + alpha or RGB + alpha
            int numBytes = numberOfInterleavedComponents - 1; // 1 for Gray, 3 for RGB
            int numColumns = image.getSize().getWidthPx();
            InflaterInputStream infStream = new InflaterInputStream(in, new Inflater());
            DataInputStream dataStream = new DataInputStream(infStream);
            int offset = 0;
            int bytesPerRow = numberOfInterleavedComponents * numColumns;
            int filter;
            // here we need to inflate the PNG pixel data, which includes alpha, separate the alpha
            // channel and then deflate the RGB channels back again
            // TODO: not using the baos below and using the original out instead (as happens in PDF)
            // would be preferable but that does not work with the rest of the postscript code; this
            // needs to be revisited
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            DeflaterOutputStream dos = new DeflaterOutputStream(/* out */baos, new Deflater());
            while ((filter = dataStream.read()) != -1) {
                byte[] bytes = new byte[bytesPerRow];
                dataStream.readFully(bytes, 0, bytesPerRow);
                dos.write((byte) filter);
                for (int j = 0; j < numColumns; j++) {
                    dos.write(bytes, offset, numBytes);
                    offset += numberOfInterleavedComponents;
                }
                offset = 0;
            }
            dos.close();
            IOUtils.copy(new ByteArrayInputStream(baos.toByteArray()), out);
        }
    } finally {
        IOUtils.closeQuietly(in);
    }
}

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);//from  ww w .j  a  v  a  2 s  .  com
    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:io.gomint.server.network.packet.PacketLogin.java

@Override
public void deserialize(PacketBuffer buffer) {
    this.protocol = buffer.readInt();

    // Decompress inner data (i don't know why you compress inside of a Batched Packet but hey)
    byte[] compressed = new byte[buffer.readInt()];
    buffer.readBytes(compressed);/*from  w ww .  j a va 2s.co m*/

    Inflater inflater = new Inflater();
    inflater.setInput(compressed);

    ByteArrayOutputStream bout = new ByteArrayOutputStream();

    try {
        byte[] comBuffer = new byte[1024];
        while (!inflater.finished()) {
            int read = inflater.inflate(comBuffer);
            bout.write(comBuffer, 0, read);
        }
    } catch (DataFormatException e) {
        System.out.println("Failed to decompress batch packet" + e);
        return;
    }

    // More data please
    ByteBuffer byteBuffer = ByteBuffer.wrap(bout.toByteArray());
    byteBuffer.order(ByteOrder.LITTLE_ENDIAN);
    byte[] stringBuffer = new byte[byteBuffer.getInt()];
    byteBuffer.get(stringBuffer);

    // Decode the json stuff
    try {
        JSONObject jsonObject = (JSONObject) new JSONParser().parse(new String(stringBuffer));
        JSONArray chainArray = (JSONArray) jsonObject.get("chain");
        if (chainArray != null) {
            this.validationKey = parseBae64JSON((String) chainArray.get(chainArray.size() - 1)); // First key in chain is last response in chain #brainfuck :D
            for (Object chainObj : chainArray) {
                decodeBase64JSON((String) chainObj);
            }
        }
    } catch (ParseException e) {
        e.printStackTrace();
    }

    // Skin comes next
    this.skin = new byte[byteBuffer.getInt()];
    byteBuffer.get(this.skin);
}

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

public void decompress() {
    if (compressed) {
        Inflater decompressor = new Inflater();
        decompressor.setInput(data);/*www.  j  ava  2 s  . co 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:spartanfinal.ProcessFiles.java

public byte[] decompress(byte[] data) throws IOException, DataFormatException {
    Inflater inflater = new Inflater();
    inflater.setInput(data);//from   ww w  .  j a  v  a2 s  .c  o  m
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream(data.length);
    byte[] buffer = new byte[1024];
    while (!inflater.finished()) {
        int count = inflater.inflate(buffer);
        outputStream.write(buffer, 0, count);

    }
    outputStream.close();
    byte[] output = outputStream.toByteArray();

    return output;
}

From source file:com.vimukti.accounter.license.LicenseManager.java

private Reader unzipText(byte[] licenseText) {
    ByteArrayInputStream in = new ByteArrayInputStream(licenseText);
    InflaterInputStream zipIn = new InflaterInputStream(in, new Inflater());
    try {//from w  w w . j  a va  2s .co m
        return new InputStreamReader(zipIn, "UTF-8");
    } catch (UnsupportedEncodingException e) {
        throw new LicenseException(e);
    }

}

From source file:radixcore.network.ByteBufIO.java

/**
 * Decompresses a compressed byte array.
 * //from   www .j  a  va2s  .com
 * @param input The byte array to be decompressed.
 * @return The byte array in its decompressed, readable form.
 */
public static byte[] decompress(byte[] input) {
    try {
        final Inflater inflater = new Inflater();
        final ByteArrayOutputStream byteOutput = new ByteArrayOutputStream(input.length);
        final byte[] buffer = new byte[1024];
        inflater.setInput(input);

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

        inflater.end();
        byteOutput.close();
        return byteOutput.toByteArray();
    }

    catch (final DataFormatException e) {
        RadixExcept.logFatalCatch(e, "Error decompressing byte array.");
        return null;
    }

    catch (final IOException e) {
        RadixExcept.logFatalCatch(e, "Error decompressing byte array.");
        return null;
    }
}

From source file:org.jahia.utils.Url.java

/**
 * Decode facet filter URL parameter/*from   w ww .  j a v a 2  s  .com*/
 * @param inputString enocded facet filter URL query parameter
 * @return decoded facet filter parameter
 */
public static String decodeUrlParam(String inputString) {
    if (StringUtils.isEmpty(inputString)) {
        return inputString;
    }
    byte[] input = Base64.decodeBase64(inputString);
    // Decompress the bytes
    Inflater decompresser = new Inflater();
    decompresser.setInput(input, 0, input.length);
    byte[] result = new byte[2048];
    String outputString = "";
    try {
        int resultlength = decompresser.inflate(result);
        decompresser.end();
        outputString = new String(result, 0, resultlength, "UTF-8");
    } catch (DataFormatException e) {
        logger.warn("Not able to decode facet URL: " + inputString, e);
    } catch (UnsupportedEncodingException e) {
        logger.warn("Not able to decode facet URL: " + inputString, e);
    }
    return outputString;
}

From source file:Decoder.java

private Reader unzipText(byte[] licenseText) throws Exception {
    ByteArrayInputStream in = new ByteArrayInputStream(licenseText);
    in.skip(LICENSE_PREFIX.length);/*from  ww  w.ja  v  a 2  s  .c  o m*/
    InflaterInputStream zipIn = new InflaterInputStream(in, new Inflater());
    try {
        return new InputStreamReader(zipIn, "UTF-8");
    } catch (UnsupportedEncodingException e) {
        throw new Exception(e);
    }
}