Search in sources :

Example 41 with GZIPInputStream

use of java.util.zip.GZIPInputStream 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 42 with GZIPInputStream

use of java.util.zip.GZIPInputStream in project hudson-2.x by hudson.

the class ConsoleNote method readFrom.

/**
     * Reads a note back from {@linkplain #encodeTo(OutputStream) its encoded form}.
     *
     * @param in
     *      Must point to the beginning of a preamble.
     *
     * @return null if the encoded form is malformed.
     */
public static ConsoleNote readFrom(DataInputStream in) throws IOException, ClassNotFoundException {
    try {
        byte[] preamble = new byte[PREAMBLE.length];
        in.readFully(preamble);
        if (!Arrays.equals(preamble, PREAMBLE))
            // not a valid preamble
            return null;
        DataInputStream decoded = new DataInputStream(new UnbufferedBase64InputStream(in));
        int sz = decoded.readInt();
        //Size should be greater than Zero. See http://issues.hudson-ci.org/browse/HUDSON-6558
        if (sz < 0) {
            return null;
        }
        byte[] buf = new byte[sz];
        decoded.readFully(buf);
        byte[] postamble = new byte[POSTAMBLE.length];
        in.readFully(postamble);
        if (!Arrays.equals(postamble, POSTAMBLE))
            // not a valid postamble
            return null;
        ObjectInputStream ois = new ObjectInputStreamEx(new GZIPInputStream(new ByteArrayInputStream(buf)), Hudson.getInstance().pluginManager.uberClassLoader);
        return (ConsoleNote) ois.readObject();
    } catch (Error e) {
        // package that up as IOException so that the caller won't fatally die.
        throw new IOException2(e);
    }
}
Also used : GZIPInputStream(java.util.zip.GZIPInputStream) UnbufferedBase64InputStream(hudson.util.UnbufferedBase64InputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) ObjectInputStreamEx(hudson.remoting.ObjectInputStreamEx) DataInputStream(java.io.DataInputStream) IOException2(hudson.util.IOException2) ObjectInputStream(java.io.ObjectInputStream)

Example 43 with GZIPInputStream

use of java.util.zip.GZIPInputStream in project JGibbLabeledLDA by myleott.

the class LDADataset method readDataSet.

//---------------------------------------------------------------
// I/O methods
//---------------------------------------------------------------
/**
     * read a dataset from a file
     * @return true if success and false otherwise
     */
public boolean readDataSet(String filename, boolean unlabeled) throws FileNotFoundException, IOException {
    BufferedReader reader = new BufferedReader(new InputStreamReader(new GZIPInputStream(new FileInputStream(filename)), "UTF-8"));
    try {
        String line;
        while ((line = reader.readLine()) != null) {
            addDoc(line, unlabeled);
        }
        setM(docs.size());
        // debug output
        System.out.println("Dataset loaded:");
        System.out.println("\tM:" + M);
        System.out.println("\tV:" + V);
        return true;
    } finally {
        reader.close();
    }
}
Also used : GZIPInputStream(java.util.zip.GZIPInputStream) InputStreamReader(java.io.InputStreamReader) BufferedReader(java.io.BufferedReader) FileInputStream(java.io.FileInputStream)

Example 44 with GZIPInputStream

use of java.util.zip.GZIPInputStream in project JGibbLabeledLDA by myleott.

the class Model method readTAssignFile.

/**
     * Load word-topic assignments for this model
     */
protected boolean readTAssignFile(String tassignFile) {
    try {
        int i, j;
        BufferedReader reader = new BufferedReader(new InputStreamReader(new GZIPInputStream(new FileInputStream(tassignFile)), "UTF-8"));
        String line;
        z = new TIntArrayList[M];
        data = new LDADataset();
        data.setM(M);
        data.V = V;
        for (i = 0; i < M; i++) {
            line = reader.readLine();
            StringTokenizer tknr = new StringTokenizer(line, " \t\r\n");
            int length = tknr.countTokens();
            TIntArrayList words = new TIntArrayList();
            TIntArrayList topics = new TIntArrayList();
            for (j = 0; j < length; j++) {
                String token = tknr.nextToken();
                StringTokenizer tknr2 = new StringTokenizer(token, ":");
                if (tknr2.countTokens() != 2) {
                    System.out.println("Invalid word-topic assignment line\n");
                    return false;
                }
                words.add(Integer.parseInt(tknr2.nextToken()));
                topics.add(Integer.parseInt(tknr2.nextToken()));
            }
            //end for each topic assignment
            //allocate and add new document to the corpus
            Document doc = new Document(words);
            data.setDoc(doc, i);
            //assign values for z
            z[i] = new TIntArrayList();
            for (j = 0; j < topics.size(); j++) {
                z[i].add(topics.get(j));
            }
        }
        //end for each doc
        reader.close();
    } catch (Exception e) {
        System.out.println("Error while loading model: " + e.getMessage());
        e.printStackTrace();
        return false;
    }
    return true;
}
Also used : GZIPInputStream(java.util.zip.GZIPInputStream) StringTokenizer(java.util.StringTokenizer) InputStreamReader(java.io.InputStreamReader) BufferedReader(java.io.BufferedReader) FileInputStream(java.io.FileInputStream) TIntArrayList(gnu.trove.list.array.TIntArrayList) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException)

Example 45 with GZIPInputStream

use of java.util.zip.GZIPInputStream in project JGibbLabeledLDA by myleott.

the class Model method readOthersFile.

/**
     * Load "others" file to get parameters
     */
protected boolean readOthersFile(String otherFile) {
    try {
        BufferedReader reader = new BufferedReader(new InputStreamReader(new GZIPInputStream(new FileInputStream(otherFile)), "UTF-8"));
        String line;
        while ((line = reader.readLine()) != null) {
            StringTokenizer tknr = new StringTokenizer(line, "= \t\r\n");
            int count = tknr.countTokens();
            if (count != 2)
                continue;
            String optstr = tknr.nextToken();
            String optval = tknr.nextToken();
            if (optstr.equalsIgnoreCase("alpha")) {
                alpha = Double.parseDouble(optval);
            } else if (optstr.equalsIgnoreCase("beta")) {
                beta = Double.parseDouble(optval);
            } else if (optstr.equalsIgnoreCase("ntopics")) {
                K = Integer.parseInt(optval);
            } else if (optstr.equalsIgnoreCase("liter")) {
                liter = Integer.parseInt(optval);
            } else if (optstr.equalsIgnoreCase("nwords")) {
                V = Integer.parseInt(optval);
            } else if (optstr.equalsIgnoreCase("ndocs")) {
                M = Integer.parseInt(optval);
            } else {
            // any more?
            }
        }
        reader.close();
    } catch (Exception e) {
        System.out.println("Error while reading other file:" + e.getMessage());
        e.printStackTrace();
        return false;
    }
    return true;
}
Also used : GZIPInputStream(java.util.zip.GZIPInputStream) StringTokenizer(java.util.StringTokenizer) InputStreamReader(java.io.InputStreamReader) BufferedReader(java.io.BufferedReader) FileInputStream(java.io.FileInputStream) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException)

Aggregations

GZIPInputStream (java.util.zip.GZIPInputStream)376 InputStream (java.io.InputStream)144 IOException (java.io.IOException)125 ByteArrayInputStream (java.io.ByteArrayInputStream)120 FileInputStream (java.io.FileInputStream)98 ByteArrayOutputStream (java.io.ByteArrayOutputStream)77 InputStreamReader (java.io.InputStreamReader)57 File (java.io.File)56 BufferedReader (java.io.BufferedReader)45 BufferedInputStream (java.io.BufferedInputStream)41 Test (org.junit.Test)41 FileOutputStream (java.io.FileOutputStream)30 URL (java.net.URL)25 InflaterInputStream (java.util.zip.InflaterInputStream)25 OutputStream (java.io.OutputStream)24 GZIPOutputStream (java.util.zip.GZIPOutputStream)21 ObjectInputStream (java.io.ObjectInputStream)19 HttpURLConnection (java.net.HttpURLConnection)19 URLConnection (java.net.URLConnection)17 HashMap (java.util.HashMap)15