Example usage for java.util.zip InflaterInputStream InflaterInputStream

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

Introduction

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

Prototype

public InflaterInputStream(InputStream in) 

Source Link

Document

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

Usage

From source file:de.burlov.amazon.s3.dirsync.DirSync.java

private void downloadFile(File target, String s3key) throws IOException, S3ServiceException {
    InputStream in = downloadData(s3key);
    if (in == null) {
        throw new IOException("No data found");
    }/*from  w w w  .ja va 2 s .c  o m*/
    in = new InflaterInputStream(new CryptInputStream(in, cipher, getDataEncryptionKey()));
    File temp = File.createTempFile("dirsync", null);
    FileOutputStream fout = new FileOutputStream(temp);
    try {
        IOUtils.copy(in, fout);
        if (target.exists()) {
            target.delete();
        }
        IOUtils.closeQuietly(fout);
        IOUtils.closeQuietly(in);
        FileUtils.moveFile(temp, target);
    } catch (IOException e) {
        fetchStream(in);
        throw e;
    } finally {
        IOUtils.closeQuietly(fout);
        IOUtils.closeQuietly(in);
    }
}

From source file:net.oddsoftware.android.feedscribe.data.FeedManager.java

void downloadFeedHttp(Feed feed, FeedStatus feedStatus, ArrayList<FeedItem> feedItems,
        ArrayList<Enclosure> enclosures) {
    try {/*from   w  w w  . ja  v a2 s.c o  m*/
        // use apache http client lib to set parameters from feedStatus
        DefaultHttpClient client = new DefaultHttpClient();

        // set up proxy handler
        ProxySelectorRoutePlanner routePlanner = new ProxySelectorRoutePlanner(
                client.getConnectionManager().getSchemeRegistry(), ProxySelector.getDefault());
        client.setRoutePlanner(routePlanner);

        HttpGet request = new HttpGet(feed.mURL);

        HttpContext httpContext = new BasicHttpContext();

        request.setHeader("User-Agent", USER_AGENT);

        // send etag if we have it
        if (feedStatus.mETag.length() > 0) {
            request.setHeader("If-None-Match", feedStatus.mETag);
        }

        // send If-Modified-Since if we have it
        if (feedStatus.mLastModified.getTime() > 0) {
            SimpleDateFormat dateFormat = new SimpleDateFormat("EEE', 'dd' 'MMM' 'yyyy' 'HH:mm:ss' GMT'",
                    Locale.US);
            dateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
            String formattedTime = dateFormat.format(feedStatus.mLastModified);
            // If-Modified-Since: Sat, 29 Oct 1994 19:43:31 GMT
            request.setHeader("If-Modified-Since", formattedTime);
        }

        request.setHeader("Accept-Encoding", "gzip,deflate");
        HttpResponse response = client.execute(request, httpContext);

        if (mLog.d())
            mLog.d("http request: " + feed.mURL);
        if (mLog.d())
            mLog.d("http response code: " + response.getStatusLine());

        InputStream inputStream = null;

        StatusLine status = response.getStatusLine();
        HttpEntity entity = response.getEntity();

        if (entity != null) {
            inputStream = entity.getContent();
        }

        try {
            if (entity != null && status.getStatusCode() == 200) {
                Header encodingHeader = entity.getContentEncoding();

                if (encodingHeader != null) {
                    if (encodingHeader.getValue().equalsIgnoreCase("gzip")) {
                        inputStream = new GZIPInputStream(inputStream);
                    } else if (encodingHeader.getValue().equalsIgnoreCase("deflate")) {
                        inputStream = new InflaterInputStream(inputStream);
                    }
                }

                // remove caching attributes to be replaced with new ones
                feedStatus.mETag = "";
                feedStatus.mLastModified.setTime(0);
                feedStatus.mTTL = 0;

                boolean success = parseFeed(inputStream, feed, feedStatus, feedItems, enclosures);

                if (success) {
                    // if the parse was ok, update these attributes
                    // ETag: "6050003-78e5-4981d775e87c0"
                    Header etagHeader = response.getFirstHeader("ETag");
                    if (etagHeader != null) {
                        if (etagHeader.getValue().length() < MAX_ETAG_LENGTH) {
                            feedStatus.mETag = etagHeader.getValue();
                        } else {
                            mLog.e("etag length was too big: " + etagHeader.getValue().length());
                        }
                    }

                    // Last-Modified: Fri, 24 Dec 2010 00:57:11 GMT
                    Header lastModifiedHeader = response.getFirstHeader("Last-Modified");
                    if (lastModifiedHeader != null) {
                        try {
                            feedStatus.mLastModified = parseRFC822Date(lastModifiedHeader.getValue());
                        } catch (ParseException exc) {
                            mLog.e("unable to parse date", exc);
                        }
                    }

                    HttpUriRequest currentReq = (HttpUriRequest) httpContext
                            .getAttribute(ExecutionContext.HTTP_REQUEST);
                    HttpHost currentHost = (HttpHost) httpContext
                            .getAttribute(ExecutionContext.HTTP_TARGET_HOST);
                    String currentUrl = currentHost.toURI() + currentReq.getURI();

                    mLog.w("loaded redirect from " + request.getURI().toString() + " to " + currentUrl);

                    feedStatus.mLastURL = currentUrl;
                }
            } else {
                if (status.getStatusCode() == 304) {
                    mLog.d("received 304 not modified");
                }
            }
        } finally {
            if (inputStream != null) {
                inputStream.close();
            }
        }
    } catch (IOException exc) {
        mLog.e("error downloading feed " + feed.mURL, exc);
    }
}

From source file:de.burlov.amazon.s3.dirsync.DirSync.java

private Object downloadObject(String s3key, byte[] encKey) throws IOException {
    InputStream in = downloadData(s3key);
    if (in == null) {
        return null;
    }//  w w  w. j av  a2 s  .  c o m
    CountingInputStream cin = new CountingInputStream(
            new InflaterInputStream(new CryptInputStream(in, cipher, encKey)));
    ObjectInputStream oin = new ObjectInputStream(cin);
    try {
        Object o = oin.readObject();
        transferredData += cin.getByteCount();
        return o;
    } catch (ClassNotFoundException e) {
        /*
         * sollte eigentlich nie vorkommen
         */
        throw new IOException(e.getLocalizedMessage());
    } finally {
        IOUtils.closeQuietly(in);
    }
}

From source file:com.funambol.transport.http.server.Sync4jServlet.java

/**
 * Returns the content of HTTP request.//from w ww. j a  v  a2 s  . c  o  m
 * Uncompresses the request if the Content-Encoding is gzip or deflate.
 * Updates the Content-Length with the length of the uncompressed request.
 *
 * @param httpRequest the HttpServletRequest
 * @param contentEncoding the content encoding
 * @param requestTime the time in which the request is arrived to servlet
 * @param sessionId the session identifier
 *
 * @return requestData the uncompressed request content
 * @throws java.io.IOException if an error occurs
 */
private byte[] getRequestContent(HttpServletRequest httpRequest, String contentEncoding, long requestTime,
        String sessionId) throws IOException {

    byte[] requestData = null;
    InputStream in = null;

    try {

        in = httpRequest.getInputStream();

        int contentLength = httpRequest.getContentLength();
        if (contentLength <= 0) {
            contentLength = SIZE_INPUT_BUFFER;
        }

        if (logMessages) {
            //
            // Read the compressed request data to log it
            //
            requestData = IOTools.readContent(in, contentLength);
            logRequest(httpRequest, requestData, requestTime, sessionId);

            //
            // Create a new InputStream to pass at the decompressor
            //
            in = new ByteArrayInputStream(requestData);
        }

        if (contentEncoding != null) {
            if (contentEncoding.equals(COMPRESSION_TYPE_GZIP)) {

                if (log.isTraceEnabled()) {
                    log.trace("Reading the request using: " + COMPRESSION_TYPE_GZIP);
                }
                in = new GZIPInputStream(in);

            } else if (contentEncoding.equals(COMPRESSION_TYPE_DEFLATE)) {

                if (log.isTraceEnabled()) {
                    log.trace("Reading the request using: " + COMPRESSION_TYPE_DEFLATE);
                }
                in = new InflaterInputStream(in);
            }

            String uncompressedContentLength = httpRequest.getHeader("Uncompressed-Content-Length");

            if (uncompressedContentLength != null) {
                contentLength = Integer.parseInt(uncompressedContentLength);
            } else {
                contentLength = SIZE_INPUT_BUFFER;
            }

        }

        if (!logMessages || contentEncoding != null) {
            requestData = IOTools.readContent(in, contentLength);
        }

    } finally {
        if (in != null) {
            in.close();
        }
    }

    return requestData;
}

From source file:github.daneren2005.dsub.util.FileUtil.java

@TargetApi(Build.VERSION_CODES.GINGERBREAD)
public static <T extends Serializable> T deserializeCompressed(Context context, String fileName,
        Class<T> tClass) {/*from ww  w  .ja  v a  2 s.  c  om*/
    Input in = null;
    try {
        RandomAccessFile file = new RandomAccessFile(context.getCacheDir() + "/" + fileName, "r");

        in = new Input(new InflaterInputStream(new FileInputStream(file.getFD())));
        synchronized (kryo) {
            T result = kryo.readObject(in, tClass);
            return result;
        }
    } catch (FileNotFoundException e) {
        // Different error message
        Log.w(TAG, "No serialization compressed for object from " + fileName);
        return null;
    } catch (Throwable x) {
        Log.w(TAG, "Failed to deserialize compressed object from " + fileName);
        return null;
    } finally {
        Util.close(in);
    }
}

From source file:org.torproject.collector.relaydescs.RelayDescriptorDownloader.java

/**
 * Attempts to download one or more descriptors identified by a resource
 * string from a directory authority and passes the returned
 * descriptor(s) to the <code>RelayDescriptorParser</code> upon success.
 * Returns the number of descriptors contained in the reply. Throws an
 * <code>IOException</code> if something goes wrong while downloading.
 *//*from w  w w .ja  v  a  2 s .  c  o m*/
private int downloadResourceFromAuthority(String authority, String resource) throws IOException {
    byte[] allData = null;
    this.requestsByAuthority.put(authority, this.requestsByAuthority.get(authority) + 1);
    /* TODO Disable compressed downloads for extra-info descriptors,
     * because zlib decompression doesn't work correctly. Figure out why
     * this is and fix it. */
    String fullUrl = "http://" + authority + resource
            + (this.downloadCompressed && !resource.startsWith("/tor/extra/") ? ".z" : "");
    URL url = new URL(fullUrl);
    HttpURLConnection huc = (HttpURLConnection) url.openConnection();
    huc.setRequestMethod("GET");
    huc.connect();
    int response = huc.getResponseCode();
    if (response == 200) {
        BufferedInputStream in = this.downloadCompressed && !resource.startsWith("/tor/extra/")
                ? new BufferedInputStream(new InflaterInputStream(huc.getInputStream()))
                : new BufferedInputStream(huc.getInputStream());
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        int len;
        byte[] data = new byte[1024];
        while ((len = in.read(data, 0, 1024)) >= 0) {
            baos.write(data, 0, len);
        }
        in.close();
        allData = baos.toByteArray();
    }
    logger.debug("Downloaded " + fullUrl + " -> " + response + " (" + (allData == null ? 0 : allData.length)
            + " bytes)");
    int receivedDescriptors = 0;
    if (allData != null) {
        if (resource.startsWith("/tor/status-vote/current/")) {
            this.rdp.parse(allData);
            receivedDescriptors = 1;
        } else if (resource.startsWith("/tor/server/") || resource.startsWith("/tor/extra/")) {
            if (resource.equals("/tor/server/all") || resource.equals("/tor/extra/all")) {
                this.lastDownloadedAllDescriptors.put(authority, this.currentTimestamp);
            }
            String ascii = null;
            try {
                ascii = new String(allData, "US-ASCII");
            } catch (UnsupportedEncodingException e) {
                /* No way that US-ASCII is not supported. */
            }
            int start = -1;
            int sig = -1;
            int end = -1;
            String startToken = resource.startsWith("/tor/server/") ? "router " : "extra-info ";
            String sigToken = "\nrouter-signature\n";
            String endToken = "\n-----END SIGNATURE-----\n";
            while (end < ascii.length()) {
                start = ascii.indexOf(startToken, end);
                if (start < 0) {
                    break;
                }
                sig = ascii.indexOf(sigToken, start);
                if (sig < 0) {
                    break;
                }
                sig += sigToken.length();
                end = ascii.indexOf(endToken, sig);
                if (end < 0) {
                    break;
                }
                end += endToken.length();
                byte[] descBytes = new byte[end - start];
                System.arraycopy(allData, start, descBytes, 0, end - start);
                this.rdp.parse(descBytes);
                receivedDescriptors++;
            }
        } else if (resource.startsWith("/tor/micro/")) {
            /* TODO We need to parse microdescriptors ourselves, rather than
             * RelayDescriptorParser, because only we know the valid-after
             * time(s) of microdesc consensus(es) containing this
             * microdescriptor.  However, this breaks functional abstraction
             * pretty badly. */
            SimpleDateFormat parseFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            parseFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
            String ascii = null;
            try {
                ascii = new String(allData, "US-ASCII");
            } catch (UnsupportedEncodingException e) {
                /* No way that US-ASCII is not supported. */
            }
            int start = -1;
            int end = -1;
            String startToken = "onion-key\n";
            while (end < ascii.length()) {
                start = ascii.indexOf(startToken, end);
                if (start < 0) {
                    break;
                }
                end = ascii.indexOf(startToken, start + 1);
                if (end < 0) {
                    end = ascii.length();
                    if (end <= start) {
                        break;
                    }
                }
                byte[] descBytes = new byte[end - start];
                System.arraycopy(allData, start, descBytes, 0, end - start);
                String digest256Base64 = Base64.encodeBase64String(DigestUtils.sha256(descBytes))
                        .replaceAll("=", "");
                if (!this.microdescriptorKeys.containsKey(digest256Base64)) {
                    continue;
                }
                String digest256Hex = DigestUtils.sha256Hex(descBytes);
                for (String microdescriptorKey : this.microdescriptorKeys.get(digest256Base64)) {
                    String validAfterTime = microdescriptorKey.split(",")[1];
                    try {
                        long validAfter = parseFormat.parse(validAfterTime).getTime();
                        this.rdp.storeMicrodescriptor(descBytes, digest256Hex, digest256Base64, validAfter);
                    } catch (ParseException e) {
                        logger.warn("Could not parse " + "valid-after time '" + validAfterTime + "' in "
                                + "microdescriptor key. Not storing microdescriptor.", e);
                    }
                }
                receivedDescriptors++;
            }
        }
    }
    return receivedDescriptors;
}

From source file:com.enonic.esl.xml.XMLTool.java

public static Document deflatedBytesToDocument(byte[] bytes) {
    ByteArrayInputStream in = new ByteArrayInputStream(bytes);
    InflaterInputStream iis = new InflaterInputStream(in);
    return domparse(iis);
}

From source file:org.apache.xmlgraphics.image.codec.png.PNGImageDecoder.java

private void parse_zTXt_chunk(final PNGChunk chunk) {

    int textIndex = 0;
    final StringBuilder key = new StringBuilder();
    byte b;//  w  w w .j ava  2 s  .  co m
    while ((b = chunk.getByte(textIndex++)) != 0) {
        key.append((char) b);
    }
    /* int method = */chunk.getByte(textIndex++);

    final StringBuilder value = new StringBuilder();
    try {
        final int length = chunk.getLength() - textIndex;
        final byte[] data = chunk.getData();
        final InputStream cis = new ByteArrayInputStream(data, textIndex, length);
        final InputStream iis = new InflaterInputStream(cis);

        int c;
        while ((c = iis.read()) != -1) {
            value.append((char) c);
        }

        this.ztextKeys.add(key.toString());
        this.ztextStrings.add(value.toString());
    } catch (final Exception e) {
        log.error("Exception", e);
    }
}

From source file:org.apache.xmlgraphics.image.codec.png.PNGRed.java

private void parse_zTXt_chunk(final PNGChunk chunk) {
    final StringBuilder key = new StringBuilder();
    final StringBuilder value = new StringBuilder();
    byte b;//w  ww.j a  v  a  2  s  .c o  m

    int textIndex = 0;
    while ((b = chunk.getByte(textIndex++)) != 0) {
        key.append((char) b);
    }
    /* int method = */chunk.getByte(textIndex++);

    try {
        final int length = chunk.getLength() - textIndex;
        final byte[] data = chunk.getData();
        final InputStream cis = new ByteArrayInputStream(data, textIndex, length);
        final InputStream iis = new InflaterInputStream(cis);

        int c;
        while ((c = iis.read()) != -1) {
            value.append((char) c);
        }

        this.ztextKeys.add(key.toString());
        this.ztextStrings.add(value.toString());
    } catch (final Exception e) {
        log.error("Exception", e);
    }
}

From source file:nl.nn.adapterframework.jdbc.JdbcTransactionalStorage.java

private Object retrieveObject(ResultSet rs, int columnIndex, boolean compressed)
        throws ClassNotFoundException, JdbcException, IOException, SQLException {
    InputStream blobStream = null;
    try {//from w  ww . j a  va2 s .c  o  m
        Blob blob = rs.getBlob(columnIndex);
        if (blob == null) {
            return null;
        }
        if (compressed) {
            blobStream = new InflaterInputStream(JdbcUtil.getBlobInputStream(blob, columnIndex + ""));
        } else {
            blobStream = JdbcUtil.getBlobInputStream(blob, columnIndex + "");
        }
        ObjectInputStream ois = new ObjectInputStream(blobStream);
        Object result = ois.readObject();
        ois.close();
        return result;
    } finally {
        if (blobStream != null) {
            blobStream.close();
        }
    }
}