Search in sources :

Example 36 with GZIPInputStream

use of java.util.zip.GZIPInputStream in project jersey by jersey.

the class InterceptorGzipTest method testGzipInterceptorOnlyOnServer.

@Test
public void testGzipInterceptorOnlyOnServer() throws IOException {
    client().register(GZIPWriterTestInterceptor.class);
    WebTarget target = target().path("test");
    String entity = "hello, this is text entity";
    Response response = target.request().put(Entity.entity(entity, MediaType.TEXT_PLAIN_TYPE));
    InputStream is = response.readEntity(InputStream.class);
    GZIPInputStream gzipIs = new GZIPInputStream(is);
    BufferedReader br = new BufferedReader(new InputStreamReader(gzipIs));
    String str = br.readLine();
    assertEquals(entity + FROM_RESOURCE, str);
}
Also used : Response(javax.ws.rs.core.Response) GZIPInputStream(java.util.zip.GZIPInputStream) InputStreamReader(java.io.InputStreamReader) GZIPInputStream(java.util.zip.GZIPInputStream) InputStream(java.io.InputStream) BufferedReader(java.io.BufferedReader) WebTarget(javax.ws.rs.client.WebTarget) JerseyTest(org.glassfish.jersey.test.JerseyTest) Test(org.junit.Test)

Example 37 with GZIPInputStream

use of java.util.zip.GZIPInputStream in project FastDev4Android by jiangqqlmj.

the class IoUtils method unGZip.

/**
     * GZip解压
     *
     * @param bContent
     * @return
     */
public static byte[] unGZip(byte[] bContent) {
    byte[] data = new byte[1024];
    try {
        ByteArrayInputStream in = new ByteArrayInputStream(bContent);
        GZIPInputStream pIn = new GZIPInputStream(in);
        DataInputStream objIn = new DataInputStream(pIn);
        int len = 0;
        int count = 0;
        while ((count = objIn.read(data, len, len + 1024)) != -1) {
            len = len + count;
        }
        byte[] trueData = new byte[len];
        System.arraycopy(data, 0, trueData, 0, len);
        objIn.close();
        pIn.close();
        in.close();
        return trueData;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}
Also used : GZIPInputStream(java.util.zip.GZIPInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) DataInputStream(java.io.DataInputStream) ClientProtocolException(org.apache.http.client.ClientProtocolException) URISyntaxException(java.net.URISyntaxException) SocketException(java.net.SocketException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 38 with GZIPInputStream

use of java.util.zip.GZIPInputStream in project JessMA by ldcsaa.

the class HttpHelper method unGZip.

/** GZip 解压 */
public static final byte[] unGZip(byte[] bytes) throws IOException {
    byte[] buffer = new byte[4096];
    ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
    GZIPInputStream gzip = new GZIPInputStream(bis);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    try {
        int r;
        while ((r = gzip.read(buffer, 0, buffer.length)) != -1) baos.write(buffer, 0, r);
        return baos.toByteArray();
    } finally {
        try {
            if (baos != null)
                baos.close();
            if (gzip != null)
                gzip.close();
            if (bis != null)
                bis.close();
        } catch (IOException e) {
        }
    }
}
Also used : GZIPInputStream(java.util.zip.GZIPInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException)

Example 39 with GZIPInputStream

use of java.util.zip.GZIPInputStream in project KJFrameForAndroid by kymjs.

the class HttpUtils method responseToBytes.

public static byte[] responseToBytes(KJHttpResponse response) throws IOException, KJHttpException {
    PoolingByteArrayOutputStream bytes = new PoolingByteArrayOutputStream(ByteArrayPool.get(), (int) response.getContentLength());
    byte[] buffer = null;
    try {
        InputStream in = response.getContentStream();
        if (isGzipContent(response) && !(in instanceof GZIPInputStream)) {
            in = new GZIPInputStream(in);
        }
        if (in == null) {
            throw new KJHttpException("服务器连接异常");
        }
        buffer = ByteArrayPool.get().getBuf(1024);
        int count;
        while ((count = in.read(buffer)) != -1) {
            bytes.write(buffer, 0, count);
        }
        return bytes.toByteArray();
    } finally {
        try {
            // Close the InputStream and release the resources by
            // "consuming the content".
            //                entity.consumeContent();
            response.getContentStream().close();
        } catch (IOException e) {
            // This can happen if there was an exception above that left the
            // entity in
            // an invalid state.
            KJLoger.debug("Error occured when calling consumingContent");
        }
        ByteArrayPool.get().returnBuf(buffer);
        bytes.close();
    }
}
Also used : GZIPInputStream(java.util.zip.GZIPInputStream) GZIPInputStream(java.util.zip.GZIPInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException)

Example 40 with GZIPInputStream

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

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