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.feedzai.fos.impl.r.RandomForestPMMLProducerConsumerTest.java

@Test
public void testCompressed() throws Exception {
    ModelConfig modelConfig = setupConfig();
    RManager rManager = setupManager();//from w w  w  .ja  v a  2s .  co m

    UUID uuid = rManager.trainAndAdd(modelConfig, RIntegrationTest.getTrainingInstances());

    File targetFile = Files.createTempFile("targetPMML", ".xml").toFile();

    // Save the model as PMML and load it.
    rManager.saveAsPMML(uuid, targetFile.getAbsolutePath(), true);

    try (FileInputStream fis = new FileInputStream(targetFile);
            GZIPInputStream gis = new GZIPInputStream(fis)) {

        JAXBUtil.unmarshalPMML(new StreamSource(gis));
    }

    targetFile.delete();
}

From source file:com.msr.dnsdemo.network.DownloadFile.java

private InputStream openURL(String url) {
    HttpGet httpget = new HttpGet(url);
    HttpResponse response;/*from   ww  w. j av a2 s . co m*/
    try {
        try {
            response = httpclient.execute(httpget);
        } catch (SSLException e) {
            Log.i(TAG, "SSL Certificate is not trusted");
            response = httpclient.execute(httpget);
        }
        Log.i(TAG, "Status:[" + response.getStatusLine().toString() + "]");
        HttpEntity entity = response.getEntity();

        if (entity != null) {
            return new GZIPInputStream(entity.getContent());
        }
    } catch (ClientProtocolException e) {
        Log.e(TAG, "There was a protocol based error", e);
    } catch (UnknownHostException e) {
        Log.e(TAG, e.getMessage());
    } catch (IOException e) {
        Log.e(TAG, "There was an IO Stream related error", e);
    }

    return null;
}

From source file:com.cloudant.sync.datastore.AttachmentStreamFactory.java

/**
 * Return a stream to be used to read from the file on disk.
 *
 * Stream's bytes will be unzipped, unencrypted attachment content.
 *
 * @param file File object to read from.
 * @param encoding Encoding of attachment.
 * @return Stream for reading attachment data.
 * @throws IOException if there's a problem reading from disk, including issues with
 *      encryption (bad key length and other key issues).
 *//* w  w w .j  a v a 2s . co  m*/
public InputStream getInputStream(File file, Attachment.Encoding encoding) throws IOException {

    // First, open a stream to the raw bytes on disk.
    // Then, if we have a key assume the file is encrypted, so add a stream
    // to the chain which decrypts the data as we read from disk.
    // Finally, decode (unzip) the data if the attachment is encoded before
    // returning the data to the user.
    //
    //  Read from disk [-> Decryption Stream] [-> Decoding Stream] -> user reads

    InputStream is = new FileInputStream(file);

    if (key != null) {
        try {
            is = new EncryptedAttachmentInputStream(is, key);
        } catch (InvalidKeyException ex) {
            // Replace with an IOException as we validate the key when opening
            // the databases and the key should be the same -- it's therefore
            // not worth forcing the developer to catch something they can't
            // fix during file read; generic IOException works better.
            throw new IOException("Bad key used to open file; check encryption key.", ex);
        }
    }

    switch (encoding) {
    case Plain:
        break; // nothing to do
    case Gzip:
        is = new GZIPInputStream(is);
    }

    return is;
}

From source file:io.bosh.client.jobs.SpringJobs.java

private File decompress(InputStream compressed) {
    File file = null;/*from w ww .  java2  s.  c om*/
    try {
        file = File.createTempFile(UUID.randomUUID().toString(), ".log");
    } catch (IOException e) {
        throw new DirectorException("Unable to create temporary file to decompress log data", e);
    }
    try (InputStream ungzippedResponse = new GZIPInputStream(compressed);
            FileWriter writer = new FileWriter(file);
            Reader reader = new InputStreamReader(ungzippedResponse, "UTF-8")) {
        char[] buffer = new char[10240];
        for (int length = 0; (length = reader.read(buffer)) > 0;) {
            writer.write(buffer, 0, length);
        }
        return file;
    } catch (IOException e) {
        throw new DirectorException("Unable to decompress log data", e);
    }
}

From source file:com.google.code.rptm.mailarchive.DefaultMailingListArchive.java

private File getMboxFile(String mailingList, YearMonth month, MailingListArchiveEventListener eventListener)
        throws MailingListArchiveException {
    MboxKey mboxKey = new MboxKey(mailingList, month);
    File mbox = cache.get(mboxKey);
    if (mbox != null) {
        return mbox;
    }//ww  w .j  a va2  s. com
    String archiveUrl = getMailArchiveForList(mailingList);
    Repository repo = new Repository(null, archiveUrl);
    try {
        Set<File> tempFiles = new HashSet<File>();
        Wagon wagon = wagonManager.getWagon(repo);
        wagon.connect(repo, wagonManager.getProxy(repo.getProtocol()));
        try {
            mbox = File.createTempFile(mailingList, ".mbox");
            tempFiles.add(mbox);
            logger.debug("Getting " + archiveUrl + month.toSimpleFormat());
            wagon.get(month.toSimpleFormat(), mbox);
            eventListener.mboxLoaded(mailingList, month);
            if (!isMboxFile(mbox)) {
                logger.debug(archiveUrl + month.toSimpleFormat()
                        + " appears to be compressed; attempting to uncompress it");
                File compressedMbox = mbox;
                mbox = File.createTempFile(mailingList, ".mbox");
                tempFiles.add(mbox);
                InputStream in = new FileInputStream(compressedMbox);
                try {
                    OutputStream out = new FileOutputStream(mbox);
                    try {
                        IOUtils.copy(new GZIPInputStream(in), out);
                    } finally {
                        out.close();
                    }
                } finally {
                    in.close();
                }
            }
            cache.put(mboxKey, mbox);
            tempFiles.remove(mbox);
            return mbox;
        } finally {
            wagon.disconnect();
            for (File tempFile : tempFiles) {
                tempFile.delete();
            }
        }
    } catch (WagonException ex) {
        throw new MailingListArchiveException("Wagon exception: " + ex.getMessage(), ex);
    } catch (IOException ex) {
        throw new MailingListArchiveException("Unexpected I/O exception: " + ex.getMessage(), ex);
    }
}

From source file:uk.ac.ebi.eva.pipeline.jobs.AggregatedVcfJobTest.java

@Test
public void aggregatedTransformAndLoadShouldBeExecuted() throws Exception {
    Config.setOpenCGAHome(opencgaHome);//from www  . j a v a2s. co m

    JobExecution jobExecution = jobLauncherTestUtils.launchJob();

    assertEquals(ExitStatus.COMPLETED, jobExecution.getExitStatus());
    assertEquals(BatchStatus.COMPLETED, jobExecution.getStatus());

    // check execution flow
    Assert.assertEquals(2, jobExecution.getStepExecutions().size());
    List<StepExecution> steps = new ArrayList<>(jobExecution.getStepExecutions());
    StepExecution transformStep = steps.get(0);
    StepExecution loadStep = steps.get(1);

    Assert.assertEquals(AggregatedVcfJob.NORMALIZE_VARIANTS, transformStep.getStepName());
    Assert.assertEquals(AggregatedVcfJob.LOAD_VARIANTS, loadStep.getStepName());

    assertTrue(transformStep.getEndTime().before(loadStep.getStartTime()));

    // check transformed file
    String outputFilename = getTransformedOutputPath(Paths.get(input).getFileName(), compressExtension,
            outputDir);

    long lines = JobTestUtils.getLines(new GZIPInputStream(new FileInputStream(outputFilename)));
    assertEquals(156, lines);

    // check ((documents in DB) == (lines in transformed file))
    VariantStorageManager variantStorageManager = StorageManagerFactory.getVariantStorageManager();
    VariantDBAdaptor variantDBAdaptor = variantStorageManager.getDBAdaptor(dbName, null);
    VariantDBIterator iterator = variantDBAdaptor.iterator(new QueryOptions());

    Assert.assertEquals(JobTestUtils.count(iterator), lines);

    // check that stats are loaded properly
    assertFalse(variantDBAdaptor.iterator(new QueryOptions()).next().getSourceEntries().values().iterator()
            .next().getCohortStats().isEmpty());
}

From source file:io.fabric8.spi.process.AbstractProcessHandler.java

@Override
public final ManagedProcess create(AgentRegistration agentReg, ProcessOptions options,
        ProcessIdentity identity) {//from  www .j  a  v  a  2 s  .com

    File targetDir = options.getTargetPath().toAbsolutePath().toFile();
    IllegalStateAssertion.assertTrue(targetDir.isDirectory() || targetDir.mkdirs(),
            "Cannot create target dir: " + targetDir);

    File homeDir = null;
    for (MavenCoordinates artefact : options.getMavenCoordinates()) {
        Resource resource = mavenRepository.findMavenResource(artefact);
        IllegalStateAssertion.assertNotNull(resource, "Cannot find maven resource: " + artefact);

        ResourceContent content = resource.adapt(ResourceContent.class);
        IllegalStateAssertion.assertNotNull(content, "Cannot obtain resource content for: " + artefact);

        try {
            ArchiveInputStream ais;
            if ("tar.gz".equals(artefact.getType())) {
                InputStream inputStream = content.getContent();
                ais = new TarArchiveInputStream(new GZIPInputStream(inputStream));
            } else {
                InputStream inputStream = content.getContent();
                ais = new ArchiveStreamFactory().createArchiveInputStream(artefact.getType(), inputStream);
            }
            ArchiveEntry entry = null;
            boolean needContainerHome = homeDir == null;
            while ((entry = ais.getNextEntry()) != null) {
                File targetFile;
                if (needContainerHome) {
                    targetFile = new File(targetDir, entry.getName());
                } else {
                    targetFile = new File(homeDir, entry.getName());
                }
                if (!entry.isDirectory()) {
                    File parentDir = targetFile.getParentFile();
                    IllegalStateAssertion.assertTrue(parentDir.exists() || parentDir.mkdirs(),
                            "Cannot create target directory: " + parentDir);

                    FileOutputStream fos = new FileOutputStream(targetFile);
                    copyStream(ais, fos);
                    fos.close();

                    if (needContainerHome && homeDir == null) {
                        File currentDir = parentDir;
                        while (!currentDir.getParentFile().equals(targetDir)) {
                            currentDir = currentDir.getParentFile();
                        }
                        homeDir = currentDir;
                    }
                }
            }
            ais.close();
        } catch (RuntimeException rte) {
            throw rte;
        } catch (Exception ex) {
            throw new IllegalStateException("Cannot extract artefact: " + artefact, ex);
        }
    }

    managedProcess = new DefaultManagedProcess(identity, options, homeDir.toPath(), State.CREATED);
    managedProcess.addAttribute(ManagedProcess.ATTRIBUTE_KEY_AGENT_REGISTRATION, agentReg);
    managedProcess.addAttribute(ContainerAttributes.ATTRIBUTE_KEY_AGENT_JMX_SERVER_URL,
            agentReg.getJmxServerUrl());
    managedProcess.addAttribute(ContainerAttributes.ATTRIBUTE_KEY_AGENT_JMX_USERNAME,
            agentReg.getJmxUsername());
    managedProcess.addAttribute(ContainerAttributes.ATTRIBUTE_KEY_AGENT_JMX_PASSWORD,
            agentReg.getJmxPassword());
    try {
        doConfigure(managedProcess);
    } catch (Exception ex) {
        throw new LifecycleException("Cannot configure container", ex);
    }
    return new ImmutableManagedProcess(managedProcess);
}

From source file:com.tedx.webservices.WebServices.java

public static JSONObject SendHttpPost(String URL, JSONObject jsonObjSend) {
    try {//  ww w .ja 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");
        httpPostRequest.setHeader("Content-type", "application/json");
        //httpPostRequest.setHeader("Accept-Encoding", "gzip"); // only set this parameter if you would like to use gzip compression

        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);
            instream.close();

            // remove wrapping "[" and "]"
            if (resultString.substring(0, 1).contains("["))
                resultString = resultString.substring(1, resultString.length() - 1);

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

            return jsonObjRecv;
        }

    } catch (Exception e) {
        // More about HTTP exception handling in another tutorial.
        // For now we just print the stack trace.
        e.printStackTrace();
    }
    return null;
}

From source file:fr.ens.biologie.genomique.eoulsan.io.CompressionType.java

/**
 * Create a GZip input stream./*from  w w  w.j a va2s.c  o  m*/
 * @param is the input stream to uncompress
 * @return a uncompressed input stream
 * @throws IOException if an error occurs while creating the input stream
 */
public static InputStream createGZipInputStream(final InputStream is) throws IOException {

    return new GZIPInputStream(is);
}

From source file:org.calrissian.accumulorecipes.blobstore.impl.ext.ExtendedAccumuloBlobStoreTest.java

@Test
public void testSaveAndQueryComplex() throws Exception {
    ExtendedAccumuloBlobStore blobStore = new ExtendedAccumuloBlobStore(getConnector(), CHUNK_SIZE);

    Collection<String> testValues = new ArrayList<String>(10);
    for (int i = 0; i < CHUNK_SIZE; i++)
        testValues.add(randomUUID().toString());

    //Store json in a Gzipped format
    OutputStream storageStream = blobStore.store("test", "1", currentTimeMillis(), "");
    mapper.writeValue(new GZIPOutputStream(storageStream), testValues);

    //reassemble the json after unzipping the stream.
    InputStream retrievalStream = blobStore.get("test", "1", Auths.EMPTY);
    Collection<String> actualValues = mapper.readValue(new GZIPInputStream(retrievalStream), strColRef);

    //if there were no errors, then verify that the two collections are equal.
    assertThat(actualValues, is(equalTo(testValues)));
}