Example usage for java.util.zip GZIPInputStream GZIPInputStream

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

Introduction

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

Prototype

public GZIPInputStream(InputStream in) throws IOException 

Source Link

Document

Creates a new input stream with a default buffer size.

Usage

From source file:com.marklogic.contentpump.CompressedAggXMLReader.java

protected void initStreamReader(InputSplit inSplit) throws IOException, InterruptedException {
    setFile(((FileSplit) inSplit).getPath());
    FSDataInputStream fileIn = fs.open(file);
    String codecString = conf.get(ConfigConstants.CONF_INPUT_COMPRESSION_CODEC,
            CompressionCodec.ZIP.toString());
    if (codecString.equalsIgnoreCase(CompressionCodec.ZIP.toString())) {
        zipIn = new ZipInputStream(fileIn);
        codec = CompressionCodec.ZIP;//  www .  j  a  v a  2  s . com
        while (true) {
            try {
                currZipEntry = ((ZipInputStream) zipIn).getNextEntry();
                if (currZipEntry == null) {
                    break;
                }
                if (currZipEntry.getSize() != 0) {
                    subId = currZipEntry.getName();
                    break;
                }
            } catch (IllegalArgumentException e) {
                LOG.warn("Skipped a zip entry in : " + file.toUri() + ", reason: " + e.getMessage());
            }
        }
        if (currZipEntry == null) { // no entry in zip
            LOG.warn("No valid entry in zip:" + file.toUri());
            return;
        }
        ByteArrayOutputStream baos;
        long size = currZipEntry.getSize();
        if (size == -1) {
            baos = new ByteArrayOutputStream();
        } else {
            baos = new ByteArrayOutputStream((int) size);
        }
        int nb;
        while ((nb = zipIn.read(buf, 0, buf.length)) != -1) {
            baos.write(buf, 0, nb);
        }
        try {
            start = 0;
            end = baos.size();
            xmlSR = f.createXMLStreamReader(new ByteArrayInputStream(baos.toByteArray()), encoding);
        } catch (XMLStreamException e) {
            LOG.error(e.getMessage(), e);
        }

    } else if (codecString.equalsIgnoreCase(CompressionCodec.GZIP.toString())) {
        zipIn = new GZIPInputStream(fileIn);
        codec = CompressionCodec.GZIP;
        try {
            start = 0;
            end = inSplit.getLength();
            xmlSR = f.createXMLStreamReader(zipIn, encoding);
        } catch (XMLStreamException e) {
            LOG.error(e.getMessage(), e);
        }
    } else {
        throw new UnsupportedOperationException("Unsupported codec: " + codec.name());
    }
    if (useAutomaticId) {
        idGen = new IdGenerator(file.toUri().getPath() + "-" + ((FileSplit) inSplit).getStart());
    }
}

From source file:org.myframe.http.FileRequest.java

public byte[] handleResponse(HttpResponse response) throws IOException, KJHttpException {
    HttpEntity entity = response.getEntity();
    long fileSize = entity.getContentLength();
    if (fileSize <= 0) {
        MLoger.debug("Response doesn't present Content-Length!");
    }/* w ww  .j  a v a2  s . co m*/

    long downloadedSize = mTemporaryFile.length();
    boolean isSupportRange = HttpUtils.isSupportRange(response);
    if (isSupportRange) {
        fileSize += downloadedSize;

        String realRangeValue = HttpUtils.getHeader(response, "Content-Range");
        if (!TextUtils.isEmpty(realRangeValue)) {
            String assumeRangeValue = "bytes " + downloadedSize + "-" + (fileSize - 1);
            if (TextUtils.indexOf(realRangeValue, assumeRangeValue) == -1) {
                throw new IllegalStateException("The Content-Range Header is invalid Assume[" + assumeRangeValue
                        + "] vs Real[" + realRangeValue + "], " + "please remove the temporary file ["
                        + mTemporaryFile + "].");
            }
        }
    }

    if (fileSize > 0 && mStoreFile.length() == fileSize) {
        mStoreFile.renameTo(mTemporaryFile);
        mRequestQueue.getConfig().mDelivery.postDownloadProgress(this, fileSize, fileSize);
        return null;
    }

    RandomAccessFile tmpFileRaf = new RandomAccessFile(mTemporaryFile, "rw");
    if (isSupportRange) {
        tmpFileRaf.seek(downloadedSize);
    } else {
        tmpFileRaf.setLength(0);
        downloadedSize = 0;
    }

    try {
        InputStream in = entity.getContent();
        if (HttpUtils.isGzipContent(response) && !(in instanceof GZIPInputStream)) {
            in = new GZIPInputStream(in);
        }
        byte[] buffer = new byte[6 * 1024]; // 6K buffer
        int offset;

        while ((offset = in.read(buffer)) != -1) {
            tmpFileRaf.write(buffer, 0, offset);

            downloadedSize += offset;
            mRequestQueue.getConfig().mDelivery.postDownloadProgress(this, fileSize, downloadedSize);

            if (isCanceled()) {
                break;
            }
        }
    } finally {
        try {
            if (entity != null)
                entity.consumeContent();
        } catch (Exception e) {
            MLoger.debug("Error occured when calling consumingContent");
        }
        tmpFileRaf.close();
    }
    return null;
}

From source file:com.panet.imeta.www.SlaveServerTransStatus.java

public SlaveServerTransStatus(Node transStatusNode) {
    this();//from w w w. ja va 2  s.c  o m
    transName = XMLHandler.getTagValue(transStatusNode, "transname");
    statusDescription = XMLHandler.getTagValue(transStatusNode, "status_desc");
    errorDescription = XMLHandler.getTagValue(transStatusNode, "error_desc");
    paused = "Y".equalsIgnoreCase(XMLHandler.getTagValue(transStatusNode, "paused"));

    Node statusListNode = XMLHandler.getSubNode(transStatusNode, "stepstatuslist");
    int nr = XMLHandler.countNodes(statusListNode, StepStatus.XML_TAG);
    for (int i = 0; i < nr; i++) {
        Node stepStatusNode = XMLHandler.getSubNodeByNr(statusListNode, StepStatus.XML_TAG, i);
        StepStatus stepStatus = new StepStatus(stepStatusNode);
        stepStatusList.add(stepStatus);
    }

    String loggingString64 = XMLHandler.getTagValue(transStatusNode, "logging_string");
    // This is a Base64 encoded GZIP compressed stream of data.
    try {
        byte[] bytes = new byte[] {};
        if (loggingString64 != null)
            bytes = Base64.decodeBase64(loggingString64.getBytes());
        if (bytes.length > 0) {
            ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
            GZIPInputStream gzip = new GZIPInputStream(bais);
            int c;
            StringBuffer buffer = new StringBuffer();
            while ((c = gzip.read()) != -1)
                buffer.append((char) c);
            gzip.close();
            loggingString = buffer.toString();
        } else {
            loggingString = "";
        }
    } catch (IOException e) {
        loggingString = "Unable to decode logging from remote server : " + e.toString() + Const.CR
                + Const.getStackTracker(e);
    }

    // get the result object, if there is any...
    //
    Node resultNode = XMLHandler.getSubNode(transStatusNode, Result.XML_TAG);
    if (resultNode != null) {
        try {
            result = new Result(resultNode);
        } catch (IOException e) {
            loggingString += "Unable to serialize result object as XML" + Const.CR + Const.getStackTracker(e)
                    + Const.CR;
        }
    }
}

From source file:com.metamx.druid.indexing.common.index.StaticS3FirehoseFactory.java

@Override
public Firehose connect() throws IOException {
    Preconditions.checkNotNull(s3Client, "null s3Client");

    return new Firehose() {
        LineIterator lineIterator = null;
        final Queue<URI> objectQueue = Lists.newLinkedList(uris);

        // Rolls over our streams and iterators to the next file, if appropriate
        private void maybeNextFile() throws Exception {

            if (lineIterator == null || !lineIterator.hasNext()) {

                // Close old streams, maybe.
                if (lineIterator != null) {
                    lineIterator.close();
                }/*w  ww  .j  a  v a 2  s .co m*/

                // Open new streams, maybe.
                final URI nextURI = objectQueue.poll();
                if (nextURI != null) {

                    final String s3Bucket = nextURI.getAuthority();
                    final S3Object s3Object = new S3Object(
                            nextURI.getPath().startsWith("/") ? nextURI.getPath().substring(1)
                                    : nextURI.getPath());

                    log.info("Reading from bucket[%s] object[%s] (%s)", s3Bucket, s3Object.getKey(), nextURI);

                    int ntry = 0;
                    try {
                        final InputStream innerInputStream = s3Client.getObject(s3Bucket, s3Object.getKey())
                                .getDataInputStream();

                        final InputStream outerInputStream = s3Object.getKey().endsWith(".gz")
                                ? new GZIPInputStream(innerInputStream)
                                : innerInputStream;

                        lineIterator = IOUtils.lineIterator(
                                new BufferedReader(new InputStreamReader(outerInputStream, Charsets.UTF_8)));
                    } catch (IOException e) {
                        log.error(e,
                                "Exception reading from bucket[%s] object[%s] (try %d) (sleeping %d millis)",
                                s3Bucket, s3Object.getKey(), ntry, retryMillis);

                        ntry++;
                        if (ntry <= retryCount) {
                            Thread.sleep(retryMillis);
                        }
                    }

                }
            }

        }

        @Override
        public boolean hasMore() {
            try {
                maybeNextFile();
            } catch (Exception e) {
                throw Throwables.propagate(e);
            }

            return lineIterator != null && lineIterator.hasNext();
        }

        @Override
        public InputRow nextRow() {
            try {
                maybeNextFile();
            } catch (Exception e) {
                throw Throwables.propagate(e);
            }

            if (lineIterator == null) {
                throw new NoSuchElementException();
            }

            return parser.parse(lineIterator.next());
        }

        @Override
        public Runnable commit() {
            // Do nothing.
            return new Runnable() {
                public void run() {
                }
            };
        }

        @Override
        public void close() throws IOException {
            objectQueue.clear();
            if (lineIterator != null) {
                lineIterator.close();
            }
        }
    };
}

From source file:pl.wasyl.mpkmaps.HttpClient.java

@Override
protected JSONArray doInBackground(Pair<String, JSONObject>... params) {
    String URL = params[0].first;
    JSONObject jsonObjSend = params[0].second;
    try {//from ww  w  .  j  a v a  2  s  .  c  o m
        DefaultHttpClient httpclient = new DefaultHttpClient();
        HttpPost httpPostRequest = new HttpPost(URL);

        StringEntity se;
        se = new StringEntity(jsonObjSend.toString());

        // Set HTTP parameters
        httpPostRequest.setEntity(se);
        httpPostRequest.setHeader("Accept", "application/json, text/javascript, */*; q=0.01");
        httpPostRequest.setHeader("Accept-Charset", "ISO-8859-2,utf-8;q=0.7,*;q=0.3");
        httpPostRequest.setHeader("Accept-Encoding", "gzip,deflate,sdch");
        httpPostRequest.setHeader("Accept-Language", "pl,en-US;q=0.8,en;q=0.6,en-GB;q=0.4");
        httpPostRequest.setHeader("Content-type", "application/x-www-form-urlencoded");
        httpPostRequest.setHeader("X-Requested-With", "XMLHttpRequest");
        //httpPostRequest.setHeader("Host", "pasazer.mpk.wroc.pl");
        //httpPostRequest.setHeader("Origin", "http://pasazer.mpk.wroc.pl");
        //httpPostRequest.setHeader("Referer", "http://pasazer.mpk.wroc.pl/jak-jezdzimy/mapa-pozycji-pojazdow");
        //httpPostRequest.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.64 Safari/537.31");
        //

        long t = System.currentTimeMillis();
        HttpResponse response = (HttpResponse) httpclient.execute(httpPostRequest);
        Log.i(TAG, "HTTPResponse received in [" + (System.currentTimeMillis() - t) + "ms]");

        // Get hold of the response entity (-> the data):
        HttpEntity entity = response.getEntity();

        if (entity != null) {
            // Read the content stream
            InputStream instream = entity.getContent();
            Header contentEncoding = response.getFirstHeader("Content-Encoding");
            if (contentEncoding != null && contentEncoding.getValue().equalsIgnoreCase("gzip")) {
                instream = new GZIPInputStream(instream);
            }

            // convert content stream to a String
            String resultString = convertStreamToString(instream);
            Log.i(TAG, "<ResultString:>\n" + resultString + "\n</ResultString>");
            instream.close();
            //resultString = resultString.substring(1,resultString.length()-1); // remove wrapping "[" and "]"

            // Transform the String into a JSONObject
            JSONArray jsonObjRecv = new JSONArray(resultString);
            // Raw DEBUG output of our received JSON object:
            Log.i(TAG, "<JSONObject>\n" + jsonObjRecv.toString() + "\n</JSONObject>");
            return jsonObjRecv;
        }

    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

From source file:com.hs.mail.imap.message.response.FetchResponseBuilder.java

private static InputStream getInputStream(FetchData fd) throws IOException {
    File file = Config.getDataFile(fd.getInternalDate(), fd.getPhysMessageID());
    if (FileUtils.isCompressed(file, false)) {
        return new GZIPInputStream(new FileInputStream(file));
    } else if (file.exists()) {
        return new BufferedInputStream(new FileInputStream(file));
    } else {//from ww  w .j  a  v a  2 s.co m
        return new ClassPathResource("/META-INF/notexist.eml").getInputStream();
    }
}

From source file:com.spstudio.common.image.ImageUtils.java

public static byte[] uncompress(byte[] bytes) {
    if (bytes == null || bytes.length == 0) {
        return null;
    }/*from  www.  j ava 2s .c  om*/
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    ByteArrayInputStream in = new ByteArrayInputStream(bytes);
    try {
        GZIPInputStream ungzip = new GZIPInputStream(in);
        byte[] buffer = new byte[256];
        int n;
        while ((n = ungzip.read(buffer)) >= 0) {
            out.write(buffer, 0, n);
        }
    } catch (IOException e) {
        logger.error("gzip uncompress error.", e);
    }

    return out.toByteArray();
}

From source file:org.wikipedia.vlsergey.secretary.jwpf.HttpBot.java

protected void get(final HttpGet getMethod, final ContentProcessable action)
        throws IOException, CookieException, ProcessException {
    getMethod.getParams().setParameter("http.protocol.content-charset", MediaWikiBot.ENCODING);

    getMethod.setHeader("Accept-Encoding", GZIP_CONTENT_ENCODING);

    httpClient.execute(getMethod, new ResponseHandler<Object>() {
        @Override//from  w ww .ja  va2s  .  com
        public Object handleResponse(HttpResponse httpResponse) throws ClientProtocolException, IOException {

            try {
                action.validateReturningCookies(httpClient.getCookieStore().getCookies(), getMethod);

                InputStream inputStream = httpResponse.getEntity().getContent();
                String contentEncoding = httpResponse.getEntity().getContentEncoding() != null
                        ? httpResponse.getEntity().getContentEncoding().getValue()
                        : "";
                if ("gzip".equalsIgnoreCase(contentEncoding))
                    inputStream = new GZIPInputStream(inputStream);

                int statuscode = httpResponse.getStatusLine().getStatusCode();

                if (statuscode == HttpStatus.SC_NOT_FOUND) {
                    log.warn("Not Found: " + getMethod.getRequestLine().getUri());
                    throw new FileNotFoundException(getMethod.getRequestLine().getUri());
                }
                if (statuscode == HttpStatus.SC_INTERNAL_SERVER_ERROR) {
                    throw new ServerErrorException(httpResponse.getStatusLine());
                }
                if (statuscode != HttpStatus.SC_OK) {
                    throw new ClientProtocolException(httpResponse.getStatusLine().toString());
                }

                String encoding = StringUtils
                        .substringAfter(httpResponse.getEntity().getContentType().getValue(), "charset=");
                String out = IoUtils.readToString(inputStream, encoding);
                action.processReturningText(getMethod, out);
            } catch (CookieException exc) {
                throw new ClientProtocolException(exc);
            } catch (ProcessException exc) {
                throw new ClientProtocolException(exc);
            }
            return null;
        }
    });

}

From source file:org.acoustid.server.util.ParameterMap.java

public static ParameterMap parseRequest(HttpServletRequest request) throws IOException {
    String contentEncoding = request.getHeader("Content-Encoding");
    if (contentEncoding != null) {
        contentEncoding = contentEncoding.toLowerCase();
    }/*from   ww w  .  j ava2 s .c  o  m*/
    String contentType = request.getContentType();
    Map<String, String[]> map;
    if ("gzip".equals(contentEncoding) && "application/x-www-form-urlencoded".equals(contentType)) {
        InputStream inputStream = new GZIPInputStream(request.getInputStream());
        InputStreamEntity entity = new InputStreamEntity(inputStream, -1);
        entity.setContentType(contentType);
        map = new HashMap<String, String[]>();
        for (NameValuePair param : URLEncodedUtils.parse(entity)) {
            String name = param.getName();
            String value = param.getValue();
            String[] values = map.get(name);
            if (values == null) {
                values = new String[] { value };
            } else {
                values = (String[]) ArrayUtils.add(values, value);
            }
            map.put(name, values);
        }
    } else {
        map = request.getParameterMap();
    }
    return new ParameterMap(map);
}

From source file:com.scut.easyfe.network.kjFrame.http.FileRequest.java

public byte[] handleResponse(HttpResponse response) throws IOException, KJHttpException {
    HttpEntity entity = response.getEntity();
    long fileSize = entity.getContentLength();
    if (fileSize <= 0) {
        KJLoger.debug("Response doesn't present Content-Length!");
    }//w  w w . j a  v  a  2 s.co  m

    long downloadedSize = mTemporaryFile.length();
    boolean isSupportRange = HttpUtils.isSupportRange(response);
    if (isSupportRange) {
        fileSize += downloadedSize;

        String realRangeValue = HttpUtils.getHeader(response, "Content-Range");
        if (!TextUtils.isEmpty(realRangeValue)) {
            String assumeRangeValue = "bytes " + downloadedSize + "-" + (fileSize - 1);
            if (TextUtils.indexOf(realRangeValue, assumeRangeValue) == -1) {
                throw new IllegalStateException("The Content-Range Header is invalid Assume[" + assumeRangeValue
                        + "] vs Real[" + realRangeValue + "], " + "please remove the temporary file ["
                        + mTemporaryFile + "].");
            }
        }
    }

    if (fileSize > 0 && mStoreFile.length() == fileSize) {
        mStoreFile.renameTo(mTemporaryFile);
        mRequestQueue.getConfig().mDelivery.postDownloadProgress(this, fileSize, fileSize);
        return null;
    }

    RandomAccessFile tmpFileRaf = new RandomAccessFile(mTemporaryFile, "rw");
    if (isSupportRange) {
        tmpFileRaf.seek(downloadedSize);
    } else {
        tmpFileRaf.setLength(0);
        downloadedSize = 0;
    }

    try {
        InputStream in = entity.getContent();
        if (HttpUtils.isGzipContent(response) && !(in instanceof GZIPInputStream)) {
            in = new GZIPInputStream(in);
        }
        byte[] buffer = new byte[6 * 1024]; // 6K buffer
        int offset;

        while ((offset = in.read(buffer)) != -1) {
            tmpFileRaf.write(buffer, 0, offset);

            downloadedSize += offset;
            mRequestQueue.getConfig().mDelivery.postDownloadProgress(this, fileSize, downloadedSize);

            if (isCanceled()) {
                break;
            }
        }
    } finally {
        try {
            if (entity != null)
                entity.consumeContent();
        } catch (Exception e) {
            KJLoger.debug("Error occured when calling consumingContent");
        }
        tmpFileRaf.close();
    }
    return null;
}