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.jaspersoft.jasperserver.api.metadata.common.domain.util.GzipDataContainer.java

public InputStream getInputStream() {
    InputStream in = decorated.getInputStream();
    if (in == null) {
        return null;
    }/*from  w  w w.ja  v a2s.  co  m*/

    try {
        return new GZIPInputStream(in);
    } catch (IOException e) {
        throw new JSExceptionWrapper(e);
    }
}

From source file:com.github.helenusdriver.commons.lang3.SerializationUtils.java

/**
 * Decompresses and deserializes an object from the specified stream.
 * <p>/*from w w w .  j a  va  2  s  .co  m*/
 * The stream will be closed once the object is written. This
 * avoids the need for a finally clause, and maybe also exception
 * handling, in the application code.
 * <p>
 * The stream passed in is not buffered internally within this method.
 * This is the responsibility of your application if desired.
 *
 * @author paouelle
 *
 * @param  inputStream the serialized object input stream
 * @return the corresponding object
 * @throws NullPointerException if <code>outputStream</code> is <code>null</code>
 * @throws SerializationException (runtime) if the serialization fails
 */
public static Object decompressAndDeserialize(InputStream inputStream) {
    org.apache.commons.lang3.Validate.notNull(inputStream, "invalid null inputStream");
    try {
        final GZIPInputStream is = new GZIPInputStream(inputStream);

        return org.apache.commons.lang3.SerializationUtils.deserialize(is);
    } catch (IOException e) {
        throw new SerializationException(e);
    }
}

From source file:com.linkedin.pinot.core.data.readers.AvroRecordReader.java

@Override
public void init() throws Exception {
    final File file = new File(_fileName);
    if (!file.exists()) {
        throw new FileNotFoundException("File is not existed!");
    }//from   w  w w  .  j a va2s.c om
    //_schemaExtractor = FieldExtractorFactory.get(_dataReaderSpec);
    if (_fileName.endsWith("gz")) {
        _dataStream = new DataFileStream<GenericRecord>(new GZIPInputStream(new FileInputStream(file)),
                new GenericDatumReader<GenericRecord>());
    } else {
        _dataStream = new DataFileStream<GenericRecord>(new FileInputStream(file),
                new GenericDatumReader<GenericRecord>());
    }
}

From source file:carmen.utils.Utils.java

public static Scanner createScanner(String inputFile) throws IOException {
    InputStream inputStream = null;
    if (new File(inputFile).getName().endsWith(".gz")) {
        inputStream = new BufferedInputStream(new GZIPInputStream(new FileInputStream(inputFile)));
    } else {/*  w  ww .  j a  v a  2 s  . c om*/
        inputStream = new BufferedInputStream(new FileInputStream(inputFile));
    }
    return new Scanner(inputStream, "UTF-8");
}

From source file:net.darkmist.clf.IndexedLogFileHandler.java

/** Checks for index file and presence of the user in the index if it exists.
 * @param file The log file being examined
 * @param base The base name of the log file
 * @return true if the index file exists and the index file does NOT contain the user.
 */// w w w.  ja v  a2  s. c  o  m
private boolean notInIndex(File file, String base) {
    File dir;
    File index;
    InputStream in = null;
    String previousThreadName;
    Thread thread;
    int bufSize;

    if ((dir = file.getParentFile()) == null) {
        logger.warn("Log file " + file + " doesn't have a parent directory?");
        return false;
    }
    index = new File(dir, base + INDEX_EXT);
    if (!index.exists()) {
        if (logger.isDebugEnabled())
            logger.debug("no index file " + index + " for log file " + file);
        return false;
    }
    thread = Thread.currentThread();
    previousThreadName = thread.getName();
    thread.setName("idx: " + index);
    if (logger.isDebugEnabled())
        logger.debug("index scan of " + index);
    bufSize = (int) Math.min(index.length(), MAX_BUF_SIZE);
    try {
        in = new GZIPInputStream(new BufferedInputStream(new FileInputStream(index), bufSize));
        if (LineMatcher.readerContains(usrPat, new BufferedReader(new InputStreamReader(in)))) {
            logger.debug("found usr in index");
            return false;
        }
        logger.debug("did not find usr in index");
        return true;
    } catch (IOException e) {
        logger.warn("IOException reading from index " + index, e);
    } finally {
        in = Util.close(in, logger, index.toString());
        thread.setName(previousThreadName);
    }
    return false;
}

From source file:com.omertron.thetvdbapi.tools.WebBrowser.java

/**
 * Request the web page at the specified URL
 *
 * @param url/*w w  w  . j  a va 2 s  .  co m*/
 * @throws IOException
 */
public static String request(URL url) throws IOException {
    StringBuilder content = new StringBuilder();

    BufferedReader in = null;
    URLConnection cnx = null;
    InputStreamReader isr = null;
    GZIPInputStream zis = null;

    try {
        cnx = openProxiedConnection(url);

        sendHeader(cnx);
        readHeader(cnx);

        // Check the content encoding of the connection. Null content encoding is standard HTTP
        if (cnx.getContentEncoding() == null) {
            //in = new BufferedReader(new InputStreamReader(cnx.getInputStream(), getCharset(cnx)));
            isr = new InputStreamReader(cnx.getInputStream(), "UTF-8");
        } else if (cnx.getContentEncoding().equalsIgnoreCase("gzip")) {
            zis = new GZIPInputStream(cnx.getInputStream());
            isr = new InputStreamReader(zis, "UTF-8");
        } else {
            return "";
        }

        in = new BufferedReader(isr);

        String line;
        while ((line = in.readLine()) != null) {
            content.append(line);
        }
    } finally {
        if (in != null) {
            try {
                in.close();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }

        if (isr != null) {
            try {
                isr.close();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }

        if (zis != null) {
            try {
                zis.close();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }

        if (cnx instanceof HttpURLConnection) {
            ((HttpURLConnection) cnx).disconnect();
        }
    }
    return content.toString();
}

From source file:com.vk.sdk.api.httpClient.VKHttpOperation.java

/**
 * Start current prepared http-operation for result
 *//*from  w  ww  . j  a  va  2 s.  co  m*/
@Override
public void start() {
    setState(VKOperationState.Executing);
    try {
        if (mUriRequest.isAborted())
            return;
        response = VKHttpClient.getClient().execute(mUriRequest);
        InputStream inputStream = response.getEntity().getContent();
        Header contentEncoding = response.getFirstHeader("Content-Encoding");
        if (contentEncoding != null && contentEncoding.getValue().equalsIgnoreCase("gzip")) {
            inputStream = new GZIPInputStream(inputStream);
        }

        if (outputStream == null) {
            outputStream = new ByteArrayOutputStream();
        }

        byte[] buffer = new byte[1024];
        int bytesRead;
        while ((bytesRead = inputStream.read(buffer)) != -1)
            outputStream.write(buffer, 0, bytesRead);
        inputStream.close();
        outputStream.flush();
        if (outputStream instanceof ByteArrayOutputStream) {
            mResponseBytes = ((ByteArrayOutputStream) outputStream).toByteArray();
        }
        outputStream.close();
    } catch (Exception e) {
        mLastException = e;
    }
    setState(VKOperationState.Finished);
}

From source file:com.pinterest.terrapin.zookeeper.ViewInfoTest.java

@Test
public void testCompressedSerialization() throws Exception {
    // Check that compressed json serialization works correctly.
    byte[] compressedJson = viewInfo.toCompressedJson();
    GZIPInputStream zipIn = new GZIPInputStream(new ByteArrayInputStream(compressedJson));
    assertEquals(JSON, new String(IOUtils.toByteArray(zipIn)));

    // Check that compressed json deserialization works correctly.
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    GZIPOutputStream zipOut = new GZIPOutputStream(out);
    zipOut.write(JSON.getBytes());/*from w  w w.ja va  2s . com*/
    zipOut.close();
    assertEquals(viewInfo, ViewInfo.fromCompressedJson(out.toByteArray()));
}

From source file:cn.keke.travelmix.publictransport.type.EfaConnectionResponseHandler.java

public void handle(HttpResponse response) throws IOException {
    if (this.job.isFinished()) {
        return;/*w w  w  .  j  av  a 2 s  .  c  om*/
    }
    HttpEntity entity = response.getEntity();
    BufferedInputStream in;
    if (this.zipped) {
        in = new BufferedInputStream(new GZIPInputStream(entity.getContent()));
    } else {
        in = new BufferedInputStream(entity.getContent());
    }

    String responseText = IOUtils.toString(in, CHARSET_ISO_8859_1);
    if (this.job.isFinished()) {
        return;
    }
    // LOG.info("PT response: " + responseText);
    LinkedList<PartialRoute> partialRoutes = parseExternalRouteResponse(responseText);
    if (!partialRoutes.isEmpty()) {
        LOG.info("Got " + partialRoutes.size() + " partial routes");
        if (!this.job.setFinished(this.url)) {
            return;
        }
        RouteResult result = readRouteInfo(partialRoutes);
        createRouteResponse(this.sb, result);
        this.job.setHandled();
    } else {
        LOG.info("No partial routes received: " + url);
    }
}

From source file:net.myrrix.common.io.IOUtils.java

/**
 * Opens an {@link InputStream} to the file. If it appears to be compressed, because its file name ends in
 * ".gz" or ".zip" or ".deflate", then it will be decompressed accordingly
 *
 * @param file file, possibly compressed, to open
 * @return {@link InputStream} on uncompressed contents
 * @throws IOException if the stream can't be opened or is invalid or can't be read
 *///  w ww . j  a v  a2 s .c  o m
public static InputStream openMaybeDecompressing(File file) throws IOException {
    String name = file.getName();
    InputStream in = new FileInputStream(file);
    if (name.endsWith(".gz")) {
        return new GZIPInputStream(in);
    }
    if (name.endsWith(".zip")) {
        return new ZipInputStream(in);
    }
    if (name.endsWith(".deflate")) {
        return new InflaterInputStream(in);
    }
    if (name.endsWith(".bz2") || name.endsWith(".bzip2")) {
        return new BZip2CompressorInputStream(in);
    }
    return in;
}