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.CompressedDocumentReader.java

protected void initStream(InputSplit inSplit) throws IOException {
    if (LOG.isDebugEnabled()) {
        LOG.debug("Starting " + file);
    }/* w  ww  .j  a  v  a2 s  .  co  m*/
    setFile(((FileSplit) inSplit).getPath());

    FSDataInputStream fileIn = fs.open(file);

    String codecString = conf.get(ConfigConstants.CONF_INPUT_COMPRESSION_CODEC, CompressionCodec.ZIP.toString())
            .toUpperCase();
    codec = CompressionCodec.valueOf(codecString);
    switch (codec) {
    case ZIP:
        zipIn = new ZipInputStream(fileIn);
        break;
    case GZIP:
        zipIn = new GZIPInputStream(fileIn);
        String uri = makeURIFromPath(file);
        if (uri.toLowerCase().endsWith(".gz") || uri.toLowerCase().endsWith(".gzip")) {
            uri = uri.substring(0, uri.lastIndexOf('.'));
        }
        setKey(uri, 0, 0, true);
        break;
    default:
        String error = "Unsupported codec: " + codec.name();
        LOG.error(error, new UnsupportedOperationException(error));
    }
}

From source file:uk.co.jassoft.network.Network.java

public InputStream read(String httpUrl, String method, boolean cache) throws IOException {

    HttpURLConnection conn = null;
    try {/*from w  w  w. j  a  v a2s. co  m*/

        URL base, next;
        String location;

        while (true) {
            // inputing the keywords to google search engine
            URL url = new URL(httpUrl);

            //                if(cache) {
            //                    //Proxy instance, proxy ip = 10.0.0.1 with port 8080
            //                    Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("cache", 3128));
            //                    // makking connection to the internet
            //                    conn = (HttpURLConnection) url.openConnection(proxy);
            //                }
            //                else {
            conn = (HttpURLConnection) url.openConnection();
            //                }

            conn.setConnectTimeout(connectTimeout);
            conn.setReadTimeout(readTimeout);
            conn.setRequestMethod(method);
            conn.setRequestProperty("User-Agent",
                    "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.13) Gecko/20101203 Firefox/3.6.13 ( .NET CLR 3.5.30729)");
            conn.setRequestProperty("Accept-Encoding", "gzip");

            switch (conn.getResponseCode()) {
            case HttpURLConnection.HTTP_MOVED_PERM:
            case HttpURLConnection.HTTP_MOVED_TEMP:
                location = conn.getHeaderField("Location");
                base = new URL(httpUrl);
                next = new URL(base, location); // Deal with relative URLs
                httpUrl = next.toExternalForm();
                continue;
            }

            break;
        }

        if (!conn.getContentType().startsWith("text/") || conn.getContentType().equals("text/xml")) {
            throw new IOException(String.format("Content of story is not Text. Returned content type is [%s]",
                    conn.getContentType()));
        }

        if ("gzip".equals(conn.getContentEncoding())) {
            return new GZIPInputStream(conn.getInputStream());
        }

        // getting the input stream of page html into bufferedreader
        return conn.getInputStream();
    } catch (Exception exception) {
        throw new IOException(exception);
    } finally {
        if (conn != null && conn.getErrorStream() != null) {
            conn.getErrorStream().close();
        }
    }
}

From source file:adams.core.io.GzipUtils.java

/**
 * Decompresses the specified gzipped bytes.
 *
 * @param input   the gzip compressed bytes
 * @param buffer   the buffer size to use
 * @return      the decompressed bytes, null in case of error
 *//*w w w.java2  s . co  m*/
@MixedCopyright(copyright = "Apache compress commons", license = License.APACHE2, url = "http://commons.apache.org/compress/apidocs/org/apache/commons/compress/compressors/CompressorStreamFactory.html")
public static byte[] decompress(byte[] input, int buffer) {
    GZIPInputStream bis;
    ByteArrayOutputStream bos;

    try {
        bis = new GZIPInputStream(new ByteArrayInputStream(input));
        bos = new ByteArrayOutputStream();
        IOUtils.copy(bis, bos, buffer);
        return bos.toByteArray();
    } catch (Exception e) {
        System.err.println("Failed to decompress bytes!");
        e.printStackTrace();
        return null;
    }
}

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

public SlaveServerJobStatus(Node jobStatusNode) {
    this();/*from  w ww .j av a2  s .  c om*/
    jobName = XMLHandler.getTagValue(jobStatusNode, "jobname");
    statusDescription = XMLHandler.getTagValue(jobStatusNode, "status_desc");
    errorDescription = XMLHandler.getTagValue(jobStatusNode, "error_desc");

    String loggingString64 = XMLHandler.getTagValue(jobStatusNode, "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) + Const.CR;
    }

    // get the result object, if there is any...
    //
    Node resultNode = XMLHandler.getSubNode(jobStatusNode, 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.streamsets.datacollector.cluster.TestTarFileCreator.java

@Test
public void testCreateEtcTarGz() throws Exception {
    File etcDir = new File(tempDir, "etc");
    Assert.assertTrue(etcDir.mkdir());//from  www  . ja  va2 s . c  o  m
    createJar(etcDir);
    createJar(etcDir);
    File tarFile = new File(tempDir, "etc.tar.gz");
    TarFileCreator.createTarGz(etcDir, tarFile);
    TarInputStream tis = new TarInputStream(new GZIPInputStream(new FileInputStream(tarFile)));
    readJar(tis);
    readJar(tis);
}

From source file:net.ychron.unirestinst.http.HttpResponse.java

@SuppressWarnings("unchecked")
public HttpResponse(Options options, org.apache.http.HttpResponse response, Class<T> responseClass) {
    HttpEntity responseEntity = response.getEntity();
    ObjectMapper objectMapper = (ObjectMapper) options.getOption(Option.OBJECT_MAPPER);

    Header[] allHeaders = response.getAllHeaders();
    for (Header header : allHeaders) {
        String headerName = header.getName();
        List<String> list = headers.get(headerName);
        if (list == null)
            list = new ArrayList<String>();
        list.add(header.getValue());//from w  w  w.  j  a  v  a 2  s.com
        headers.put(headerName, list);
    }
    StatusLine statusLine = response.getStatusLine();
    this.statusCode = statusLine.getStatusCode();
    this.statusText = statusLine.getReasonPhrase();

    if (responseEntity != null) {
        String charset = "UTF-8";

        Header contentType = responseEntity.getContentType();
        if (contentType != null) {
            String responseCharset = ResponseUtils.getCharsetFromContentType(contentType.getValue());
            if (responseCharset != null && !responseCharset.trim().equals("")) {
                charset = responseCharset;
            }
        }

        try {
            byte[] rawBody;
            try {
                InputStream responseInputStream = responseEntity.getContent();
                if (ResponseUtils.isGzipped(responseEntity.getContentEncoding())) {
                    responseInputStream = new GZIPInputStream(responseEntity.getContent());
                }
                rawBody = ResponseUtils.getBytes(responseInputStream);
            } catch (IOException e2) {
                throw new RuntimeException(e2);
            }
            this.rawBody = new ByteArrayInputStream(rawBody);

            if (JsonNode.class.equals(responseClass)) {
                String jsonString = new String(rawBody, charset).trim();
                this.body = (T) new JsonNode(jsonString);
            } else if (String.class.equals(responseClass)) {
                this.body = (T) new String(rawBody, charset);
            } else if (InputStream.class.equals(responseClass)) {
                this.body = (T) this.rawBody;
            } else if (objectMapper != null) {
                this.body = objectMapper.readValue(new String(rawBody, charset), responseClass);
            } else {
                throw new Exception(
                        "Only String, JsonNode and InputStream are supported, or an ObjectMapper implementation is required.");
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    EntityUtils.consumeQuietly(responseEntity);
}

From source file:org.dd4t.databind.DD4TModelConverter.java

public static String decompress(byte[] bytes) throws IOException {
    GZIPInputStream gis = null;//w  w w. java  2 s.c  o m
    String result = null;

    try {
        gis = new GZIPInputStream(new ByteArrayInputStream(bytes));
        result = IOUtils.toString(gis);
    } finally {
        IOUtils.closeQuietly(gis);
    }
    return result;
}

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

@Override
protected void onHandleIntent(Intent intent) {
    ContentResolver resolver = getContentResolver();
    Cursor cursor = null;/*from  w  w  w . jav a  2s  .co 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, projection, Caches.CACHE_TABLE_NAME + " LIKE ?",
                new String[] { "cameras" }, null);

        if (cursor != null && cursor.moveToFirst()) {
            long lastUpdated = cursor.getLong(0);
            //long deltaDays = (now - lastUpdated) / DateUtils.DAY_IN_MILLIS;
            //Log.d(DEBUG_TAG, "Delta since last update is " + deltaDays + " day(s)");
            shouldUpdate = (Math.abs(now - lastUpdated) > (7 * DateUtils.DAY_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();

        try {
            URL url = new URL(CAMERAS_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 jsonFile = "";
            String line;
            while ((line = in.readLine()) != null)
                jsonFile += line;
            in.close();

            JSONObject obj = new JSONObject(jsonFile);
            JSONObject result = obj.getJSONObject("cameras");
            JSONArray items = result.getJSONArray("items");
            List<ContentValues> cams = new ArrayList<ContentValues>();

            int numItems = items.length();
            for (int j = 0; j < numItems; j++) {
                JSONObject item = items.getJSONObject(j);
                ContentValues cameraData = new ContentValues();

                cameraData.put(Cameras.CAMERA_ID, item.getString("id"));
                cameraData.put(Cameras.CAMERA_TITLE, item.getString("title"));
                cameraData.put(Cameras.CAMERA_URL, item.getString("url"));
                cameraData.put(Cameras.CAMERA_LATITUDE, item.getString("lat"));
                cameraData.put(Cameras.CAMERA_LONGITUDE, item.getString("lon"));
                cameraData.put(Cameras.CAMERA_HAS_VIDEO, item.getString("video"));
                cameraData.put(Cameras.CAMERA_ROAD_NAME, item.getString("roadName"));

                if (starred.contains(Integer.parseInt(item.getString("id")))) {
                    cameraData.put(Cameras.CAMERA_IS_STARRED, 1);
                }

                cams.add(cameraData);
            }

            // Purge existing cameras covered by incoming data
            resolver.delete(Cameras.CONTENT_URI, null, null);
            // Bulk insert all the new cameras
            resolver.bulkInsert(Cameras.CONTENT_URI, cams.toArray(new ContentValues[cams.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 + " LIKE ?",
                    new String[] { "cameras" });

            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.CAMERAS_RESPONSE");
    broadcastIntent.addCategory(Intent.CATEGORY_DEFAULT);
    broadcastIntent.putExtra("responseString", responseString);
    sendBroadcast(broadcastIntent);
}

From source file:com.blockwithme.longdb.tools.Utils.java

/** Get checksum CRC32 for the file content.
 * // w  ww .ja  va 2 s  . com
 * @param theFile
 *        the file
 * @param isCommpressed
 *        true if commpressed
 * @return the checksum (CRC32) of the file.
 * @throws Exception */
@SuppressWarnings("resource")
public static long getCRC32(final File theFile, final boolean isCommpressed) throws Exception {
    CheckedInputStream cis = null;
    try {
        final CRC32 checksum = new CRC32();
        // TODO: Would a buffered input stream make this faster?
        cis = new CheckedInputStream((isCommpressed ? new GZIPInputStream(new FileInputStream(theFile))
                : new FileInputStream(theFile)), checksum);
        final byte[] tempBuf = new byte[ONE_K];
        while (cis.read(tempBuf) >= 0)
            ; // just read the full stream. // $codepro.audit.disable
        return checksum.getValue();
    } finally {
        if (cis != null)
            cis.close();
    }
}

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

@Override
protected void onHandleIntent(Intent intent) {
    ContentResolver resolver = getContentResolver();
    Cursor cursor = null;/*from   w  ww  . ja  v  a  2  s  .  co 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[] { "travel_times" }, 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) > (5 * 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();

        try {
            URL url = new URL(TRAVEL_TIMES_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 jsonFile = "";
            String line;

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

            JSONObject obj = new JSONObject(jsonFile);
            JSONObject result = obj.getJSONObject("traveltimes");
            JSONArray items = result.getJSONArray("items");
            List<ContentValues> times = new ArrayList<ContentValues>();

            int numItems = items.length();
            for (int j = 0; j < numItems; j++) {
                JSONObject item = items.getJSONObject(j);
                ContentValues timesValues = new ContentValues();
                timesValues.put(TravelTimes.TRAVEL_TIMES_TITLE, item.getString("title"));
                timesValues.put(TravelTimes.TRAVEL_TIMES_CURRENT, item.getInt("current"));
                timesValues.put(TravelTimes.TRAVEL_TIMES_AVERAGE, item.getInt("average"));
                timesValues.put(TravelTimes.TRAVEL_TIMES_DISTANCE, item.getString("distance") + " miles");
                timesValues.put(TravelTimes.TRAVEL_TIMES_ID, Integer.parseInt(item.getString("routeid")));
                timesValues.put(TravelTimes.TRAVEL_TIMES_UPDATED, item.getString("updated"));

                if (starred.contains(Integer.parseInt(item.getString("routeid")))) {
                    timesValues.put(TravelTimes.TRAVEL_TIMES_IS_STARRED, 1);
                }

                times.add(timesValues);
            }

            // Purge existing travel times covered by incoming data
            resolver.delete(TravelTimes.CONTENT_URI, null, null);
            // Bulk insert all the new travel times
            resolver.bulkInsert(TravelTimes.CONTENT_URI, times.toArray(new ContentValues[times.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[] { "travel_times" });

            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.TRAVEL_TIMES_RESPONSE");
    broadcastIntent.addCategory(Intent.CATEGORY_DEFAULT);
    broadcastIntent.putExtra("responseString", responseString);
    sendBroadcast(broadcastIntent);
}