Example usage for java.util.zip Inflater end

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

Introduction

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

Prototype

public void end() 

Source Link

Document

Closes the decompressor and discards any unprocessed input.

Usage

From source file:divconq.ctp.stream.UngzipStream.java

@Override
public void close() {
    Inflater inf = this.inflater;

    if (inf != null)
        inf.end();

    this.inflater = null;

    ByteBuf rem = this.remnant;

    if (rem != null) {
        rem.release();// w  w w . ja va  2 s.  co  m

        this.remnant = null;
    }

    // not truly thread safe, consider
    for (ByteBuf bb : this.outlist)
        bb.release();

    this.outlist.clear();

    super.close();
}

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;/*w  w  w .  ja v a2  s . co 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:acp.sdk.SecureUtil.java

/**
 * .// ww  w. ja v a  2s  .  c  o m
 * 
 * @param inputByte
 *            byte[]?
 * @return ??
 * @throws IOException
 */
public static byte[] inflater(final byte[] inputByte) throws IOException {
    int compressedDataLength = 0;
    Inflater compresser = new Inflater(false);
    compresser.setInput(inputByte, 0, inputByte.length);
    ByteArrayOutputStream o = new ByteArrayOutputStream(inputByte.length);
    byte[] result = new byte[1024];
    try {
        while (!compresser.finished()) {
            compressedDataLength = compresser.inflate(result);
            if (compressedDataLength == 0) {
                break;
            }
            o.write(result, 0, compressedDataLength);
        }
    } catch (Exception ex) {
        System.err.println("Data format error!\n");
        ex.printStackTrace();
    } finally {
        o.close();
    }
    compresser.end();
    return o.toByteArray();
}

From source file:org.eclipsetrader.directa.internal.core.connector.BackfillConnector.java

protected void parseIntradayStream(BufferedInputStream in, List<OHLC> list) throws Exception {
    Calendar cal = Calendar.getInstance();

    int startTime = 9 * 60;
    int endTime = 17 * 60 + 25;
    ;/*from   w  w  w .  j  a  va2s  . c o m*/

    byte[] buffer = new byte[1];
    while (in.read(buffer) == 1) {
        if (buffer[0] == '<') {
            StringBuilder sb = new StringBuilder();
            while (in.read(buffer) == 1) {
                if (buffer[0] == '>') {
                    break;
                }
                sb.append(new String(buffer));
            }
            String line = sb.toString();
            if (line.startsWith("GRA")) { //$NON-NLS-1$
                int s = line.indexOf("L=") + 2; //$NON-NLS-1$
                int e = line.indexOf(" ", s); //$NON-NLS-1$
                int uncompressLen = Integer.parseInt(line.substring(s, e));

                byte[] output = new byte[uncompressLen];
                boolean compressed = line.indexOf("LC=") != -1; //$NON-NLS-1$

                if (compressed) {
                    s = line.indexOf("LC=") + 3; //$NON-NLS-1$
                    e = line.indexOf(" ", s); //$NON-NLS-1$
                    int compressLen = Integer.parseInt(line.substring(s, e));

                    while (in.read(buffer) == 1) {
                        if (buffer[0] == 0x78) {
                            break;
                        }
                    }
                    if (buffer[0] != 0x78) {
                        break;
                    }

                    int readed = 1, len;
                    byte[] input = new byte[compressLen];
                    input[0] = buffer[0];
                    do {
                        len = in.read(input, readed, input.length - readed);
                        readed += len;
                    } while (len > 0 && readed < input.length);

                    Inflater infl = new Inflater();
                    infl.setInput(input, 0, readed);
                    infl.inflate(output);
                    infl.end();
                } else {
                    in.read(buffer);

                    int readed = 0, len;
                    do {
                        len = in.read(output, readed, output.length - readed);
                        readed += len;
                    } while (len > 0 && readed < output.length);
                }

                for (int i = 0; i < output.length; i += 28) {
                    Date date = getDate(output, i);
                    cal.setTime(date);
                    int time = cal.get(Calendar.HOUR_OF_DAY) * 60 + cal.get(Calendar.MINUTE);
                    if (time >= startTime && time <= endTime) {
                        float low = getFloat(output, i + 8);
                        float high = getFloat(output, i + 12);
                        float close = getFloat(output, i + 16);
                        float volume = getFloat(output, i + 20);
                        float open = getFloat(output, i + 24);
                        list.add(new OHLC(date, (double) open, (double) high, (double) low, (double) close,
                                (long) volume));
                    }
                }
            }
        }
    }
}

From source file:org.wso2.carbon.identity.sso.saml.util.SAMLSSOUtil.java

/**
 * Decoding and deflating the encoded AuthReq
 *
 * @param encodedStr encoded AuthReq//  w w w  . j  ava 2s  .  c  om
 * @return decoded AuthReq
 */
public static String decode(String encodedStr) throws IdentityException {
    try {
        org.apache.commons.codec.binary.Base64 base64Decoder = new org.apache.commons.codec.binary.Base64();
        byte[] xmlBytes = encodedStr.getBytes("UTF-8");
        byte[] base64DecodedByteArray = base64Decoder.decode(xmlBytes);

        try {
            Inflater inflater = new Inflater(true);
            inflater.setInput(base64DecodedByteArray);
            byte[] xmlMessageBytes = new byte[5000];
            int resultLength = inflater.inflate(xmlMessageBytes);

            if (inflater.getRemaining() > 0) {
                throw new RuntimeException("didn't allocate enough space to hold " + "decompressed data");
            }

            inflater.end();
            String decodedString = new String(xmlMessageBytes, 0, resultLength, "UTF-8");
            if (log.isDebugEnabled()) {
                log.debug("Request message " + decodedString);
            }
            return decodedString;

        } catch (DataFormatException e) {
            ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(base64DecodedByteArray);
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            InflaterInputStream iis = new InflaterInputStream(byteArrayInputStream);
            byte[] buf = new byte[1024];
            int count = iis.read(buf);
            while (count != -1) {
                byteArrayOutputStream.write(buf, 0, count);
                count = iis.read(buf);
            }
            iis.close();
            String decodedStr = new String(byteArrayOutputStream.toByteArray(), StandardCharsets.UTF_8);
            if (log.isDebugEnabled()) {
                log.debug("Request message " + decodedStr, e);
            }
            return decodedStr;
        }
    } catch (IOException e) {
        throw new IdentityException("Error when decoding the SAML Request.", e);
    }

}

From source file:com.alibaba.citrus.service.requestcontext.session.encoder.AbstractSerializationEncoder.java

/** ? */
public Map<String, Object> decode(String encodedValue, StoreContext storeContext)
        throws SessionEncoderException {
    // 1. base64?
    byte[] cryptotext = null;

    try {/*  w  w w .  ja  v a  2  s .  c o m*/
        encodedValue = URLDecoder.decode(assertNotNull(encodedValue, "encodedValue is null"), "ISO-8859-1");
        cryptotext = Base64.decodeBase64(encodedValue.getBytes("ISO-8859-1"));

        if (isEmptyArray(cryptotext)) {
            throw new SessionEncoderException("Session state is empty: " + encodedValue);
        }
    } catch (Exception e) {
        throw new SessionEncoderException("Failed to decode session state: ", e);
    }

    // 2. 
    byte[] plaintext = decrypt(cryptotext);

    if (isEmptyArray(plaintext)) {
        throw new SessionEncoderException("Decrypted session state is empty: " + encodedValue);
    }

    // 3. 
    ByteArrayInputStream bais = new ByteArrayInputStream(plaintext);
    Inflater inf = new Inflater(false);
    InflaterInputStream iis = new InflaterInputStream(bais, inf);

    // 4. ???
    try {
        @SuppressWarnings("unchecked")
        Map<String, Object> attrs = (Map<String, Object>) serializer.deserialize(iis);
        return attrs;
    } catch (Exception e) {
        throw new SessionEncoderException("Failed to parse session state", e);
    } finally {
        try {
            iis.close();
        } catch (IOException e) {
        }

        inf.end();
    }
}

From source file:org.ajax4jsf.resource.ResourceBuilderImpl.java

protected byte[] decrypt(byte[] src) {
    try {//from  www .j  a  v  a 2s  .  co  m
        byte[] zipsrc = codec.decode(src);
        Inflater decompressor = new Inflater();
        byte[] uncompressed = new byte[zipsrc.length * 5];
        decompressor.setInput(zipsrc);
        int totalOut = decompressor.inflate(uncompressed);
        byte[] out = new byte[totalOut];
        System.arraycopy(uncompressed, 0, out, 0, totalOut);
        decompressor.end();
        return out;
    } catch (Exception e) {
        throw new FacesException("Error decode resource data", e);
    }
}

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

public byte[] decompress(byte[] compressed, int bytesToSkip) {
    Inflater decompressor = new Inflater();
    decompressor.setInput(compressed, bytesToSkip, compressed.length - bytesToSkip);

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

    // Decompress the data 
    byte[] buf = new byte[1024];
    while (!decompressor.finished()) {
        try {/*from   w ww .j  a va  2  s .  c o m*/
            int count = decompressor.inflate(buf);
            bos.write(buf, 0, count);
        } catch (DataFormatException e) {
            decompressor.end();
            return null;
        }
    }
    decompressor.end();
    try {
        if (bos != null)
            bos.close();
    } catch (IOException e) {
        MBLog.w(TAG, "Unable to close stream");
    }

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

From source file:servlets.ProcessResponseServlet.java

private String decodeAuthnRequestXML(String encodedRequestXmlString) throws SamlException {
    try {//w  w w  . j av a  2  s.  c om
        // URL decode
        // No need to URL decode: auto decoded by request.getParameter() method

        // Base64 decode
        Base64 base64Decoder = new Base64();
        byte[] xmlBytes = encodedRequestXmlString.getBytes("UTF-8");
        byte[] base64DecodedByteArray = base64Decoder.decode(xmlBytes);

        //Uncompress the AuthnRequest data
        //First attempt to unzip the byte array according to DEFLATE (rfc 1951)
        try {

            Inflater inflater = new Inflater(true);
            inflater.setInput(base64DecodedByteArray);
            // since we are decompressing, it's impossible to know how much space we
            // might need; hopefully this number is suitably big
            byte[] xmlMessageBytes = new byte[5000];
            int resultLength = inflater.inflate(xmlMessageBytes);

            if (!inflater.finished()) {
                throw new RuntimeException("didn't allocate enough space to hold " + "decompressed data");
            }

            inflater.end();
            return new String(xmlMessageBytes, 0, resultLength, "UTF-8");

        } catch (DataFormatException e) {

            // if DEFLATE fails, then attempt to unzip the byte array according to
            // zlib (rfc 1950)      
            ByteArrayInputStream bais = new ByteArrayInputStream(base64DecodedByteArray);
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            InflaterInputStream iis = new InflaterInputStream(bais);
            byte[] buf = new byte[1024];
            int count = iis.read(buf);
            while (count != -1) {
                baos.write(buf, 0, count);
                count = iis.read(buf);
            }
            iis.close();
            return new String(baos.toByteArray());
        }

    } catch (UnsupportedEncodingException e) {
        throw new SamlException("Error decoding AuthnRequest: " + "Check decoding scheme - " + e.getMessage());
    } catch (IOException e) {
        throw new SamlException("Error decoding AuthnRequest: " + "Check decoding scheme - " + e.getMessage());
    }
}

From source file:MSUmpire.SpectrumParser.mzXMLReadUnit.java

public byte[] ZlibUncompressBuffer(byte[] compressed) throws IOException, DataFormatException {

    Inflater decompressor = new Inflater();
    decompressor.setInput(compressed);//  w w  w  . j a  v  a  2 s . c  o  m

    ByteArrayOutputStream bos = null;
    try {

        bos = new ByteArrayOutputStream(compressed.length);

        // Decompress the data
        byte[] buf = new byte[decompressor.getRemaining() * 2];
        while (decompressor.getRemaining() > 0) {
            int count = decompressor.inflate(buf);
            bos.write(buf, 0, count);
        }

    } finally {
        try {
            bos.close();
        } catch (Exception nope) {
            /* This exception doesn't matter */ }
    }
    decompressor.end();
    compressed = null;
    decompressor = null;
    byte[] result = bos.toByteArray();
    bos = null;
    return result;
}