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:io.druid.segment.realtime.firehose.WikipediaIrcDecoder.java

private void downloadGeoLiteDbToFile(File geoDb) {
    if (geoDb.exists()) {
        return;// w  w  w .jav  a2s .co m
    }

    try {
        log.info("Downloading geo ip database to [%s]. This may take a few minutes.", geoDb.getAbsolutePath());

        File tmpFile = File.createTempFile("druid", "geo");

        FileUtils.copyInputStreamToFile(new GZIPInputStream(
                new URL("http://geolite.maxmind.com/download/geoip/database/GeoLite2-City.mmdb.gz")
                        .openStream()),
                tmpFile);

        if (!tmpFile.renameTo(geoDb)) {
            throw new RuntimeException("Unable to move geo file to [" + geoDb.getAbsolutePath() + "]!");
        }
    } catch (IOException e) {
        throw new RuntimeException("Unable to download geo ip database.", e);
    }
}

From source file:com.alphabetbloc.accessmrs.tasks.CheckConnectivityTask.java

protected DataInputStream getServerStream() throws Exception {

    HttpPost request = new HttpPost(mServer);
    request.setEntity(new OdkAuthEntity());
    HttpResponse response = httpClient.execute(request);
    response.getStatusLine().getStatusCode();
    HttpEntity responseEntity = response.getEntity();
    responseEntity.getContentLength();/*from  w  w w.ja  v  a2 s . c  om*/

    DataInputStream zdis = new DataInputStream(new GZIPInputStream(responseEntity.getContent()));

    int status = zdis.readInt();
    if (status == HttpURLConnection.HTTP_UNAUTHORIZED) {
        zdis.close();
        throw new IOException("Access denied. Check your username and password.");
    } else if (status <= 0 || status >= HttpURLConnection.HTTP_BAD_REQUEST) {
        zdis.close();
        throw new IOException("Connection Failed. Please Try Again.");
    } else {
        assert (status == HttpURLConnection.HTTP_OK); // success
        return zdis;
    }
}

From source file:ch.cyberduck.core.dav.DAVResource.java

/**
 * Get InputStream for the GET method for the given path.
 *
 * @param path the server relative path of the resource to get
 * @return InputStream/*www  .  java  2  s  .c o  m*/
 * @throws IOException
 */
@Override
public InputStream getMethodData(String path) throws IOException {
    setClient();

    GetMethod method = new GetMethod(URIUtil.encodePathQuery(path));
    method.setFollowRedirects(super.followRedirects);

    generateTransactionHeader(method);
    generateAdditionalHeaders(method);
    final int statusCode = client.executeMethod(method);
    Header contentRange = method.getResponseHeader("Content-Range");
    resume = contentRange != null;

    setStatusCode(statusCode, method.getStatusText());

    if (isHttpSuccess(statusCode)) {
        Header contentEncoding = method.getResponseHeader("Content-Encoding");
        boolean zipped = contentEncoding != null && "gzip".equalsIgnoreCase(contentEncoding.getValue());
        if (zipped) {
            return new GZIPInputStream(method.getResponseBodyAsStream());
        }
        return method.getResponseBodyAsStream();
    } else {
        throw new IOException(method.getStatusText());
    }
}

From source file:ch.ksfx.web.services.spidering.http.WebEngine.java

public void loadResource(Resource resource) {
    HttpMethod httpMethod;//from w w w  .j a v a2 s  .  c  o  m

    try {
        if (/*resource.getHttpMethod().equals(GET)*/true) {
            String url = resource.getUrl();

            if (url != null) {
                url = url.replaceAll("&amp;", "&");
                url = url.replaceAll("&quot;", "\"");
            }

            httpMethod = this.httpClientHelper.executeGetMethod(url);
        } else {
            //TODO implement POST functionality
            /*
            NameValuePair[] nameValuePairs = new NameValuePair[resource.getPostData().size()];
            for(int i = 0; i < resource.getPostData().size(); i++) {
            nameValuePairs[i] = new NameValuePair(resource.getPostData().get(i).getName(),
                                                  resource.getPostData().get(i).getValue());
            }
                    
            String url = resource.getURL().toString();
                    
            if (url != null) {
               url = url.replaceAll("&amp;","&");
               url = url.replaceAll("&quot;","\"");
            }
                    
            httpMethod = this.httpClientHelper.executePostMethod(url,
                                                             nameValuePairs);
             */
        }

    } catch (Exception e) {
        resource.setLoadSucceed(false);
        resource.setHttpStatusCode(222);

        logger.log(Level.SEVERE, "Unable to load resource", e);
        return;
    }

    if (httpMethod == null) {
        resource.setLoadSucceed(false);
        return;
    }

    if (httpMethod.getStatusCode() != HttpStatus.SC_OK) {
        try {
            resource.setUrl(httpMethod.getURI().toString());

            for (Header header : httpMethod.getResponseHeaders()) {
                resource.addResponseHeader(new ResponseHeader(header.getName(), header.getValue()));
            }
            resource.setHttpStatusCode(httpMethod.getStatusCode());
        } catch (Exception e) {
            logger.warning(e.getMessage());
            e.printStackTrace();
        }

        return;
    }

    try {
        if (httpMethod.getResponseHeader("Content-Encoding") != null
                && httpMethod.getResponseHeader("Content-Encoding").getValue().contains("gzip")) {
            BufferedInputStream in = new BufferedInputStream(
                    new GZIPInputStream(httpMethod.getResponseBodyAsStream()));
            ByteArrayOutputStream out = new ByteArrayOutputStream(1024);
            byte[] buffer = new byte[1024];
            int length;
            while ((length = in.read(buffer)) >= 0) {
                out.write(buffer, 0, length);
            }

            resource.setRawContent(out.toByteArray());
            resource.setSize(new Long(out.toByteArray().length));
        } else {
            BufferedInputStream in = new BufferedInputStream(httpMethod.getResponseBodyAsStream());
            ByteArrayOutputStream out = new ByteArrayOutputStream(1024);
            byte[] buffer = new byte[1024];
            int length;
            while ((length = in.read(buffer)) >= 0) {
                out.write(buffer, 0, length);
            }

            resource.setRawContent(out.toByteArray());
            resource.setSize(new Long(out.toByteArray().length));
        }

        resource.setHttpStatusCode(httpMethod.getStatusCode());
        resource.setLoadSucceed(true);

        resource.setMimeType(recognizeMimeType(resource, httpMethod));
        if (resource.getMimeType().startsWith("text") || resource.getMimeType().equals("application/json")) {

            resource.setContent(EncodingHelper.encode(resource.getRawContent(), resource.getEncoding(),
                    ((HttpMethodBase) httpMethod).getResponseCharSet()));
        } else {
            resource.setIsBinary(true);
        }

    } catch (IOException e) {
        e.printStackTrace();
        logger.log(Level.SEVERE, "Unable to load resource", e);
    } finally {
        httpMethod.releaseConnection();
    }
}

From source file:org.grameenfoundation.consulteca.utils.HttpHelpers.java

private static InputStream getInputStream(HttpURLConnection httpUrlConnection) throws IOException {
    InputStream inputStream = httpUrlConnection.getInputStream();
    String contentEncoding = httpUrlConnection.getHeaderField("Content-Encoding");
    if (contentEncoding != null && contentEncoding.equalsIgnoreCase("gzip")) {
        inputStream = new GZIPInputStream(inputStream);
    }/*  w w  w .  j a va  2 s.co  m*/
    return inputStream;
}

From source file:com.spotify.helios.client.DefaultRequestDispatcher.java

@Override
public ListenableFuture<Response> request(final URI uri, final String method, final byte[] entityBytes,
        final Map<String, List<String>> headers) {
    return executorService.submit(new Callable<Response>() {
        @Override//from  ww w.j  av  a2 s. c om
        public Response call() throws Exception {
            final HttpURLConnection connection = connect(uri, method, entityBytes, headers);
            final int status = connection.getResponseCode();
            final InputStream rawStream;

            if (status / 100 != 2) {
                rawStream = connection.getErrorStream();
            } else {
                rawStream = connection.getInputStream();
            }

            final boolean gzip = isGzipCompressed(connection);
            final InputStream stream = gzip ? new GZIPInputStream(rawStream) : rawStream;
            final ByteArrayOutputStream payload = new ByteArrayOutputStream();
            if (stream != null) {
                int n;
                byte[] buffer = new byte[4096];
                while ((n = stream.read(buffer, 0, buffer.length)) != -1) {
                    payload.write(buffer, 0, n);
                }
            }

            URI realUri = connection.getURL().toURI();
            if (log.isTraceEnabled()) {
                log.trace("rep: {} {} {} {} {} gzip:{}", method, realUri, status, payload.size(),
                        decode(payload), gzip);
            } else {
                log.debug("rep: {} {} {} {} gzip:{}", method, realUri, status, payload.size(), gzip);
            }

            return new Response(method, uri, status, payload.toByteArray(),
                    Collections.unmodifiableMap(Maps.newHashMap(connection.getHeaderFields())));
        }

        private boolean isGzipCompressed(final HttpURLConnection connection) {
            final List<String> encodings = connection.getHeaderFields().get("Content-Encoding");
            if (encodings == null) {
                return false;
            }
            for (String encoding : encodings) {
                if ("gzip".equals(encoding)) {
                    return true;
                }
            }
            return false;
        }
    });
}

From source file:edu.stanford.muse.index.NEROld.java

public static void readLocationNamesToSuppress() {
    String suppress_file = "suppress.locations.txt.gz";
    try {/*  w w w.ja va 2s.  c  o  m*/
        InputStream is = new GZIPInputStream(NER.class.getClassLoader().getResourceAsStream(suppress_file));
        LineNumberReader lnr = new LineNumberReader(new InputStreamReader(is, "UTF-8"));
        while (true) {
            String line = lnr.readLine();
            if (line == null)
                break;
            StringTokenizer st = new StringTokenizer(line);
            if (st.hasMoreTokens()) {
                String s = st.nextToken();
                if (!s.startsWith("#"))
                    locationsToSuppress.add(s.toLowerCase());
            }
        }
    } catch (Exception e) {
        log.warn("Error: unable to read " + suppress_file);
        Util.print_exception(e);
    }
    log.info(locationsToSuppress.size() + " names to suppress as locations");
}

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

@Override
protected void initStream(InputSplit inSplit) throws IOException, InterruptedException {
    setFile(((FileSplit) inSplit).getPath());
    FSDataInputStream fileIn = fs.open(file);
    URI zipURI = file.toUri();//w  w  w .  j a va 2  s .  co  m
    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;
        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();
            // if we don't know the size, assume it's big!
            initParser(zipURI.toASCIIString() + "/" + subId, INMEMORYTHRESHOLD);
        } else {
            baos = new ByteArrayOutputStream((int) size);
            initParser(zipURI.toASCIIString() + "/" + subId, size);
        }
        int nb;
        while ((nb = zipIn.read(buf, 0, buf.length)) != -1) {
            baos.write(buf, 0, nb);
        }
        parse(subId, new ByteArrayInputStream(baos.toByteArray()));
    } else if (codecString.equalsIgnoreCase(CompressionCodec.GZIP.toString())) {
        long size = inSplit.getLength();
        zipIn = new GZIPInputStream(fileIn);
        codec = CompressionCodec.GZIP;
        initParser(zipURI.toASCIIString(), size * COMPRESSIONFACTOR);
        parse(file.getName(), zipIn);
    } else {
        throw new UnsupportedOperationException("Unsupported codec: " + codec.name());
    }
}

From source file:gov.wa.wsdot.android.wsdot.service.MountainPassesSyncService.java

@Override
protected void onHandleIntent(Intent intent) {
    ContentResolver resolver = getContentResolver();
    Cursor cursor = null;/*from w w w .j a  v  a2 s  .  c o m*/
    long now = System.currentTimeMillis();
    boolean shouldUpdate = true;
    String responseString = "";

    /** 
     * Check the cache table for the last time data was downloaded. If we are within
     * the allowed time period, don't sync, otherwise get fresh data from the server.
     */
    try {
        cursor = resolver.query(Caches.CONTENT_URI, new String[] { Caches.CACHE_LAST_UPDATED },
                Caches.CACHE_TABLE_NAME + " LIKE ?", new String[] { "mountain_passes" }, null);

        if (cursor != null && cursor.moveToFirst()) {
            long lastUpdated = cursor.getLong(0);
            //long deltaMinutes = (now - lastUpdated) / DateUtils.MINUTE_IN_MILLIS;
            //Log.d(DEBUG_TAG, "Delta since last update is " + deltaMinutes + " min");
            shouldUpdate = (Math.abs(now - lastUpdated) > (15 * DateUtils.MINUTE_IN_MILLIS));
        }
    } finally {
        if (cursor != null) {
            cursor.close();
        }
    }

    // Ability to force a refresh of camera data.
    boolean forceUpdate = intent.getBooleanExtra("forceUpdate", false);

    if (shouldUpdate || forceUpdate) {
        List<Integer> starred = new ArrayList<Integer>();

        starred = getStarred();
        buildWeatherPhrases();

        try {
            URL url = new URL(MOUNTAIN_PASS_URL);
            URLConnection urlConn = url.openConnection();

            BufferedInputStream bis = new BufferedInputStream(urlConn.getInputStream());
            GZIPInputStream gzin = new GZIPInputStream(bis);
            InputStreamReader is = new InputStreamReader(gzin);
            BufferedReader in = new BufferedReader(is);

            String mDateUpdated = "";
            String jsonFile = "";
            String line;
            while ((line = in.readLine()) != null)
                jsonFile += line;
            in.close();

            JSONObject obj = new JSONObject(jsonFile);
            JSONObject result = obj.getJSONObject("GetMountainPassConditionsResult");
            JSONArray passConditions = result.getJSONArray("PassCondition");
            String weatherCondition;
            Integer weather_image;
            Integer forecast_weather_image;
            List<ContentValues> passes = new ArrayList<ContentValues>();

            int numConditions = passConditions.length();
            for (int j = 0; j < numConditions; j++) {
                JSONObject pass = passConditions.getJSONObject(j);
                ContentValues passData = new ContentValues();
                weatherCondition = pass.getString("WeatherCondition");
                weather_image = getWeatherImage(weatherPhrases, weatherCondition);

                String tempDate = pass.getString("DateUpdated");

                try {
                    tempDate = tempDate.replace("[", "");
                    tempDate = tempDate.replace("]", "");

                    String[] a = tempDate.split(",");
                    StringBuilder sb = new StringBuilder();
                    for (int m = 0; m < 5; m++) {
                        sb.append(a[m]);
                        sb.append(",");
                    }
                    tempDate = sb.toString().trim();
                    tempDate = tempDate.substring(0, tempDate.length() - 1);
                    Date date = parseDateFormat.parse(tempDate);
                    mDateUpdated = displayDateFormat.format(date);
                } catch (Exception e) {
                    Log.e(DEBUG_TAG, "Error parsing date: " + tempDate, e);
                    mDateUpdated = "N/A";
                }

                JSONArray forecasts = pass.getJSONArray("Forecast");
                JSONArray forecastItems = new JSONArray();

                int numForecasts = forecasts.length();
                for (int l = 0; l < numForecasts; l++) {
                    JSONObject forecast = forecasts.getJSONObject(l);

                    if (isNight(forecast.getString("Day"))) {
                        forecast_weather_image = getWeatherImage(weatherPhrasesNight,
                                forecast.getString("ForecastText"));
                    } else {
                        forecast_weather_image = getWeatherImage(weatherPhrases,
                                forecast.getString("ForecastText"));
                    }

                    forecast.put("weather_icon", forecast_weather_image);

                    if (l == 0) {
                        if (weatherCondition.equals("")) {
                            weatherCondition = forecast.getString("ForecastText").split("\\.")[0] + ".";
                            weather_image = forecast_weather_image;
                        }
                    }

                    forecastItems.put(forecast);
                }

                passData.put(MountainPasses.MOUNTAIN_PASS_ID, pass.getString("MountainPassId"));
                passData.put(MountainPasses.MOUNTAIN_PASS_NAME, pass.getString("MountainPassName"));
                passData.put(MountainPasses.MOUNTAIN_PASS_WEATHER_ICON, weather_image);
                passData.put(MountainPasses.MOUNTAIN_PASS_FORECAST, forecastItems.toString());
                passData.put(MountainPasses.MOUNTAIN_PASS_WEATHER_CONDITION, weatherCondition);
                passData.put(MountainPasses.MOUNTAIN_PASS_DATE_UPDATED, mDateUpdated);
                passData.put(MountainPasses.MOUNTAIN_PASS_CAMERA, pass.getString("Cameras"));
                passData.put(MountainPasses.MOUNTAIN_PASS_ELEVATION, pass.getString("ElevationInFeet"));
                passData.put(MountainPasses.MOUNTAIN_PASS_TRAVEL_ADVISORY_ACTIVE,
                        pass.getString("TravelAdvisoryActive"));
                passData.put(MountainPasses.MOUNTAIN_PASS_ROAD_CONDITION, pass.getString("RoadCondition"));
                passData.put(MountainPasses.MOUNTAIN_PASS_TEMPERATURE,
                        pass.getString("TemperatureInFahrenheit"));
                JSONObject restrictionOne = pass.getJSONObject("RestrictionOne");
                passData.put(MountainPasses.MOUNTAIN_PASS_RESTRICTION_ONE,
                        restrictionOne.getString("RestrictionText"));
                passData.put(MountainPasses.MOUNTAIN_PASS_RESTRICTION_ONE_DIRECTION,
                        restrictionOne.getString("TravelDirection"));
                JSONObject restrictionTwo = pass.getJSONObject("RestrictionTwo");
                passData.put(MountainPasses.MOUNTAIN_PASS_RESTRICTION_TWO,
                        restrictionTwo.getString("RestrictionText"));
                passData.put(MountainPasses.MOUNTAIN_PASS_RESTRICTION_TWO_DIRECTION,
                        restrictionTwo.getString("TravelDirection"));

                if (starred.contains(Integer.parseInt(pass.getString("MountainPassId")))) {
                    passData.put(MountainPasses.MOUNTAIN_PASS_IS_STARRED, 1);
                }

                passes.add(passData);

            }

            // Purge existing mountain passes covered by incoming data
            resolver.delete(MountainPasses.CONTENT_URI, null, null);
            // Bulk insert all the new mountain passes
            resolver.bulkInsert(MountainPasses.CONTENT_URI, passes.toArray(new ContentValues[passes.size()]));
            // Update the cache table with the time we did the update
            ContentValues values = new ContentValues();
            values.put(Caches.CACHE_LAST_UPDATED, System.currentTimeMillis());
            resolver.update(Caches.CONTENT_URI, values, Caches.CACHE_TABLE_NAME + "=?",
                    new String[] { "mountain_passes" });

            responseString = "OK";
        } catch (Exception e) {
            Log.e(DEBUG_TAG, "Error: " + e.getMessage());
            responseString = e.getMessage();
        }

    } else {
        responseString = "NOP";
    }

    Intent broadcastIntent = new Intent();
    broadcastIntent.setAction("gov.wa.wsdot.android.wsdot.intent.action.MOUNTAIN_PASSES_RESPONSE");
    broadcastIntent.addCategory(Intent.CATEGORY_DEFAULT);
    broadcastIntent.putExtra("responseString", responseString);
    sendBroadcast(broadcastIntent);
}

From source file:de.wpsverlinden.dupfind.FileIndexer.java

@SuppressWarnings("unchecked")
public void loadIndex() {
    File file = new File(userDir + "/DupFind.index.gz");
    outputPrinter.print("Loading index ... ");
    if (file.exists() && file.isFile()) {
        try (XMLDecoder xdec = new XMLDecoder(new GZIPInputStream(new FileInputStream(file)))) {
            fileIndex.clear();/*from  w  ww .  j  a v  a  2  s  .co m*/
            fileIndex.putAll((Map<String, FileEntry>) xdec.readObject());
            outputPrinter.println("done. " + fileIndex.size() + " files in index.");
        } catch (Exception e) {
            System.err.println(e);
        }
    } else {
        outputPrinter.println("failed. Creating new index.");
        fileIndex.clear();
    }
}