Search in sources :

Example 91 with Inflater

use of java.util.zip.Inflater in project eiger by wlloyd.

the class CassandraServer method uncompress.

private static String uncompress(ByteBuffer query, Compression compression) throws InvalidRequestException {
    String queryString = null;
    // Decompress the query string.
    try {
        switch(compression) {
            case GZIP:
                FastByteArrayOutputStream byteArray = new FastByteArrayOutputStream();
                byte[] outBuffer = new byte[1024], inBuffer = new byte[1024];
                Inflater decompressor = new Inflater();
                int lenRead = 0;
                while (true) {
                    if (decompressor.needsInput())
                        lenRead = query.remaining() < 1024 ? query.remaining() : 1024;
                    query.get(inBuffer, 0, lenRead);
                    decompressor.setInput(inBuffer, 0, lenRead);
                    int lenWrite = 0;
                    while ((lenWrite = decompressor.inflate(outBuffer)) != 0) byteArray.write(outBuffer, 0, lenWrite);
                    if (decompressor.finished())
                        break;
                }
                decompressor.end();
                queryString = new String(byteArray.toByteArray(), 0, byteArray.size(), "UTF-8");
                break;
            case NONE:
                try {
                    queryString = ByteBufferUtil.string(query);
                } catch (CharacterCodingException ex) {
                    throw new InvalidRequestException(ex.getMessage());
                }
                break;
        }
    } catch (DataFormatException e) {
        throw new InvalidRequestException("Error deflating query string.");
    } catch (UnsupportedEncodingException e) {
        throw new InvalidRequestException("Unknown query string encoding.");
    }
    return queryString;
}
Also used : FastByteArrayOutputStream(org.apache.cassandra.io.util.FastByteArrayOutputStream) DataFormatException(java.util.zip.DataFormatException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) Inflater(java.util.zip.Inflater) CharacterCodingException(java.nio.charset.CharacterCodingException)

Example 92 with Inflater

use of java.util.zip.Inflater in project intellij-community by JetBrains.

the class JBZipEntry method getInputStream.

private InputStream getInputStream() throws IOException {
    long start = calcDataOffset();
    BoundedInputStream bis = new BoundedInputStream(start, getCompressedSize());
    switch(getMethod()) {
        case ZipEntry.STORED:
            return bis;
        case ZipEntry.DEFLATED:
            bis.addDummy();
            return new InflaterInputStream(bis, new Inflater(true));
        default:
            throw new ZipException("Found unsupported compression method " + getMethod());
    }
}
Also used : InflaterInputStream(java.util.zip.InflaterInputStream) Inflater(java.util.zip.Inflater) ZipException(java.util.zip.ZipException)

Example 93 with Inflater

use of java.util.zip.Inflater in project ABPlayer by winkstu.

the class CompressionTools method decompress.

public static byte[] decompress(byte[] value) throws DataFormatException {
    ByteArrayOutputStream bos = new ByteArrayOutputStream(value.length);
    Inflater decompressor = new Inflater();
    try {
        decompressor.setInput(value);
        final byte[] buf = new byte[1024];
        while (!decompressor.finished()) {
            int count = decompressor.inflate(buf);
            bos.write(buf, 0, count);
        }
    } finally {
        decompressor.end();
    }
    return bos.toByteArray();
}
Also used : Inflater(java.util.zip.Inflater) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 94 with Inflater

use of java.util.zip.Inflater in project ABPlayer by winkstu.

the class CompressionTools method decompressXML.

public static byte[] decompressXML(byte[] data) throws DataFormatException {
    byte[] dest = new byte[data.length + 2];
    System.arraycopy(data, 0, dest, 2, data.length);
    dest[0] = 0x78;
    dest[1] = 0x01;
    data = dest;
    Inflater decompresser = new Inflater();
    decompresser.setInput(data);
    byte[] bufferArray = new byte[1024];
    ByteArrayOutputStream baos = new ByteArrayOutputStream(1024);
    try {
        int i = 1;
        while (i != 0) {
            i = decompresser.inflate(bufferArray);
            baos.write(bufferArray, 0, i);
        }
        data = baos.toByteArray();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            baos.flush();
            baos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    decompresser.end();
    return data;
}
Also used : Inflater(java.util.zip.Inflater) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) DataFormatException(java.util.zip.DataFormatException) IOException(java.io.IOException)

Example 95 with Inflater

use of java.util.zip.Inflater in project OpenAM by OpenRock.

the class SAML2Utils method decodeFromRedirect.

/**
     * Decodes the request message.
     *
     * @param str String to be decoded.
     * @return String the decoded String.
     */
public static String decodeFromRedirect(final String str) {
    final String classMethod = "SAML2Utils.decodeFromRedirect: ";
    if (StringUtils.isEmpty(str)) {
        debug.error(classMethod + "input is null.");
        return null;
    }
    if (debug.messageEnabled()) {
        debug.message(classMethod + "input string length : " + str.length());
        debug.message(classMethod + "input string is ===>" + str + "<===");
    }
    byte[] input = Base64.decode(removeNewLineChars(str));
    if (input == null || input.length == 0) {
        debug.error(classMethod + "Base64 decoded result is null");
        return null;
    }
    // From the Inflater JavaDoc:
    // Note: When using the 'nowrap' option it is also necessary to provide an extra "dummy" byte as input.
    // This is required by the ZLIB native library in order to support certain optimizations.
    input = Arrays.copyOf(input, input.length + 1);
    int bufferLength = 2048;
    try {
        if (bufferLen != null && !bufferLen.isEmpty()) {
            bufferLength = Integer.parseInt(bufferLen);
        }
    } catch (NumberFormatException nfe) {
        debug.error(classMethod + "Unable to parse buffer length.", nfe);
    }
    // Decompress the bytes
    Inflater inflater = new Inflater(true);
    InflaterInputStream inflaterInputStream = new InflaterInputStream(new ByteArrayInputStream(input), inflater);
    ByteArrayOutputStream bout = new ByteArrayOutputStream(bufferLength);
    try {
        int b = inflaterInputStream.read();
        while (b != -1) {
            bout.write(b);
            b = inflaterInputStream.read();
        }
    } catch (IOException e) {
        debug.error(classMethod + "There was a problem reading the compressed input", e);
        return null;
    } finally {
        IOUtils.closeIfNotNull(inflaterInputStream);
    }
    String result;
    try {
        result = bout.toString("UTF-8");
    } catch (UnsupportedEncodingException uee) {
        debug.error(classMethod + "cannot convert byte array to string.", uee);
        return null;
    }
    if (debug.messageEnabled()) {
        debug.message(classMethod + "Return value: \n" + result);
    }
    return result;
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) InflaterInputStream(java.util.zip.InflaterInputStream) UnsupportedEncodingException(java.io.UnsupportedEncodingException) Inflater(java.util.zip.Inflater) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException)

Aggregations

Inflater (java.util.zip.Inflater)108 InflaterInputStream (java.util.zip.InflaterInputStream)36 IOException (java.io.IOException)33 ByteArrayOutputStream (java.io.ByteArrayOutputStream)31 DataFormatException (java.util.zip.DataFormatException)30 InputStream (java.io.InputStream)24 ByteArrayInputStream (java.io.ByteArrayInputStream)14 GZIPInputStream (java.util.zip.GZIPInputStream)14 Deflater (java.util.zip.Deflater)9 Test (org.junit.Test)7 DataInputStream (java.io.DataInputStream)6 OutputStream (java.io.OutputStream)6 BufferedInputStream (java.io.BufferedInputStream)5 BufferedReader (java.io.BufferedReader)5 InputStreamReader (java.io.InputStreamReader)5 HttpURLConnection (java.net.HttpURLConnection)5 ByteBuffer (java.nio.ByteBuffer)5 JSONObject (org.json.JSONObject)5 DataOutputStream (java.io.DataOutputStream)4 OutputStreamWriter (java.io.OutputStreamWriter)4