Search in sources :

Example 11 with Inflater

use of java.util.zip.Inflater in project c-geo by just-radovan.

the class cgBase method requestJSON.

public String requestJSON(String scheme, String host, String path, String method, String params) {
    int httpCode = -1;
    String httpLocation = null;
    if (method == null) {
        method = "GET";
    } else {
        method = method.toUpperCase();
    }
    boolean methodPost = false;
    if (method.equalsIgnoreCase("POST")) {
        methodPost = true;
    }
    URLConnection uc = null;
    HttpURLConnection connection = null;
    Integer timeout = 30000;
    final StringBuffer buffer = new StringBuffer();
    for (int i = 0; i < 3; i++) {
        if (i > 0) {
            Log.w(cgSettings.tag, "Failed to download data, retrying. Attempt #" + (i + 1));
        }
        buffer.delete(0, buffer.length());
        timeout = 30000 + (i * 15000);
        try {
            try {
                URL u = null;
                if (methodPost) {
                    u = new URL(scheme + host + path);
                } else {
                    u = new URL(scheme + host + path + "?" + params);
                }
                if (u.getProtocol().toLowerCase().equals("https")) {
                    trustAllHosts();
                    HttpsURLConnection https = (HttpsURLConnection) u.openConnection();
                    https.setHostnameVerifier(doNotVerify);
                    uc = https;
                } else {
                    uc = (HttpURLConnection) u.openConnection();
                }
                uc.setRequestProperty("Host", host);
                uc.setRequestProperty("Accept", "application/json, text/javascript, */*; q=0.01");
                if (methodPost) {
                    uc.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
                    uc.setRequestProperty("Content-Length", Integer.toString(params.length()));
                    uc.setRequestProperty("X-HTTP-Method-Override", "GET");
                } else {
                    uc.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
                }
                uc.setRequestProperty("X-Requested-With", "XMLHttpRequest");
                connection = (HttpURLConnection) uc;
                connection.setReadTimeout(timeout);
                connection.setRequestMethod(method);
                // TODO: Fix these (FilCab)
                HttpURLConnection.setFollowRedirects(false);
                connection.setDoInput(true);
                if (methodPost) {
                    connection.setDoOutput(true);
                    final OutputStream out = connection.getOutputStream();
                    final OutputStreamWriter wr = new OutputStreamWriter(out);
                    wr.write(params);
                    wr.flush();
                    wr.close();
                } else {
                    connection.setDoOutput(false);
                }
                final String encoding = connection.getContentEncoding();
                InputStream ins;
                if (encoding != null && encoding.equalsIgnoreCase("gzip")) {
                    ins = new GZIPInputStream(connection.getInputStream());
                } else if (encoding != null && encoding.equalsIgnoreCase("deflate")) {
                    ins = new InflaterInputStream(connection.getInputStream(), new Inflater(true));
                } else {
                    ins = connection.getInputStream();
                }
                final InputStreamReader inr = new InputStreamReader(ins);
                final BufferedReader br = new BufferedReader(inr);
                readIntoBuffer(br, buffer);
                httpCode = connection.getResponseCode();
                final String paramsLog = params.replaceAll(passMatch, "password=***");
                Log.i(cgSettings.tag + " | JSON", "[POST " + (int) (params.length() / 1024) + "k | " + httpCode + " | " + (int) (buffer.length() / 1024) + "k] Downloaded " + "http://" + host + path + "?" + paramsLog);
                connection.disconnect();
                br.close();
                ins.close();
                inr.close();
            } catch (IOException e) {
                httpCode = connection.getResponseCode();
                Log.e(cgSettings.tag, "cgeoBase.requestJSON.IOException: " + httpCode + ": " + connection.getResponseMessage() + " ~ " + e.toString());
            }
        } catch (Exception e) {
            Log.e(cgSettings.tag, "cgeoBase.requestJSON: " + e.toString());
        }
        if (buffer != null && buffer.length() > 0) {
            break;
        }
        if (httpCode == 403) {
            // we're not allowed to download content, so let's move
            break;
        }
    }
    String page = null;
    if (httpCode == 302 && httpLocation != null) {
        final Uri newLocation = Uri.parse(httpLocation);
        if (newLocation.isRelative() == true) {
            page = requestJSONgc(host, path, params);
        } else {
            page = requestJSONgc(newLocation.getHost(), newLocation.getPath(), params);
        }
    } else {
        page = replaceWhitespace(buffer);
    }
    if (page != null) {
        return page;
    } else {
        return "";
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) GZIPInputStream(java.util.zip.GZIPInputStream) InflaterInputStream(java.util.zip.InflaterInputStream) InputStream(java.io.InputStream) InflaterInputStream(java.util.zip.InflaterInputStream) DataOutputStream(java.io.DataOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) IOException(java.io.IOException) Uri(android.net.Uri) HttpURLConnection(java.net.HttpURLConnection) URLConnection(java.net.URLConnection) HttpsURLConnection(javax.net.ssl.HttpsURLConnection) URL(java.net.URL) SocketException(java.net.SocketException) IOException(java.io.IOException) CertificateException(java.security.cert.CertificateException) BigInteger(java.math.BigInteger) GZIPInputStream(java.util.zip.GZIPInputStream) HttpURLConnection(java.net.HttpURLConnection) BufferedReader(java.io.BufferedReader) OutputStreamWriter(java.io.OutputStreamWriter) Inflater(java.util.zip.Inflater) HttpsURLConnection(javax.net.ssl.HttpsURLConnection)

Example 12 with Inflater

use of java.util.zip.Inflater in project c-geo by just-radovan.

the class cgBase method requestJSONgc.

public String requestJSONgc(String host, String path, String params) {
    int httpCode = -1;
    String httpLocation = null;
    // prepare cookies
    String cookiesDone = null;
    if (cookies == null || cookies.isEmpty() == true) {
        if (cookies == null) {
            cookies = new HashMap<String, String>();
        }
        final Map<String, ?> prefsAll = prefs.getAll();
        final Set<String> prefsKeys = prefsAll.keySet();
        for (String key : prefsKeys) {
            if (key.matches("cookie_.+") == true) {
                final String cookieKey = key.substring(7);
                final String cookieValue = (String) prefsAll.get(key);
                cookies.put(cookieKey, cookieValue);
            }
        }
    }
    if (cookies != null) {
        final Object[] keys = cookies.keySet().toArray();
        final ArrayList<String> cookiesEncoded = new ArrayList<String>();
        for (int i = 0; i < keys.length; i++) {
            String value = cookies.get(keys[i].toString());
            cookiesEncoded.add(keys[i] + "=" + value);
        }
        if (cookiesEncoded.size() > 0) {
            cookiesDone = implode("; ", cookiesEncoded.toArray());
        }
    }
    if (cookiesDone == null) {
        Map<String, ?> prefsValues = prefs.getAll();
        if (prefsValues != null && prefsValues.size() > 0) {
            final Object[] keys = prefsValues.keySet().toArray();
            final ArrayList<String> cookiesEncoded = new ArrayList<String>();
            final int length = keys.length;
            for (int i = 0; i < length; i++) {
                if (keys[i].toString().length() > 7 && keys[i].toString().substring(0, 7).equals("cookie_") == true) {
                    cookiesEncoded.add(keys[i].toString().substring(7) + "=" + prefsValues.get(keys[i].toString()));
                }
            }
            if (cookiesEncoded.size() > 0) {
                cookiesDone = implode("; ", cookiesEncoded.toArray());
            }
        }
    }
    if (cookiesDone == null) {
        cookiesDone = "";
    }
    URLConnection uc = null;
    HttpURLConnection connection = null;
    Integer timeout = 30000;
    final StringBuffer buffer = new StringBuffer();
    for (int i = 0; i < 3; i++) {
        if (i > 0) {
            Log.w(cgSettings.tag, "Failed to download data, retrying. Attempt #" + (i + 1));
        }
        buffer.delete(0, buffer.length());
        timeout = 30000 + (i * 15000);
        try {
            // POST
            final URL u = new URL("http://" + host + path);
            uc = u.openConnection();
            uc.setRequestProperty("Host", host);
            uc.setRequestProperty("Cookie", cookiesDone);
            uc.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
            uc.setRequestProperty("X-Requested-With", "XMLHttpRequest");
            uc.setRequestProperty("Accept", "application/json, text/javascript, */*; q=0.01");
            uc.setRequestProperty("Referer", host + "/" + path);
            if (settings.asBrowser == 1) {
                uc.setRequestProperty("Accept-Charset", "utf-8, iso-8859-1, utf-16, *;q=0.7");
                uc.setRequestProperty("Accept-Language", "en-US");
                uc.setRequestProperty("User-Agent", idBrowser);
                uc.setRequestProperty("Connection", "keep-alive");
                uc.setRequestProperty("Keep-Alive", "300");
            }
            connection = (HttpURLConnection) uc;
            connection.setReadTimeout(timeout);
            connection.setRequestMethod("POST");
            // TODO: Fix these (FilCab)
            HttpURLConnection.setFollowRedirects(false);
            connection.setDoInput(true);
            connection.setDoOutput(true);
            final OutputStream out = connection.getOutputStream();
            final OutputStreamWriter wr = new OutputStreamWriter(out);
            wr.write(params);
            wr.flush();
            wr.close();
            String headerName = null;
            final SharedPreferences.Editor prefsEditor = prefs.edit();
            for (int j = 1; (headerName = uc.getHeaderFieldKey(j)) != null; j++) {
                if (headerName != null && headerName.equalsIgnoreCase("Set-Cookie")) {
                    int index;
                    String cookie = uc.getHeaderField(j);
                    index = cookie.indexOf(";");
                    if (index > -1) {
                        cookie = cookie.substring(0, cookie.indexOf(";"));
                    }
                    index = cookie.indexOf("=");
                    if (index > -1 && cookie.length() > (index + 1)) {
                        String name = cookie.substring(0, cookie.indexOf("="));
                        String value = cookie.substring(cookie.indexOf("=") + 1, cookie.length());
                        cookies.put(name, value);
                        prefsEditor.putString("cookie_" + name, value);
                    }
                }
            }
            prefsEditor.commit();
            final String encoding = connection.getContentEncoding();
            InputStream ins;
            if (encoding != null && encoding.equalsIgnoreCase("gzip")) {
                ins = new GZIPInputStream(connection.getInputStream());
            } else if (encoding != null && encoding.equalsIgnoreCase("deflate")) {
                ins = new InflaterInputStream(connection.getInputStream(), new Inflater(true));
            } else {
                ins = connection.getInputStream();
            }
            final InputStreamReader inr = new InputStreamReader(ins);
            final BufferedReader br = new BufferedReader(inr);
            readIntoBuffer(br, buffer);
            httpCode = connection.getResponseCode();
            httpLocation = uc.getHeaderField("Location");
            final String paramsLog = params.replaceAll(passMatch, "password=***");
            Log.i(cgSettings.tag + " | JSON", "[POST " + (int) (params.length() / 1024) + "k | " + httpCode + " | " + (int) (buffer.length() / 1024) + "k] Downloaded " + "http://" + host + path + "?" + paramsLog);
            connection.disconnect();
            br.close();
            ins.close();
            inr.close();
        } catch (IOException e) {
            Log.e(cgSettings.tag, "cgeoBase.requestJSONgc.IOException: " + e.toString());
        } catch (Exception e) {
            Log.e(cgSettings.tag, "cgeoBase.requestJSONgc: " + e.toString());
        }
        if (buffer != null && buffer.length() > 0) {
            break;
        }
    }
    String page = null;
    if (httpCode == 302 && httpLocation != null) {
        final Uri newLocation = Uri.parse(httpLocation);
        if (newLocation.isRelative() == true) {
            page = requestJSONgc(host, path, params);
        } else {
            page = requestJSONgc(newLocation.getHost(), newLocation.getPath(), params);
        }
    } else {
        page = replaceWhitespace(buffer);
    }
    if (page != null) {
        return page;
    } else {
        return "";
    }
}
Also used : DataOutputStream(java.io.DataOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) ArrayList(java.util.ArrayList) Uri(android.net.Uri) URL(java.net.URL) GZIPInputStream(java.util.zip.GZIPInputStream) HttpURLConnection(java.net.HttpURLConnection) InputStreamReader(java.io.InputStreamReader) SharedPreferences(android.content.SharedPreferences) GZIPInputStream(java.util.zip.GZIPInputStream) InflaterInputStream(java.util.zip.InflaterInputStream) InputStream(java.io.InputStream) InflaterInputStream(java.util.zip.InflaterInputStream) IOException(java.io.IOException) HttpURLConnection(java.net.HttpURLConnection) URLConnection(java.net.URLConnection) HttpsURLConnection(javax.net.ssl.HttpsURLConnection) SocketException(java.net.SocketException) IOException(java.io.IOException) CertificateException(java.security.cert.CertificateException) BigInteger(java.math.BigInteger) BufferedReader(java.io.BufferedReader) JSONObject(org.json.JSONObject) OutputStreamWriter(java.io.OutputStreamWriter) Inflater(java.util.zip.Inflater)

Example 13 with Inflater

use of java.util.zip.Inflater in project netty by netty.

the class JdkZlibDecoder method decode.

@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
    if (finished) {
        // Skip data received after finished.
        in.skipBytes(in.readableBytes());
        return;
    }
    int readableBytes = in.readableBytes();
    if (readableBytes == 0) {
        return;
    }
    if (decideZlibOrNone) {
        // First two bytes are needed to decide if it's a ZLIB stream.
        if (readableBytes < 2) {
            return;
        }
        boolean nowrap = !looksLikeZlib(in.getShort(in.readerIndex()));
        inflater = new Inflater(nowrap);
        decideZlibOrNone = false;
    }
    if (crc != null) {
        switch(gzipState) {
            case FOOTER_START:
                if (readGZIPFooter(in)) {
                    finished = true;
                }
                return;
            default:
                if (gzipState != GzipState.HEADER_END) {
                    if (!readGZIPHeader(in)) {
                        return;
                    }
                }
        }
        // Some bytes may have been consumed, and so we must re-set the number of readable bytes.
        readableBytes = in.readableBytes();
    }
    if (in.hasArray()) {
        inflater.setInput(in.array(), in.arrayOffset() + in.readerIndex(), readableBytes);
    } else {
        byte[] array = new byte[readableBytes];
        in.getBytes(in.readerIndex(), array);
        inflater.setInput(array);
    }
    int maxOutputLength = inflater.getRemaining() << 1;
    ByteBuf decompressed = ctx.alloc().heapBuffer(maxOutputLength);
    try {
        boolean readFooter = false;
        byte[] outArray = decompressed.array();
        while (!inflater.needsInput()) {
            int writerIndex = decompressed.writerIndex();
            int outIndex = decompressed.arrayOffset() + writerIndex;
            int length = decompressed.writableBytes();
            if (length == 0) {
                // completely filled the buffer allocate a new one and start to fill it
                out.add(decompressed);
                decompressed = ctx.alloc().heapBuffer(maxOutputLength);
                outArray = decompressed.array();
                continue;
            }
            int outputLength = inflater.inflate(outArray, outIndex, length);
            if (outputLength > 0) {
                decompressed.writerIndex(writerIndex + outputLength);
                if (crc != null) {
                    crc.update(outArray, outIndex, outputLength);
                }
            } else {
                if (inflater.needsDictionary()) {
                    if (dictionary == null) {
                        throw new DecompressionException("decompression failure, unable to set dictionary as non was specified");
                    }
                    inflater.setDictionary(dictionary);
                }
            }
            if (inflater.finished()) {
                if (crc == null) {
                    // Do not decode anymore.
                    finished = true;
                } else {
                    readFooter = true;
                }
                break;
            }
        }
        in.skipBytes(readableBytes - inflater.getRemaining());
        if (readFooter) {
            gzipState = GzipState.FOOTER_START;
            if (readGZIPFooter(in)) {
                finished = true;
            }
        }
    } catch (DataFormatException e) {
        throw new DecompressionException("decompression failure", e);
    } finally {
        if (decompressed.isReadable()) {
            out.add(decompressed);
        } else {
            decompressed.release();
        }
    }
}
Also used : DataFormatException(java.util.zip.DataFormatException) Inflater(java.util.zip.Inflater) ByteBuf(io.netty.buffer.ByteBuf)

Example 14 with Inflater

use of java.util.zip.Inflater in project nutz by nutzam.

the class Sender method createResponse.

protected Response createResponse(Map<String, String> reHeaders) throws IOException {
    Response rep = null;
    if (reHeaders != null) {
        rep = new Response(conn, reHeaders);
        if (rep.isOK()) {
            InputStream is1 = conn.getInputStream();
            InputStream is2 = null;
            String encoding = conn.getContentEncoding();
            // 如果采用了压缩,则需要处理否则都是乱码
            if (encoding != null && encoding.contains("gzip")) {
                is2 = new GZIPInputStream(is1);
            } else if (encoding != null && encoding.contains("deflate")) {
                is2 = new InflaterInputStream(is1, new Inflater(true));
            } else {
                is2 = is1;
            }
            BufferedInputStream is = new BufferedInputStream(is2);
            rep.setStream(is);
        } else {
            try {
                rep.setStream(conn.getInputStream());
            } catch (IOException e) {
                try {
                    rep.setStream(conn.getErrorStream());
                } catch (Exception e1) {
                    rep.setStream(new VoidInputStream());
                }
            }
        }
    }
    if (this.interceptor != null)
        this.interceptor.afterResponse(request, conn, rep);
    return rep;
}
Also used : GZIPInputStream(java.util.zip.GZIPInputStream) BufferedInputStream(java.io.BufferedInputStream) GZIPInputStream(java.util.zip.GZIPInputStream) BufferedInputStream(java.io.BufferedInputStream) InflaterInputStream(java.util.zip.InflaterInputStream) VoidInputStream(org.nutz.lang.stream.VoidInputStream) InputStream(java.io.InputStream) InflaterInputStream(java.util.zip.InflaterInputStream) VoidInputStream(org.nutz.lang.stream.VoidInputStream) Inflater(java.util.zip.Inflater) IOException(java.io.IOException) IOException(java.io.IOException)

Example 15 with Inflater

use of java.util.zip.Inflater in project presto by prestodb.

the class OrcInputStream method decompressZip.

// This comes from the Apache Hive ORC code
private int decompressZip(Slice in) throws IOException {
    Inflater inflater = new Inflater(true);
    try {
        inflater.setInput((byte[]) in.getBase(), (int) (in.getAddress() - ARRAY_BYTE_BASE_OFFSET), in.length());
        allocateOrGrowBuffer(in.length() * EXPECTED_COMPRESSION_RATIO, false);
        int uncompressedLength = 0;
        while (true) {
            uncompressedLength += inflater.inflate(buffer, uncompressedLength, buffer.length - uncompressedLength);
            if (inflater.finished() || buffer.length >= maxBufferSize) {
                break;
            }
            int oldBufferSize = buffer.length;
            allocateOrGrowBuffer(buffer.length * 2, true);
            if (buffer.length <= oldBufferSize) {
                throw new IllegalStateException(String.format("Buffer failed to grow. Old size %d, current size %d", oldBufferSize, buffer.length));
            }
        }
        if (!inflater.finished()) {
            throw new OrcCorruptionException("Could not decompress all input (output buffer too small?)");
        }
        return uncompressedLength;
    } catch (DataFormatException e) {
        throw new OrcCorruptionException(e, "Invalid compressed stream");
    } finally {
        inflater.end();
    }
}
Also used : DataFormatException(java.util.zip.DataFormatException) Inflater(java.util.zip.Inflater) OrcCorruptionException(com.facebook.presto.orc.OrcCorruptionException) InputStreamCheckpoint.createInputStreamCheckpoint(com.facebook.presto.orc.checkpoint.InputStreamCheckpoint.createInputStreamCheckpoint)

Aggregations

Inflater (java.util.zip.Inflater)99 InflaterInputStream (java.util.zip.InflaterInputStream)34 IOException (java.io.IOException)28 ByteArrayOutputStream (java.io.ByteArrayOutputStream)26 InputStream (java.io.InputStream)24 DataFormatException (java.util.zip.DataFormatException)24 GZIPInputStream (java.util.zip.GZIPInputStream)14 ByteArrayInputStream (java.io.ByteArrayInputStream)10 Deflater (java.util.zip.Deflater)9 BufferedInputStream (java.io.BufferedInputStream)6 DataInputStream (java.io.DataInputStream)6 OutputStream (java.io.OutputStream)6 Test (org.junit.Test)6 BufferedReader (java.io.BufferedReader)5 InputStreamReader (java.io.InputStreamReader)5 UnsupportedEncodingException (java.io.UnsupportedEncodingException)5 HttpURLConnection (java.net.HttpURLConnection)5 ByteBuffer (java.nio.ByteBuffer)5 DataOutputStream (java.io.DataOutputStream)4 OutputStreamWriter (java.io.OutputStreamWriter)4