Search in sources :

Example 56 with Inflater

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

the class Downloader method fetch.

/**
 * This method initiates a connect call of the provided connection and returns the response
 * stream. It only returns the error stream if it is available and readErrorStreamNoException is
 * true otherwise it throws an IOException if an error happens. Furthermore it wraps the stream
 * to decompress it if the connection content encoding is specified.
 */
public InputStream fetch(HttpURLConnection connection, boolean readErrorStreamNoException) throws IOException {
    // create connection but before reading get the correct inputstream based on the compression and if error
    connection.connect();
    InputStream is;
    if (readErrorStreamNoException && connection.getResponseCode() >= 400 && connection.getErrorStream() != null)
        is = connection.getErrorStream();
    else
        is = connection.getInputStream();
    if (is == null)
        throw new IOException("Stream is null. Message:" + connection.getResponseMessage());
    // wrap
    try {
        String encoding = connection.getContentEncoding();
        if (encoding != null && encoding.equalsIgnoreCase("gzip"))
            is = new GZIPInputStream(is);
        else if (encoding != null && encoding.equalsIgnoreCase("deflate"))
            is = new InflaterInputStream(is, new Inflater(true));
    } catch (IOException ex) {
    }
    return is;
}
Also used : GZIPInputStream(java.util.zip.GZIPInputStream) GZIPInputStream(java.util.zip.GZIPInputStream) InflaterInputStream(java.util.zip.InflaterInputStream) InflaterInputStream(java.util.zip.InflaterInputStream) Inflater(java.util.zip.Inflater)

Example 57 with Inflater

use of java.util.zip.Inflater in project k-9 by k9mail.

the class ImapConnection method enableCompression.

private void enableCompression() throws IOException, MessagingException {
    try {
        executeSimpleCommand(Commands.COMPRESS_DEFLATE);
    } catch (NegativeImapResponseException e) {
        Log.d(LOG_TAG, "Unable to negotiate compression: " + e.getMessage());
        return;
    }
    try {
        InflaterInputStream input = new InflaterInputStream(socket.getInputStream(), new Inflater(true));
        ZOutputStream output = new ZOutputStream(socket.getOutputStream(), JZlib.Z_BEST_SPEED, true);
        output.setFlushMode(JZlib.Z_PARTIAL_FLUSH);
        setUpStreamsAndParser(input, output);
        if (K9MailLib.isDebug()) {
            Log.i(LOG_TAG, "Compression enabled for " + getLogId());
        }
    } catch (IOException e) {
        close();
        Log.e(LOG_TAG, "Error enabling compression", e);
    }
}
Also used : InflaterInputStream(java.util.zip.InflaterInputStream) Inflater(java.util.zip.Inflater) IOException(java.io.IOException) ZOutputStream(com.jcraft.jzlib.ZOutputStream)

Example 58 with Inflater

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

the class cgBase method postTweet.

public void postTweet(cgeoapplication app, cgSettings settings, String status, Double latitude, Double longitude) {
    if (app == null) {
        return;
    }
    if (settings == null || settings.tokenPublic == null || settings.tokenPublic.length() == 0 || settings.tokenSecret == null || settings.tokenSecret.length() == 0) {
        return;
    }
    try {
        HashMap<String, String> parameters = new HashMap<String, String>();
        parameters.put("status", status);
        if (latitude != null && longitude != null) {
            parameters.put("lat", String.format("%.6f", latitude));
            parameters.put("long", String.format("%.6f", longitude));
            parameters.put("display_coordinates", "true");
        }
        final String paramsDone = cgOAuth.signOAuth("api.twitter.com", "/1/statuses/update.json", "POST", false, parameters, settings.tokenPublic, settings.tokenSecret);
        HttpURLConnection connection = null;
        try {
            final StringBuffer buffer = new StringBuffer();
            final URL u = new URL("http://api.twitter.com/1/statuses/update.json");
            final URLConnection uc = u.openConnection();
            uc.setRequestProperty("Host", "api.twitter.com");
            connection = (HttpURLConnection) uc;
            connection.setReadTimeout(30000);
            connection.setRequestMethod("POST");
            HttpURLConnection.setFollowRedirects(true);
            connection.setDoInput(true);
            connection.setDoOutput(true);
            final OutputStream out = connection.getOutputStream();
            final OutputStreamWriter wr = new OutputStreamWriter(out);
            wr.write(paramsDone);
            wr.flush();
            wr.close();
            Log.i(cgSettings.tag, "Twitter.com: " + connection.getResponseCode() + " " + connection.getResponseMessage());
            InputStream ins;
            final String encoding = connection.getContentEncoding();
            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);
            br.close();
            ins.close();
            inr.close();
            connection.disconnect();
        } catch (IOException e) {
            Log.e(cgSettings.tag, "cgBase.postTweet.IO: " + connection.getResponseCode() + ": " + connection.getResponseMessage() + " ~ " + e.toString());
            final InputStream ins = connection.getErrorStream();
            final StringBuffer buffer = new StringBuffer();
            final InputStreamReader inr = new InputStreamReader(ins);
            final BufferedReader br = new BufferedReader(inr);
            readIntoBuffer(br, buffer);
            br.close();
            ins.close();
            inr.close();
        } catch (Exception e) {
            Log.e(cgSettings.tag, "cgBase.postTweet.inner: " + e.toString());
        }
        connection.disconnect();
    } catch (Exception e) {
        Log.e(cgSettings.tag, "cgBase.postTweet: " + e.toString());
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) HashMap(java.util.HashMap) 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) URL(java.net.URL) 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) GZIPInputStream(java.util.zip.GZIPInputStream) HttpURLConnection(java.net.HttpURLConnection) BufferedReader(java.io.BufferedReader) OutputStreamWriter(java.io.OutputStreamWriter) Inflater(java.util.zip.Inflater)

Example 59 with Inflater

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

the class cgBase method request.

public cgResponse request(boolean secure, String host, String path, String method, String params, int requestId, Boolean xContentType) {
    URL u = null;
    int httpCode = -1;
    String httpMessage = null;
    String httpLocation = null;
    if (requestId == 0) {
        requestId = (int) (Math.random() * 1000);
    }
    if (method == null || (method.equalsIgnoreCase("GET") == false && method.equalsIgnoreCase("POST") == false)) {
        method = "POST";
    } else {
        method = method.toUpperCase();
    }
    // https
    String scheme = "http://";
    if (secure) {
        scheme = "https://";
    }
    // 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 && !cookies.isEmpty() && cookies.keySet().size() > 0) {
        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 && prefsValues.keySet().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;
    StringBuffer buffer = null;
    for (int i = 0; i < 5; i++) {
        if (i > 0) {
            Log.w(cgSettings.tag, "Failed to download data, retrying. Attempt #" + (i + 1));
        }
        buffer = new StringBuffer();
        timeout = 30000 + (i * 10000);
        try {
            if (method.equals("GET")) {
                // GET
                u = new URL(scheme + host + path + "?" + params);
                uc = u.openConnection();
                uc.setRequestProperty("Host", host);
                uc.setRequestProperty("Cookie", cookiesDone);
                if (xContentType == true) {
                    uc.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
                }
                if (settings.asBrowser == 1) {
                    uc.setRequestProperty("Accept", "application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5");
                    // uc.setRequestProperty("Accept-Encoding", "gzip"); // not supported via cellular network
                    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(method);
                HttpURLConnection.setFollowRedirects(false);
                connection.setDoInput(true);
                connection.setDoOutput(false);
            } else {
                // POST
                u = new URL(scheme + host + path);
                uc = u.openConnection();
                uc.setRequestProperty("Host", host);
                uc.setRequestProperty("Cookie", cookiesDone);
                if (xContentType == true) {
                    uc.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
                }
                if (settings.asBrowser == 1) {
                    uc.setRequestProperty("Accept", "application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5");
                    // uc.setRequestProperty("Accept-Encoding", "gzip"); // not supported via cellular network
                    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(method);
                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();
            httpMessage = connection.getResponseMessage();
            httpLocation = uc.getHeaderField("Location");
            final String paramsLog = params.replaceAll(passMatch, "password=***");
            if (buffer != null && connection != null) {
                Log.i(cgSettings.tag + "|" + requestId, "[" + method + " " + (int) (params.length() / 1024) + "k | " + httpCode + " | " + (int) (buffer.length() / 1024) + "k] Downloaded " + scheme + host + path + "?" + paramsLog);
            } else {
                Log.i(cgSettings.tag + "|" + requestId, "[" + method + " | " + httpCode + "] Failed to download " + scheme + host + path + "?" + paramsLog);
            }
            connection.disconnect();
            br.close();
            ins.close();
            inr.close();
        } catch (IOException e) {
            Log.e(cgSettings.tag, "cgeoBase.request.IOException: " + e.toString());
        } catch (Exception e) {
            Log.e(cgSettings.tag, "cgeoBase.request: " + e.toString());
        }
        if (buffer != null && buffer.length() > 0) {
            break;
        }
    }
    cgResponse response = new cgResponse();
    String data = null;
    try {
        if (httpCode == 302 && httpLocation != null) {
            final Uri newLocation = Uri.parse(httpLocation);
            if (newLocation.isRelative() == true) {
                response = request(secure, host, path, "GET", new HashMap<String, String>(), requestId, false, false, false);
            } else {
                boolean secureRedir = false;
                if (newLocation.getScheme().equals("https")) {
                    secureRedir = true;
                }
                response = request(secureRedir, newLocation.getHost(), newLocation.getPath(), "GET", new HashMap<String, String>(), requestId, false, false, false);
            }
        } else {
            if (buffer != null && buffer.length() > 0) {
                data = replaceWhitespace(buffer);
                buffer = null;
                if (data != null) {
                    response.setData(data);
                } else {
                    response.setData("");
                }
                response.setStatusCode(httpCode);
                response.setStatusMessage(httpMessage);
                response.setUrl(u.toString());
            }
        }
    } catch (Exception e) {
        Log.e(cgSettings.tag, "cgeoBase.page: " + e.toString());
    }
    return response;
}
Also used : HashMap(java.util.HashMap) 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 60 with Inflater

use of java.util.zip.Inflater in project bazel by bazelbuild.

the class ProfileInfo method loadProfile.

/**
   * Loads and parses Blaze profile file.
   *
   * @param profileFile profile file path
   *
   * @return ProfileInfo object with some fields populated (call calculateStats()
   *         and analyzeRelationships() to populate the remaining fields)
   * @throws UnsupportedEncodingException if the file format is invalid
   * @throws IOException if the file can't be read
   */
public static ProfileInfo loadProfile(Path profileFile) throws IOException {
    // It is extremely important to wrap InflaterInputStream using
    // BufferedInputStream because majority of reads would be done using
    // readInt()/readLong() methods and InflaterInputStream is very inefficient
    // in handling small read requests (performance difference with 1MB buffer
    // used below is almost 10x).
    DataInputStream in = new DataInputStream(new BufferedInputStream(new InflaterInputStream(profileFile.getInputStream(), new Inflater(false), 65536), 1024 * 1024));
    if (in.readInt() != Profiler.MAGIC) {
        in.close();
        throw new UnsupportedEncodingException("Invalid profile datafile format");
    }
    if (in.readInt() != Profiler.VERSION) {
        in.close();
        throw new UnsupportedEncodingException("Incompatible profile datafile version");
    }
    String fileComment = in.readUTF();
    // Read list of used record types
    int typeCount = in.readInt();
    boolean hasUnknownTypes = false;
    Set<String> supportedTasks = new HashSet<>();
    for (ProfilerTask task : ProfilerTask.values()) {
        supportedTasks.add(task.toString());
    }
    List<ProfilerTask> typeList = new ArrayList<>();
    for (int i = 0; i < typeCount; i++) {
        String name = in.readUTF();
        if (supportedTasks.contains(name)) {
            typeList.add(ProfilerTask.valueOf(name));
        } else {
            hasUnknownTypes = true;
            typeList.add(ProfilerTask.UNKNOWN);
        }
    }
    ProfileInfo info = new ProfileInfo(fileComment);
    // TODO(bazel-team): Maybe this still should handle corrupted(truncated) files.
    try {
        int size;
        while ((size = in.readInt()) != Profiler.EOF_MARKER) {
            byte[] backingArray = new byte[size];
            in.readFully(backingArray);
            ByteBuffer buffer = ByteBuffer.wrap(backingArray);
            long threadId = VarInt.getVarLong(buffer);
            int id = VarInt.getVarInt(buffer);
            int parentId = VarInt.getVarInt(buffer);
            long startTime = VarInt.getVarLong(buffer);
            long duration = VarInt.getVarLong(buffer);
            int descIndex = VarInt.getVarInt(buffer) - 1;
            if (descIndex == -1) {
                String desc = in.readUTF();
                descIndex = info.descriptionList.size();
                info.descriptionList.add(desc);
            }
            ProfilerTask type = typeList.get(buffer.get());
            byte[] stats = null;
            if (buffer.hasRemaining()) {
                // Copy aggregated stats.
                int offset = buffer.position();
                stats = Arrays.copyOfRange(backingArray, offset, size);
                if (hasUnknownTypes) {
                    while (buffer.hasRemaining()) {
                        byte attrType = buffer.get();
                        if (typeList.get(attrType) == ProfilerTask.UNKNOWN) {
                            // We're dealing with unknown aggregated type - update stats array to
                            // use ProfilerTask.UNKNOWN.ordinal() value.
                            stats[buffer.position() - 1 - offset] = (byte) ProfilerTask.UNKNOWN.ordinal();
                        }
                        VarInt.getVarInt(buffer);
                        VarInt.getVarLong(buffer);
                    }
                }
            }
            ProfileInfo.Task task = info.new Task(threadId, id, parentId, startTime, duration, type, descIndex, new CompactStatistics(stats));
            info.addTask(task);
        }
    } catch (IOException e) {
        info.corruptedOrIncomplete = true;
    } finally {
        in.close();
    }
    return info;
}
Also used : InflaterInputStream(java.util.zip.InflaterInputStream) ArrayList(java.util.ArrayList) UnsupportedEncodingException(java.io.UnsupportedEncodingException) IOException(java.io.IOException) DataInputStream(java.io.DataInputStream) ByteBuffer(java.nio.ByteBuffer) BufferedInputStream(java.io.BufferedInputStream) Inflater(java.util.zip.Inflater) HashSet(java.util.HashSet)

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