Example usage for java.util.zip GZIPOutputStream GZIPOutputStream

List of usage examples for java.util.zip GZIPOutputStream GZIPOutputStream

Introduction

In this page you can find the example usage for java.util.zip GZIPOutputStream GZIPOutputStream.

Prototype

public GZIPOutputStream(OutputStream out, boolean syncFlush) throws IOException 

Source Link

Document

Creates a new output stream with a default buffer size and the specified flush mode.

Usage

From source file:com.javacreed.examples.sql.Example2.java

public static void main(final String[] args) throws Exception {
    try (BasicDataSource dataSource = DatabaseUtils.createDataSource();
            Connection connection = dataSource.getConnection()) {
        final ExampleTest test = new ExampleTest(connection, "compressed_table", "compressed") {
            @Override/*  w ww .  j  a  va  2 s  .c o m*/
            protected String parseRow(final ResultSet resultSet) throws Exception {
                try (GZIPInputStream in = new GZIPInputStream(resultSet.getBinaryStream("compressed"))) {
                    return IOUtils.toString(in, "UTF-8");
                }
            }

            @Override
            protected void setPreparedStatement(final String data, final PreparedStatement statement)
                    throws Exception {
                // Compress the data before inserting it. We need to compress before inserting the data to make this process
                // as realistic as possible.
                final ByteArrayOutputStream baos = new ByteArrayOutputStream(data.length());
                try (OutputStream out = new GZIPOutputStream(baos, data.length())) {
                    out.write(data.getBytes("UTF-8"));
                }
                statement.setBinaryStream(1, new ByteArrayInputStream(baos.toByteArray()));
            }
        };
        test.runTest();
    }
    Example2.LOGGER.debug("Done");
}

From source file:com.milaboratory.util.CompressionType.java

private static OutputStream createOutputStream(CompressionType ct, OutputStream os) throws IOException {
    switch (ct) {
    case None:/* www. j  a v a2s . co  m*/
        return os;
    case GZIP:
        return new GZIPOutputStream(os, 2048);
    case BZIP2:
        CompressorStreamFactory factory = new CompressorStreamFactory();
        try {
            return factory.createCompressorOutputStream(CompressorStreamFactory.BZIP2,
                    new BufferedOutputStream(os));
        } catch (CompressorException e) {
            throw new IOException(e);
        }
    }
    throw new NullPointerException();
}

From source file:com.addthis.hydra.store.compress.CompressedStream.java

public static @Nonnull OutputStream compressOutputStream(@Nonnull OutputStream out,
        @Nonnull CompressionType type) throws IOException {
    switch (type) {
    case GZIP:/*from w w  w. ja va2  s  .c  om*/
        out = new GZIPOutputStream(out, BUFFER_SIZE);
        break;
    case LZF:
        out = new LZFOutputStream(out);
        break;
    case SNAPPY:
        out = new SnappyOutputStream(out);
        break;
    case BZIP2:
        out = new BZip2CompressorOutputStream(out);
        break;
    case LZMA:
        throw new UnsupportedOperationException(
                "Writing .lzma files is no longer supported. Use .xz format instead");
    case XZ:
        out = new XZOutputStream(out, new LZMA2Options());
        break;
    default:
        throw new IllegalStateException("Unknown compression type " + type);
    }
    return out;
}

From source file:com.kolich.common.util.io.GZIPCompressor.java

/**
 * Given an uncompressed InputStream, compress it and return the
 * result as new byte array.//from ww w  . j  ava 2  s .  co m
 * @return
 */
public static final byte[] compress(final InputStream is, final int outputBufferSize) {
    GZIPOutputStream gzos = null;
    try {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        gzos = new GZIPOutputStream(baos, outputBufferSize) {
            // Ugly anonymous constructor hack to set the compression
            // level on the underlying Deflater to "max compression".
            {
                def.setLevel(Deflater.BEST_COMPRESSION);
            }
        };
        IOUtils.copyLarge(is, gzos);
        gzos.finish();
        return baos.toByteArray();
    } catch (Exception e) {
        throw new GZIPCompressorException(e);
    } finally {
        closeQuietly(gzos);
    }
}

From source file:io.ecarf.core.cloud.task.processor.reason.phase2.ReasonUtils.java

/**
 * // ww w  .  java 2  s.  co  m
 * @param file
 * @param writer
 * @param compressed
 * @return
 * @throws IOException 
 */
public static int reason(String inFile, String outFile, boolean compressed, Map<Long, Set<Triple>> schemaTerms,
        Set<Long> productiveTerms) throws IOException {

    log.info("Reasoning for file: " + inFile + ", memory usage: " + Utils.getMemoryUsageInGB() + "GB");

    int inferredTriples = 0;

    // loop through the instance triples probably stored in a file and generate all the triples matching the schema triples set
    try (BufferedReader reader = getQueryResultsReader(inFile, compressed);
            PrintWriter writer = new PrintWriter(new BufferedOutputStream(
                    new GZIPOutputStream(new FileOutputStream(outFile), Constants.GZIP_BUF_SIZE),
                    Constants.GZIP_BUF_SIZE));) {

        Iterable<CSVRecord> records;

        if (compressed) {
            // ignore first row subject,predicate,object
            records = CSVFormat.DEFAULT.withHeader().withSkipHeaderRecord().parse(reader);

        } else {
            records = CSVFormat.DEFAULT.parse(reader);
        }

        Long term;

        for (CSVRecord record : records) {

            ETriple instanceTriple = ETriple.fromCSV(record.values());

            // TODO review for OWL ruleset
            if (SchemaURIType.RDF_TYPE.id == instanceTriple.getPredicate()) {

                term = instanceTriple.getObject(); // object

            } else {

                term = instanceTriple.getPredicate(); // predicate
            }

            Set<Triple> schemaTriples = schemaTerms.get(term);

            if ((schemaTriples != null) && !schemaTriples.isEmpty()) {
                productiveTerms.add(term);

                for (Triple schemaTriple : schemaTriples) {
                    Rule rule = GenericRule.getRule(schemaTriple);
                    Triple inferredTriple = rule.head(schemaTriple, instanceTriple);

                    if (inferredTriple != null) {
                        writer.println(inferredTriple.toCsv());
                        inferredTriples++;
                    }
                }
            }

        }

    }

    return inferredTriples;
}

From source file:com.milaboratory.core.io.CompressionType.java

private static OutputStream createOutputStream(CompressionType ct, OutputStream os, int buffer)
        throws IOException {
    switch (ct) {
    case None://from   www. ja v  a  2s  .  c om
        return os;
    case GZIP:
        return new GZIPOutputStream(os, buffer);
    case BZIP2:
        CompressorStreamFactory factory = new CompressorStreamFactory();
        try {
            return factory.createCompressorOutputStream(CompressorStreamFactory.BZIP2,
                    new BufferedOutputStream(os));
        } catch (CompressorException e) {
            throw new IOException(e);
        }
    }
    throw new NullPointerException();
}

From source file:com.opengamma.web.analytics.json.Compressor.java

static void compressStream(InputStream inputStream, OutputStream outputStream)
        throws IOException {
    InputStream iStream = new BufferedInputStream(inputStream);
    GZIPOutputStream oStream = new GZIPOutputStream(
            new Base64OutputStream(new BufferedOutputStream(outputStream), true, -1, null), 2048);
    byte[] buffer = new byte[2048];
    int bytesRead;
    while ((bytesRead = iStream.read(buffer)) != -1) {
        oStream.write(buffer, 0, bytesRead);
    }//from   w w  w. j av  a 2s . c om
    oStream.close(); // this is necessary for the gzip and base64 streams
}

From source file:net.yacy.cora.protocol.http.GzipCompressingEntity.java

@Override
public void writeTo(final OutputStream outstream) throws IOException {
    if (outstream == null) {
        throw new IllegalArgumentException("Output stream may not be null");
    }//from   w ww.  j a  v a 2s . c  o m
    GZIPOutputStream gzip = new GZIPOutputStream(outstream, 65536) {
        {
            def.setLevel(Deflater.BEST_SPEED);
        }
    };
    wrappedEntity.writeTo(gzip);
    gzip.finish();
}

From source file:jetbrains.exodus.env.EnvironmentTestsBase.java

public static void archiveDB(final String location, final String target) {
    try {//from   w w  w.  j  a va2  s .c  o m
        System.out.println("Dumping " + location + " to " + target);
        final File root = new File(location);
        final File targetFile = new File(target);
        TarArchiveOutputStream tarGz = new TarArchiveOutputStream(
                new GZIPOutputStream(new BufferedOutputStream(new FileOutputStream(targetFile)), 0x1000));
        for (final File file : IOUtil.listFiles(root)) {
            final long fileSize = file.length();
            if (file.isFile() && fileSize != 0) {
                CompressBackupUtil.archiveFile(tarGz, "", file, fileSize);
            }
        }
        tarGz.close();
    } catch (IOException ioe) {
        System.out.println("Can't create backup");
    }
}

From source file:nl.nn.adapterframework.extensions.cmis.CmisHttpSender.java

@Override
public HttpRequestBase getMethod(URIBuilder uri, String message, ParameterValueList pvl,
        Map<String, String> headersParamsMap, IPipeLineSession session) throws SenderException {
    HttpRequestBase method = null;//from   www  .  j av a  2s  .c om

    try {
        if (getMethodType().equals("GET")) {
            method = new HttpGet(uri.build());
        } else if (getMethodType().equals("POST")) {
            HttpPost httpPost = new HttpPost(uri.build());

            // send data
            if (pvl.getParameterValue("writer") != null) {
                Output writer = (Output) pvl.getParameterValue("writer").getValue();
                ByteArrayOutputStream out = new ByteArrayOutputStream();

                Object clientCompression = pvl.getParameterValue(SessionParameter.CLIENT_COMPRESSION);
                if ((clientCompression != null) && Boolean.parseBoolean(clientCompression.toString())) {
                    httpPost.setHeader("Content-Encoding", "gzip");
                    writer.write(new GZIPOutputStream(out, 4096));
                } else {
                    writer.write(out);
                }

                HttpEntity entity = new BufferedHttpEntity(new ByteArrayEntity(out.toByteArray()));
                httpPost.setEntity(entity);
                out.close();

                method = httpPost;
            }
        } else if (getMethodType().equals("PUT")) {
            HttpPut httpPut = new HttpPut(uri.build());

            // send data
            if (pvl.getParameterValue("writer") != null) {
                Output writer = (Output) pvl.getParameterValue("writer").getValue();
                ByteArrayOutputStream out = new ByteArrayOutputStream();

                Object clientCompression = pvl.getParameterValue(SessionParameter.CLIENT_COMPRESSION);
                if ((clientCompression != null) && Boolean.parseBoolean(clientCompression.toString())) {
                    httpPut.setHeader("Content-Encoding", "gzip");
                    writer.write(new GZIPOutputStream(out, 4096));
                } else {
                    writer.write(out);
                }

                HttpEntity entity = new BufferedHttpEntity(new ByteArrayEntity(out.toByteArray()));
                httpPut.setEntity(entity);
                out.close();

                method = httpPut;
            }
        } else if (getMethodType().equals("DELETE")) {
            method = new HttpDelete(uri.build());
        } else {
            throw new MethodNotSupportedException("method [" + getMethodType() + "] not implemented");
        }
    } catch (Exception e) {
        throw new SenderException(e);
    }

    for (Map.Entry<String, String> entry : headers.entrySet()) {
        log.debug("append header [" + entry.getKey() + "] with value [" + entry.getValue() + "]");

        method.addHeader(entry.getKey(), entry.getValue());
    }

    //Cmis creates it's own contentType depending on the method and bindingType
    method.setHeader("Content-Type", getContentType());

    log.debug(getLogPrefix() + "HttpSender constructed " + getMethodType() + "-method [" + method.getURI()
            + "] query [" + method.getURI().getQuery() + "] ");
    return method;
}