Example usage for java.util.zip DeflaterInputStream DeflaterInputStream

List of usage examples for java.util.zip DeflaterInputStream DeflaterInputStream

Introduction

In this page you can find the example usage for java.util.zip DeflaterInputStream DeflaterInputStream.

Prototype

public DeflaterInputStream(InputStream in) 

Source Link

Document

Creates a new input stream with a default compressor and buffer size.

Usage

From source file:net.solarnetwork.node.support.HttpClientSupport.java

/**
 * Get an InputStream from a URLConnection response, handling compression.
 * //www . j  a  v a 2  s . c o m
 * <p>
 * This method handles decompressing the response if the encoding is set to
 * {@code gzip} or {@code deflate}.
 * </p>
 * 
 * @param conn
 *        the URLConnection
 * @return the InputStream
 * @throws IOException
 *         if any IO error occurs
 */
protected InputStream getInputStreamFromURLConnection(URLConnection conn) throws IOException {
    String enc = conn.getContentEncoding();
    String type = conn.getContentType();

    if (conn instanceof HttpURLConnection) {
        HttpURLConnection httpConn = (HttpURLConnection) conn;
        if (httpConn.getResponseCode() < 200 || httpConn.getResponseCode() > 299) {
            log.info("Non-200 HTTP response from {}: {}", conn.getURL(), httpConn.getResponseCode());
        }
    }

    log.trace("Got content type [{}] encoded as [{}]", type, enc);

    InputStream is = conn.getInputStream();
    if ("gzip".equalsIgnoreCase(enc)) {
        is = new GZIPInputStream(is);
    } else if ("deflate".equalsIgnoreCase("enc")) {
        is = new DeflaterInputStream(is);
    }
    return is;
}

From source file:org.bimserver.client.Channel.java

public long checkin(String baseAddress, String token, long poid, String comment, long deserializerOid,
        boolean merge, boolean sync, long fileSize, String filename, InputStream inputStream)
        throws ServerException, UserException {
    String address = baseAddress + "/upload";
    DefaultHttpClient httpclient = new DefaultHttpClient();
    httpclient.addRequestInterceptor(new HttpRequestInterceptor() {
        public void process(final HttpRequest request, final HttpContext context)
                throws HttpException, IOException {
            if (!request.containsHeader("Accept-Encoding")) {
                request.addHeader("Accept-Encoding", "gzip");
            }//from  ww w  . jav a  2s . c o m
        }
    });

    httpclient.addResponseInterceptor(new HttpResponseInterceptor() {
        public void process(final HttpResponse response, final HttpContext context)
                throws HttpException, IOException {
            HttpEntity entity = response.getEntity();
            if (entity != null) {
                Header ceheader = entity.getContentEncoding();
                if (ceheader != null) {
                    HeaderElement[] codecs = ceheader.getElements();
                    for (int i = 0; i < codecs.length; i++) {
                        if (codecs[i].getName().equalsIgnoreCase("gzip")) {
                            response.setEntity(new GzipDecompressingEntity(response.getEntity()));
                            return;
                        }
                    }
                }
            }
        }
    });
    HttpPost httppost = new HttpPost(address);
    try {
        // TODO find some GzipInputStream variant that _compresses_ instead of _decompresses_ using deflate for now
        InputStreamBody data = new InputStreamBody(new DeflaterInputStream(inputStream), filename);

        MultipartEntity reqEntity = new MultipartEntity();
        reqEntity.addPart("data", data);
        reqEntity.addPart("token", new StringBody(token));
        reqEntity.addPart("deserializerOid", new StringBody("" + deserializerOid));
        reqEntity.addPart("merge", new StringBody("" + merge));
        reqEntity.addPart("poid", new StringBody("" + poid));
        reqEntity.addPart("comment", new StringBody("" + comment));
        reqEntity.addPart("sync", new StringBody("" + sync));
        reqEntity.addPart("compression", new StringBody("deflate"));
        httppost.setEntity(reqEntity);

        HttpResponse httpResponse = httpclient.execute(httppost);
        if (httpResponse.getStatusLine().getStatusCode() == 200) {
            JsonParser jsonParser = new JsonParser();
            JsonElement result = jsonParser
                    .parse(new JsonReader(new InputStreamReader(httpResponse.getEntity().getContent())));
            if (result instanceof JsonObject) {
                JsonObject jsonObject = (JsonObject) result;
                if (jsonObject.has("exception")) {
                    JsonObject exceptionJson = jsonObject.get("exception").getAsJsonObject();
                    String exceptionType = exceptionJson.get("__type").getAsString();
                    String message = exceptionJson.has("message") ? exceptionJson.get("message").getAsString()
                            : "unknown";
                    if (exceptionType.equals(UserException.class.getSimpleName())) {
                        throw new UserException(message);
                    } else if (exceptionType.equals(ServerException.class.getSimpleName())) {
                        throw new ServerException(message);
                    }
                } else {
                    return jsonObject.get("checkinid").getAsLong();
                }
            }
        }
    } catch (ClientProtocolException e) {
        LOGGER.error("", e);
    } catch (IOException e) {
        LOGGER.error("", e);
    }
    return -1;
}

From source file:com.hp.hpl.jena.sparql.engine.http.HttpQuery.java

private InputStream execCommon() throws QueryExceptionHTTP {
    try {/* w  w w  .j  a  va2 s.c  o  m*/
        responseCode = httpConnection.getResponseCode();
        responseMessage = Convert.decWWWForm(httpConnection.getResponseMessage());

        // 1xx: Informational 
        // 2xx: Success 
        // 3xx: Redirection 
        // 4xx: Client Error 
        // 5xx: Server Error 

        if (300 <= responseCode && responseCode < 400)
            throw new QueryExceptionHTTP(responseCode, responseMessage);

        // Other 400 and 500 - errors 

        if (responseCode >= 400) {

            InputStream x = httpConnection.getErrorStream();
            if (x != null) {
                //String ct = httpConnection.getContentType() ;
                //httpConnection.getContentEncoding() ;
                String str = FileUtils.readWholeFileAsUTF8(x);
                throw new QueryExceptionHTTP(responseCode, responseMessage + "\n" + str);
            } else
                throw new QueryExceptionHTTP(responseCode, responseMessage);
        }

        // Request succeeded
        //httpConnection.setReadTimeout(10) ;
        InputStream in = httpConnection.getInputStream();

        //Get the returned content type so we can expose this later via the getContentType() method
        //We strip any parameters off the returned content type e.g. ;charset=UTF-8 since code that
        //consumes our getContentType() method will expect a bare MIME type
        contentTypeResult = httpConnection.getContentType();
        if (contentTypeResult.contains(";")) {
            contentTypeResult = contentTypeResult.substring(0, contentTypeResult.indexOf(';'));
        }

        //If compression was enabled and we got a compressed response as indicated by the presence of
        //a Content-Encoding header we need to ensure the input stream is appropriately wrapped in
        //the relevant stream type but checking that the JVM hasn't been clever enough to do
        //this for us already
        String contentEnc = httpConnection.getContentEncoding();

        if (contentEnc != null) {
            if (contentEnc.equalsIgnoreCase("gzip")) {
                if (!(in instanceof GZIPInputStream)) {
                    in = new GZIPInputStream(in);
                }
            } else if (contentEnc.equalsIgnoreCase("deflate")) {
                if (!(in instanceof DeflaterInputStream)) {
                    in = new DeflaterInputStream(in);
                }
            }
        }

        if (false) {
            // Dump the header
            Map<String, List<String>> map = httpConnection.getHeaderFields();
            for (Iterator<String> iter = map.keySet().iterator(); iter.hasNext();) {
                String k = iter.next();
                List<String> v = map.get(k);
                System.out.println(k + " = " + v);
            }
        }

        // Dump response body
        if (false) {
            StringBuffer b = new StringBuffer(1000);
            byte[] chars = new byte[1000];
            while (true) {
                int x = in.read(chars);
                if (x < 0)
                    break;
                b.append(new String(chars, 0, x, FileUtils.encodingUTF8));
            }
            System.out.println(b.toString());
            System.out.flush();
            // Reset
            in = new ByteArrayInputStream(b.toString().getBytes(FileUtils.encodingUTF8));
        }

        // +++ WORKAROUND for badly behaved apps.
        // Apps sometimes call QueryExecution.close straight after .execSelect.
        // that results in some resuls being seen, not all of them => XMl parse errors.
        //            byte[] bytes = IO.readWholeFile(in) ;
        //            in = new ByteArrayInputStream(bytes) ;
        // +++ 

        return in;
    } catch (IOException ioEx) {
        throw new QueryExceptionHTTP(ioEx);
    } catch (QueryExceptionHTTP httpEx) {
        //We want to throw this upwards and not catch it in the next block and inadvertently rewrap it
        //since that can hide the real error details from the user
        throw httpEx;
    } catch (JenaException rdfEx) {
        throw new QueryExceptionHTTP(rdfEx);
    }
}

From source file:org.georchestra.security.Proxy.java

/**
 * For certain requests (OGC Web services mainly), the charset is absolutely
 * required. So for certain content types (xml-based normally) this method
 * is called to detect the charset of the data. This method is a slow way of
 * transferring data, so data of any significant size should not enter this
 * method./*  ww  w  . jav  a2s.c o  m*/
 */
private void doHandleRequestCharsetRequired(HttpServletRequest orignalRequest,
        HttpServletResponse finalResponse, RequestType requestType, HttpResponse proxiedResponse,
        String contentType) {

    InputStream streamFromServer = null;
    OutputStream streamToClient = null;

    try {

        /*
         * Here comes the tricky part because some host send files without
         * the charset in the header, therefore we do not know how they are
         * text encoded. It can result in serious issues on IE browsers when
         * parsing those files. There is a workaround which consists to read
         * the encoding within the file. It is made possible because this
         * proxy mainly forwards xml files. They all have the encoding
         * attribute in the first xml node.
         *
         * This is implemented as follows:
         *
         * A. The content type provides a charset: Nothing special, just
         * send back the stream to the client B. There is no charset
         * provided: The encoding has to be extracted from the file. The
         * file is read in ASCII, which is common to many charsets, like
         * that the encoding located in the first not can be retrieved. Once
         * the charset is found, the content-type header is overridden and
         * the charset is appended.
         *
         * /!\ Special case: whenever data are compressed in gzip/deflate
         * the stream has to be uncompressed and re-compressed
         */

        boolean isCharsetKnown = proxiedResponse.getEntity().getContentType().getValue().toLowerCase()
                .contains("charset");
        // String contentEncoding =
        // getContentEncoding(proxiedResponse.getAllHeaders());
        String contentEncoding = getContentEncoding(proxiedResponse.getHeaders("Content-Encoding"));

        if (logger.isDebugEnabled()) {

            String cskString = "\tisCharSetKnown=" + isCharsetKnown;
            String cEString = "\tcontentEncoding=" + contentEncoding;
            logger.debug("Charset is required so verifying that it has been added to the headers\n" + cskString
                    + "\n" + cEString);
        }

        if (contentEncoding == null || isCharsetKnown) {
            // A simple stream can do the job for data that is not in
            // content encoded
            // but also for data content encoded with a known charset
            streamFromServer = proxiedResponse.getEntity().getContent();
            streamToClient = finalResponse.getOutputStream();
        } else if (!isCharsetKnown
                && ("gzip".equalsIgnoreCase(contentEncoding) || "x-gzip".equalsIgnoreCase(contentEncoding))) {
            // the charset is unknown and the data are compressed in gzip
            // we add the gzip wrapper to be able to read/write the stream
            // content
            streamFromServer = new GZIPInputStream(proxiedResponse.getEntity().getContent());
            streamToClient = new GZIPOutputStream(finalResponse.getOutputStream());
        } else if ("deflate".equalsIgnoreCase(contentEncoding) && !isCharsetKnown) {
            // same but with deflate
            streamFromServer = new DeflaterInputStream(proxiedResponse.getEntity().getContent());
            streamToClient = new DeflaterOutputStream(finalResponse.getOutputStream());
        } else {
            doHandleRequest(orignalRequest, finalResponse, requestType, proxiedResponse);
            return;
        }

        byte[] buf = new byte[1024]; // read maximum 1024 bytes
        int len; // number of bytes read from the stream
        boolean first = true; // helps to find the encoding once and only
                              // once
        String s = ""; // piece of file that should contain the encoding
        while ((len = streamFromServer.read(buf)) > 0) {

            if (first && !isCharsetKnown) {
                // charset is unknown try to find it in the file content
                for (int i = 0; i < len; i++) {
                    s += (char) buf[i]; // get the beginning of the file as
                                        // ASCII
                }
                // s has to be long enough to contain the encoding
                if (s.length() > 200) {

                    if (logger.isTraceEnabled()) {
                        logger.trace("attempting to read charset from: " + s);
                    }
                    String charset = getCharset(s); // extract charset

                    if (charset == null) {
                        if (logger.isTraceEnabled()) {
                            logger.trace("unable to find charset from raw ASCII data.  Trying to unzip it");
                        }

                        // the charset cannot be found, IE users must be
                        // warned
                        // that the request cannot be fulfilled, nothing
                        // good would happen otherwise
                    }
                    if (charset == null) {
                        String guessedCharset = null;
                        if (logger.isDebugEnabled()) {
                            logger.debug(
                                    "unable to find charset so using the first one from the accept-charset request header");
                        }
                        String calculateDefaultCharset = calculateDefaultCharset(orignalRequest);
                        if (calculateDefaultCharset != null) {
                            guessedCharset = calculateDefaultCharset;
                            if (logger.isDebugEnabled()) {
                                logger.debug("hopefully the server responded with this charset: "
                                        + calculateDefaultCharset);
                            }
                        } else {
                            guessedCharset = defaultCharset;
                            if (logger.isDebugEnabled()) {
                                logger.debug("unable to find charset, so using default:" + defaultCharset);
                            }
                        }
                        String adjustedContentType = proxiedResponse.getEntity().getContentType().getValue()
                                + ";charset=" + guessedCharset;
                        finalResponse.setHeader("Content-Type", adjustedContentType);
                        first = false; // we found the encoding, don't try
                                       // to do it again
                        finalResponse.setCharacterEncoding(guessedCharset);

                    } else {
                        if (logger.isDebugEnabled()) {
                            logger.debug("found charset: " + charset);
                        }
                        String adjustedContentType = proxiedResponse.getEntity().getContentType().getValue()
                                + ";charset=" + charset;
                        finalResponse.setHeader("Content-Type", adjustedContentType);
                        first = false; // we found the encoding, don't try
                                       // to do it again
                        finalResponse.setCharacterEncoding(charset);
                    }
                }
            }

            // for everyone, the stream is just forwarded to the client
            streamToClient.write(buf, 0, len);
        }

    } catch (IOException e) {
        // connection problem with the host
        e.printStackTrace();
    } finally {
        IOException exc = close(streamFromServer);
        exc = close(streamToClient, exc);
        if (exc != null) {
            logger.error("Error closing streams", exc);
        }
    }
}

From source file:org.archive.util.Recorder.java

/**
 * Get a replay cued up for the 'content' (after all leading headers)
 * /*from   w  w  w .  j ava2 s  .c  o m*/
 * @return A replay input stream.
 * @throws IOException
 */
public InputStream getContentReplayInputStream() throws IOException {
    InputStream entityStream = getEntityReplayInputStream();
    if (StringUtils.isEmpty(contentEncoding)) {
        return entityStream;
    } else if ("gzip".equalsIgnoreCase(contentEncoding) || "x-gzip".equalsIgnoreCase(contentEncoding)) {
        try {
            return new GZIPInputStream(entityStream);
        } catch (IOException ioe) {
            logger.log(Level.WARNING, "gzip problem; using raw entity instead", ioe);
            IOUtils.closeQuietly(entityStream); // close partially-read stream
            return getEntityReplayInputStream();
        }
    } else if ("deflate".equalsIgnoreCase(contentEncoding)) {
        return new DeflaterInputStream(entityStream);
    } else if ("identity".equalsIgnoreCase(contentEncoding) || "none".equalsIgnoreCase(contentEncoding)) {
        return entityStream;
    } else {
        // shouldn't be reached given check on setContentEncoding
        logger.log(Level.INFO,
                "Unknown content-encoding '" + contentEncoding + "' declared; using raw entity instead");
        return entityStream;
    }
}

From source file:org.diorite.nbt.NbtInputStream.java

/**
 * Construct new NbtInputStream for deflated input stream and limiter.
 *
 * @param in      input stream to be used.
 * @param limiter limiter to be used.//  ww  w.  j av  a  2  s. co m
 *
 * @return new NbtInputStream.
 */
public static NbtInputStream fromDeflater(final InputStream in, final NbtLimiter limiter) {
    return new NbtInputStream(new DataInputStream(
            new BufferedInputStream(new DeflaterInputStream(new NbtInputLimitedStream(in, limiter)))));
}

From source file:org.fejoa.library.messages.ZipEnvelope.java

static public InputStream zip(InputStream data, boolean isRawData) throws JSONException, IOException {
    JSONObject object = new JSONObject();
    object.put(Envelope.PACK_TYPE_KEY, ZIP_TYPE);
    if (isRawData)
        object.put(Envelope.CONTAINS_DATA_KEY, 1);
    object.put(ZIP_FORMAT_KEY, ZIP_FORMAT);
    String header = object.toString() + "\n";

    return new SequenceInputStream(new ByteArrayInputStream(header.getBytes()), new DeflaterInputStream(data));
}

From source file:org.javaweb.net.HttpResponseParser.java

/**
 * ?Http?? ??Transfer-Encoding  Content-Encoding ?chunked?
 *
 * @throws IOException//from   w w w.j ava2  s .  co  m
 */
private void parseHttpResponseBodyInputStream() throws IOException {
    InputStream bodyInputStream = null;

    // ?chunked
    if (this.headerMap.containsKey("Transfer-Encoding")) {
        String transferEncoding = (String) this.headerMap.get("Transfer-Encoding");

        if (StringUtils.isNotEmpty(transferEncoding) && "chunked".equalsIgnoreCase(transferEncoding)) {
            bodyInputStream = new ChunkedInputStream(dis);
        } else {
            bodyInputStream = dis;
        }
    } else {
        bodyInputStream = dis;
    }

    // ?gzipdeflate
    if (this.headerMap.containsKey("Content-Encoding")) {
        String contentEncoding = (String) this.headerMap.get("Content-Encoding");
        InputStream compressInputStream = null;

        try {
            if ("gzip".equalsIgnoreCase(contentEncoding)) {
                compressInputStream = new GZIPInputStream(bodyInputStream);
            } else if ("deflate".equalsIgnoreCase(contentEncoding)) {
                compressInputStream = new DeflaterInputStream(bodyInputStream);
            }
        } catch (IOException e) {
            throw e;
        }

        if (compressInputStream != null) {
            bodyInputStream = compressInputStream;
        }
    }

    this.httpBodyInputStream = bodyInputStream;
}

From source file:org.thelq.stackexchange.api.StackClient.java

protected InputStream createResponse(URI uri) {
    try {//from   ww  w .  j a v a2 s  .c o  m
        HttpURLConnection connection = (HttpURLConnection) uri.toURL().openConnection();
        connection.setDoInput(true);
        connection.connect();
        InputStream connectionInput = (connection.getResponseCode() >= 400) ? connection.getErrorStream()
                : connection.getInputStream();
        if (connection.getContentEncoding().equalsIgnoreCase("gzip"))
            return new GZIPInputStream(connectionInput);
        else if (connection.getContentEncoding().equalsIgnoreCase("deflate"))
            return new DeflaterInputStream(connectionInput);
        else
            return connectionInput;
    } catch (Exception ex) {
        throw new RuntimeException("Cannot create response", ex);
    }
}

From source file:ubicrypt.core.crypto.AESGCMTest.java

@Test
public void testName() throws Exception {
    final byte[] key = AESGCM.rndKey();
    final ByteArrayInputStream bis = new ByteArrayInputStream("Ciao".getBytes());
    final InputStream cipherStream = new DeflaterInputStream(encryptIs(key, bis));
    final InputStream decrypt2InputStream = decryptIs(key, new InflaterInputStream(cipherStream));
    Assertions.assertThat(IOUtils.toString(decrypt2InputStream)).isEqualTo("Ciao");
}