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.fanfou.app.opensource.http.support.GzipDecompressingEntity.java

@Override
InputStream getDecompressingInputStream(final InputStream wrapped) throws IOException {
    return new GZIPInputStream(wrapped);
}

From source file:com.amazonaws.services.kinesis.model.transform.PutRecordsRequestMarshallerTest.java

@Test
public void test() throws Exception {
    PutRecordsRequest putRecordsRequest = new PutRecordsRequest();
    putRecordsRequest.setStreamName("stream name");
    List<PutRecordsRequestEntry> records = new ArrayList<PutRecordsRequestEntry>();
    for (int i = 0; i < 10; i++) {
        PutRecordsRequestEntry record = new PutRecordsRequestEntry();
        String randomStr = RandomStringUtils.random(8 * 1024);
        record.setData(ByteBuffer.wrap(randomStr.getBytes(StringUtils.UTF8)));
        record.setPartitionKey("partition key");
        records.add(record);// ww w  .  j a  v a  2  s  .c o m
    }
    putRecordsRequest.setRecords(records);
    PutRecordsRequestMarshaller marshaller = new PutRecordsRequestMarshaller();
    Request<PutRecordsRequest> request = marshaller.marshall(putRecordsRequest);

    assertEquals("content encoding", "gzip", request.getHeaders().get("Content-Encoding"));
    byte[] content = IOUtils.toByteArray(request.getContent());
    assertEquals("content length", request.getHeaders().get("Content-Length"), String.valueOf(content.length));
    GZIPInputStream gis = new GZIPInputStream(new ByteArrayInputStream(content));
    String str = IOUtils.toString(gis);
    assertTrue("content is compressed", content.length < str.length());
    Map<String, String> map = JsonUtils.jsonToMap(str);
    assertEquals("StreamName", "stream name", map.get("StreamName"));
}

From source file:org.onebusaway.gtfs_realtime.nextbus.services.DownloaderService.java

public synchronized InputStream openUrl(String uri) throws IOException {

    stallIfNeeded();//from w  w  w  .  j a  va2s . c  o  m

    HttpUriRequest request = new HttpGet(uri);
    request.addHeader("Accept-Encoding", "gzip");
    HttpResponse response = _client.execute(request);
    HttpEntity entity = response.getEntity();

    noteDownload(entity);

    InputStream in = entity.getContent();
    Header contentEncoding = response.getFirstHeader("Content-Encoding");
    if (contentEncoding != null && contentEncoding.getValue().equalsIgnoreCase("gzip")) {
        in = new GZIPInputStream(in);
    }
    return in;
}

From source file:com.github.util.HttpHandler.java

private String executeRequest(HttpUriRequest request) {
    DefaultHttpClient client = new DefaultHttpClient();
    try {/* w  w w. j a v a 2 s . com*/
        HttpResponse execute = client.execute(request);
        Header firstHeader = execute.getFirstHeader("Content-Encoding");
        HttpEntity response = execute.getEntity();
        InputStream inputStream = response.getContent();
        if (firstHeader != null && firstHeader.getValue() != null
                && firstHeader.getValue().equalsIgnoreCase("gzip")) {
            inputStream = new GZIPInputStream(inputStream);
        }
        return streamToString(inputStream);
    } catch (IOException e) {
        return null;
    }
}

From source file:edu.dfci.cccb.mev.dataset.domain.gzip.GzipTsvInput.java

@Override
public InputStream input() throws IOException {
    if (log.isDebugEnabled())
        log.debug(url);/*from ww w.  j a va2  s .  c  om*/

    GZIPInputStream zis = null;
    BufferedOutputStream dest = null;
    BufferedInputStream bis = null;
    File unzipped = new TemporaryFile();
    try {
        URLConnection urlc = url.openConnection();
        zis = new GZIPInputStream(urlc.getInputStream());

        int count;
        byte data[] = new byte[BUFFER];
        // write the files to the disk

        FileOutputStream fos = new FileOutputStream(unzipped);
        if (log.isDebugEnabled())
            log.debug("Unzipping SOFT file to " + unzipped.getAbsolutePath());

        dest = new BufferedOutputStream(fos, BUFFER);
        while ((count = zis.read(data, 0, BUFFER)) != -1) {
            dest.write(data, 0, count);
        }
        dest.flush();
        dest.close();
        return new FileInputStream(unzipped);

    } finally {
        if (bis != null) {
            try {
                bis.close();
            } catch (IOException ioe) {
            }
        }
        if (dest != null) {
            try {
                dest.close();
            } catch (IOException ioe) {
            }
        }
        if (zis != null) {
            try {
                zis.close();
            } catch (IOException ioe) {
            }
        }
    }

}

From source file:com.unilever.audit.services2.Sync_Up.java

/**
 * Retrieves representation of an instance of
 * com.unilever.audit.services2.SyncUpResource
 *
 * @return an instance of java.lang.String
 *///from w w w .j  a v  a2s . com
@POST
@Produces("application/json")
@Consumes("text/plain")
public String postSyncUP(byte[] jsonByte) throws IOException {
    //TODO return proper representation object
    JSONObject syncUPJsonObj;
    byte[] buffer;
    try {
        /**
         * ***************** Decompress JsonObject ***********************
         */
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        GZIPInputStream in;
        in = new GZIPInputStream(new ByteArrayInputStream(jsonByte));
        IOUtils.copy(in, out);
        buffer = out.toByteArray();
        syncUPJsonObj = new JSONObject(new String(buffer));

        /**
         * *********** Call Methods To Save Sync UP objects **************
         */
        persistVisit(syncUPJsonObj);

    } catch (IOException ex) {
        ex.printStackTrace();
    } catch (JSONException ex) {
        ex.printStackTrace();
    }

    return null;
}

From source file:net.landora.animeinfo.mylistreader.ListReader.java

public boolean download(InputStream input) throws Throwable {
    TarInputStream is = null;/*  w  w  w . java 2 s  . c om*/
    try {
        is = new TarInputStream(new GZIPInputStream(input));

        TarEntry entry;
        while ((entry = is.getNextEntry()) != null) {
            if (!entry.getName().equalsIgnoreCase("mylist.xml")) {
                continue;
            }

            XMLStreamReader reader = XMLInputFactory.newFactory().createXMLStreamReader(is);
            reader.nextTag();
            reader.require(XMLStreamReader.START_ELEMENT, null, "my_anime_list");
            values = new HashMap<String, String>();
            StringBuilder value = new StringBuilder();

            while (reader.nextTag() != XMLStreamReader.END_ELEMENT) {
                reader.require(XMLStreamReader.START_ELEMENT, null, null);
                String tableName = reader.getLocalName();

                values.clear();

                while (reader.nextTag() != XMLStreamReader.END_ELEMENT) {
                    String valueName = reader.getLocalName();

                    value.setLength(0);
                    while (reader.next() != XMLStreamReader.END_ELEMENT) {
                        switch (reader.getEventType()) {
                        case XMLStreamReader.CDATA:
                        case XMLStreamReader.CHARACTERS:
                        case XMLStreamReader.SPACE:
                            value.append(reader.getText());
                        }
                    }
                    reader.require(XMLStreamReader.END_ELEMENT, null, valueName);
                    values.put(valueName, value.toString());

                }
                reader.require(XMLStreamReader.END_ELEMENT, null, tableName);

                handleTable(tableName);

            }
            reader.require(XMLStreamReader.END_ELEMENT, null, "my_anime_list");

            saveLast();
        }
        return true;
    } finally {
        if (is != null) {
            IOUtils.closeQuietly(is);
        } else if (input != null) {
            IOUtils.closeQuietly(input);
        }
    }
}

From source file:edu.cornell.med.icb.geo.tools.GEOPlatform.java

/**
 * Read the platform information from a GEO platform file.
 *
 * @param filename/* w  ww  .ja  v a2  s  .  c  o m*/
 * @throws SyntaxErrorException
 * @throws IOException
 */
public void read(final String filename) throws SyntaxErrorException, IOException {
    if (filename == null) {
        return;
    }
    final GeoPlatformFileReader reader = new GeoPlatformFileReader();
    final Reader lowLevelReader;
    if (filename.endsWith(".gz")) {
        lowLevelReader = new InputStreamReader(new GZIPInputStream(new FileInputStream(filename)));
    } else {
        lowLevelReader = new FileReader(filename);
    }

    final Table platformFileContent = reader.read(lowLevelReader);
    final Table.RowIterator ri = platformFileContent.firstRow();

    int probeSetIdColumnIndex = -1;
    int genbankAcColumnIndex = -1;
    int genbankListColumnIndex = -1;
    int genbankColumnIndex = -1;
    final String probeIdColumnName = "ID";
    final String genbankAcColumnName = "GB_ACC";
    final String genbankListColumnName = "GB_LIST";

    try {
        probeSetIdColumnIndex = platformFileContent.getColumnIndex(probeIdColumnName);
    } catch (InvalidColumnException e) {
        assert false;
    }
    try {
        genbankAcColumnIndex = platformFileContent.getColumnIndex(genbankAcColumnName);
    } catch (InvalidColumnException e) { // NOPMD
        // OK, see below.
    }
    try {
        genbankListColumnIndex = platformFileContent.getColumnIndex(genbankListColumnName);
    } catch (InvalidColumnException e) { // NOPMD
        // OK, see below.
    }

    genbankColumnIndex = (genbankListColumnIndex != -1 ? genbankListColumnIndex : genbankAcColumnIndex);
    if (probeSetIdColumnIndex == -1 || genbankColumnIndex == -1) {
        throw new SyntaxErrorException(0,
                "One of the following column names could not be found in the platformFileContent description file: "
                        + probeIdColumnName + ", (at least one of : " + genbankAcColumnName + ", "
                        + genbankListColumnName + " ).");
    }

    int count = 0;

    while (!ri.end()) {
        try {
            final String probesetId = (String) platformFileContent.getValue(probeSetIdColumnIndex, ri);
            final String genbankAccession = (String) platformFileContent.getValue(genbankColumnIndex, ri);
            probesetId2GenbankList.put(probesetId, genbankAccession);
            count++;
        } catch (TypeMismatchException e) {
            throw new InternalError("Column type does not match" + e.getMessage());
        } catch (InvalidColumnException e) {
            throw new InternalError("Should never happen");

        }
        ri.next();
    }
    this.count = count;
}

From source file:com.zimbra.cs.store.Blob.java

public InputStream getInputStream() throws IOException {
    InputStream in = new ZSharedFileInputStream(file);
    if (isCompressed()) {
        in = new GZIPInputStream(in);
    }/*from   w  w w.  j  a  v  a2  s  .c o m*/
    return in;
}

From source file:com.discogs.api.webservice.impl.GZipCapableGetMethod.java

/**
 * Overrides method in {@link GetMethod} to set the responseStream variable appropriately.
 *
 * If the response body was GZIP-compressed, responseStream will be set to a GZIPInputStream
 * wrapping the original InputStream used by the superclass.
 *
 */// www .j a  v a 2s  .c  o m
protected void readResponseBody(HttpState state, HttpConnection conn) throws IOException, HttpException {
    super.readResponseBody(state, conn);

    Header contentEncodingHeader = getResponseHeader("Content-Encoding");

    if (contentEncodingHeader != null && contentEncodingHeader.getValue().equalsIgnoreCase("gzip")) {
        setResponseStream(new GZIPInputStream(getResponseStream()));
    }
}