Search in sources :

Example 11 with InflaterInputStream

use of java.util.zip.InflaterInputStream in project openhab1-addons by openhab.

the class AbstractRequest method executeUrl.

/**
     * Executes the given <code>url</code> with the given <code>httpMethod</code>. In the case of httpMethods that do
     * not support automatic redirection, manually handle the HTTP temporary redirect (307) and retry with the new URL.
     * 
     * @param httpMethod
     *            the HTTP method to use
     * @param url
     *            the url to execute (in milliseconds)
     * @param contentString
     *            the content to be sent to the given <code>url</code> or <code>null</code> if no content should be
     *            sent.
     * @param contentType
     *            the content type of the given <code>contentString</code>
     * @return the response body or <code>NULL</code> when the request went wrong
     */
protected final String executeUrl(final String httpMethod, final String url, final String contentString, final String contentType) {
    HttpClient client = new HttpClient();
    HttpMethod method = HttpUtil.createHttpMethod(httpMethod, url);
    method.getParams().setSoTimeout(httpRequestTimeout);
    method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(3, false));
    for (String httpHeaderKey : HTTP_HEADERS.stringPropertyNames()) {
        method.addRequestHeader(new Header(httpHeaderKey, HTTP_HEADERS.getProperty(httpHeaderKey)));
    }
    // add content if a valid method is given ...
    if (method instanceof EntityEnclosingMethod && contentString != null) {
        EntityEnclosingMethod eeMethod = (EntityEnclosingMethod) method;
        InputStream content = new ByteArrayInputStream(contentString.getBytes());
        eeMethod.setRequestEntity(new InputStreamRequestEntity(content, contentType));
    }
    if (logger.isDebugEnabled()) {
        try {
            logger.trace("About to execute '" + method.getURI().toString() + "'");
        } catch (URIException e) {
            logger.trace(e.getMessage());
        }
    }
    try {
        int statusCode = client.executeMethod(method);
        if (statusCode == HttpStatus.SC_NO_CONTENT || statusCode == HttpStatus.SC_ACCEPTED) {
            // perfectly fine but we cannot expect any answer...
            return null;
        }
        // Manually handle 307 redirects with a little tail recursion
        if (statusCode == HttpStatus.SC_TEMPORARY_REDIRECT) {
            Header[] headers = method.getResponseHeaders("Location");
            String newUrl = headers[headers.length - 1].getValue();
            return executeUrl(httpMethod, newUrl, contentString, contentType);
        }
        if (statusCode != HttpStatus.SC_OK) {
            logger.warn("Method failed: " + method.getStatusLine());
        }
        InputStream tmpResponseStream = method.getResponseBodyAsStream();
        Header encodingHeader = method.getResponseHeader("Content-Encoding");
        if (encodingHeader != null) {
            for (HeaderElement ehElem : encodingHeader.getElements()) {
                if (ehElem.toString().matches(".*gzip.*")) {
                    tmpResponseStream = new GZIPInputStream(tmpResponseStream);
                    logger.trace("GZipped InputStream from {}", url);
                } else if (ehElem.toString().matches(".*deflate.*")) {
                    tmpResponseStream = new InflaterInputStream(tmpResponseStream);
                    logger.trace("Deflated InputStream from {}", url);
                }
            }
        }
        String responseBody = IOUtils.toString(tmpResponseStream);
        if (!responseBody.isEmpty()) {
            logger.trace(responseBody);
        }
        return responseBody;
    } catch (HttpException he) {
        logger.error("Fatal protocol violation: {}", he.toString());
    } catch (IOException ioe) {
        logger.error("Fatal transport error: {}", ioe.toString());
    } finally {
        method.releaseConnection();
    }
    return null;
}
Also used : InputStreamRequestEntity(org.apache.commons.httpclient.methods.InputStreamRequestEntity) GZIPInputStream(java.util.zip.GZIPInputStream) InflaterInputStream(java.util.zip.InflaterInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) HeaderElement(org.apache.commons.httpclient.HeaderElement) InflaterInputStream(java.util.zip.InflaterInputStream) EntityEnclosingMethod(org.apache.commons.httpclient.methods.EntityEnclosingMethod) DefaultHttpMethodRetryHandler(org.apache.commons.httpclient.DefaultHttpMethodRetryHandler) IOException(java.io.IOException) GZIPInputStream(java.util.zip.GZIPInputStream) URIException(org.apache.commons.httpclient.URIException) Header(org.apache.commons.httpclient.Header) ByteArrayInputStream(java.io.ByteArrayInputStream) HttpClient(org.apache.commons.httpclient.HttpClient) HttpException(org.apache.commons.httpclient.HttpException) HttpMethod(org.apache.commons.httpclient.HttpMethod)

Example 12 with InflaterInputStream

use of java.util.zip.InflaterInputStream 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 13 with InflaterInputStream

use of java.util.zip.InflaterInputStream in project robovm by robovm.

the class SpdyReader method newNameValueBlockStream.

private DataInputStream newNameValueBlockStream() {
    // Limit the inflater input stream to only those bytes in the Name/Value block.
    final InputStream throttleStream = new InputStream() {

        @Override
        public int read() throws IOException {
            return Util.readSingleByte(this);
        }

        @Override
        public int read(byte[] buffer, int offset, int byteCount) throws IOException {
            byteCount = Math.min(byteCount, compressedLimit);
            int consumed = in.read(buffer, offset, byteCount);
            compressedLimit -= consumed;
            return consumed;
        }

        @Override
        public void close() throws IOException {
            in.close();
        }
    };
    // Subclass inflater to install a dictionary when it's needed.
    Inflater inflater = new Inflater() {

        @Override
        public int inflate(byte[] buffer, int offset, int count) throws DataFormatException {
            int result = super.inflate(buffer, offset, count);
            if (result == 0 && needsDictionary()) {
                setDictionary(DICTIONARY);
                result = super.inflate(buffer, offset, count);
            }
            return result;
        }
    };
    return new DataInputStream(new InflaterInputStream(throttleStream, inflater));
}
Also used : DataInputStream(java.io.DataInputStream) InflaterInputStream(java.util.zip.InflaterInputStream) InputStream(java.io.InputStream) InflaterInputStream(java.util.zip.InflaterInputStream) Inflater(java.util.zip.Inflater) DataInputStream(java.io.DataInputStream)

Example 14 with InflaterInputStream

use of java.util.zip.InflaterInputStream in project robovm by robovm.

the class FilterInputStreamNullSourceTest method testInflaterInputStream.

public void testInflaterInputStream() throws IOException {
    try {
        new InflaterInputStream(null);
        fail();
    } catch (NullPointerException expected) {
    }
    try {
        new InflaterInputStream(null, new Inflater());
        fail();
    } catch (NullPointerException expected) {
    }
    try {
        new InflaterInputStream(null, new Inflater(), 1024);
        fail();
    } catch (NullPointerException expected) {
    }
}
Also used : InflaterInputStream(java.util.zip.InflaterInputStream) Inflater(java.util.zip.Inflater)

Example 15 with InflaterInputStream

use of java.util.zip.InflaterInputStream in project robovm by robovm.

the class DeflaterOutputStreamTest method testSyncFlushDeflater.

/**
     * Confirm that a DeflaterOutputStream constructed with Deflater
     * with flushParm == SYNC_FLUSH does not need to to be flushed.
     *
     * http://b/4005091
     */
public void testSyncFlushDeflater() throws Exception {
    Deflater def = new Deflater();
    Field f = def.getClass().getDeclaredField("flushParm");
    f.setAccessible(true);
    f.setInt(def, Deflater.SYNC_FLUSH);
    final int deflaterBufferSize = 512;
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    DeflaterOutputStream dos = new DeflaterOutputStream(baos, def, deflaterBufferSize);
    // make output buffer large enough that even if compressed it
    // won't all fit within the deflaterBufferSize.
    final int outputBufferSize = 128 * deflaterBufferSize;
    byte[] output = new byte[outputBufferSize];
    for (int i = 0; i < output.length; i++) {
        output[i] = (byte) i;
    }
    dos.write(output);
    byte[] compressed = baos.toByteArray();
    // this main reason for this assert is to make sure that the
    // compressed byte count is larger than the
    // deflaterBufferSize. However, when the original bug exists,
    // it will also fail because the compressed length will be
    // exactly the length of the deflaterBufferSize.
    assertTrue("compressed=" + compressed.length + " but deflaterBufferSize=" + deflaterBufferSize, compressed.length > deflaterBufferSize);
    // assert that we returned data matches the input exactly.
    ByteArrayInputStream bais = new ByteArrayInputStream(compressed);
    InflaterInputStream iis = new InflaterInputStream(bais);
    byte[] input = new byte[output.length];
    int total = 0;
    while (true) {
        int n = iis.read(input, total, input.length - total);
        if (n == -1) {
            break;
        }
        total += n;
        if (total == input.length) {
            try {
                iis.read();
                fail();
            } catch (EOFException expected) {
                break;
            }
        }
    }
    assertEquals(output.length, total);
    assertTrue(Arrays.equals(input, output));
    // ensure Deflater.finish has not been called at any point
    // during the test, since that would lead to the results being
    // flushed even without SYNC_FLUSH being used
    assertFalse(def.finished());
    // Quieten CloseGuard.
    def.end();
    iis.close();
}
Also used : Field(java.lang.reflect.Field) Deflater(java.util.zip.Deflater) ByteArrayInputStream(java.io.ByteArrayInputStream) InflaterInputStream(java.util.zip.InflaterInputStream) DeflaterOutputStream(java.util.zip.DeflaterOutputStream) EOFException(java.io.EOFException) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Aggregations

InflaterInputStream (java.util.zip.InflaterInputStream)91 ByteArrayInputStream (java.io.ByteArrayInputStream)49 InputStream (java.io.InputStream)46 IOException (java.io.IOException)38 ByteArrayOutputStream (java.io.ByteArrayOutputStream)33 Inflater (java.util.zip.Inflater)33 GZIPInputStream (java.util.zip.GZIPInputStream)26 DataInputStream (java.io.DataInputStream)16 BufferedInputStream (java.io.BufferedInputStream)11 HttpURLConnection (java.net.HttpURLConnection)9 OutputStream (java.io.OutputStream)8 URL (java.net.URL)8 DeflaterOutputStream (java.util.zip.DeflaterOutputStream)8 BufferedReader (java.io.BufferedReader)6 InputStreamReader (java.io.InputStreamReader)6 URLConnection (java.net.URLConnection)6 EOFException (java.io.EOFException)5 DataOutputStream (java.io.DataOutputStream)4 OutputStreamWriter (java.io.OutputStreamWriter)4 SocketException (java.net.SocketException)4