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) throws IOException 

Source Link

Document

Creates a new output stream with a default buffer size.

Usage

From source file:com.jaspersoft.jasperserver.core.util.StreamUtil.java

public static byte[] compress(Object object) throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    GZIPOutputStream gzos = new GZIPOutputStream(baos);
    ObjectOutputStream oos = new ObjectOutputStream(gzos);
    oos.writeObject(object);/*  ww  w  .  ja  v  a  2 s  . com*/
    oos.flush();
    oos.close();
    return baos.toByteArray();
}

From source file:com.bstek.dorado.web.resolver.AbstractWebFileResolver.java

protected OutputStream getOutputStream(HttpServletRequest request, HttpServletResponse response,
        ResourcesWrapper resourcesWrapper) throws IOException {
    int encodingType = 0;
    String encoding = request.getHeader(HttpConstants.ACCEPT_ENCODING);
    if (encoding != null) {
        encodingType = (encoding.indexOf(HttpConstants.GZIP) >= 0) ? 1
                : (encoding.indexOf(HttpConstants.COMPRESS) >= 0 ? 2 : 0);
    }/*from w w w .  j a va2 s . c  om*/

    OutputStream out = response.getOutputStream();
    if (encodingType > 0 && shouldCompress(resourcesWrapper)) {
        try {
            if (encodingType == 1) {
                response.setHeader(HttpConstants.CONTENT_ENCODING, HttpConstants.GZIP);
                out = new GZIPOutputStream(out);
            } else if (encodingType == 2) {
                response.setHeader(HttpConstants.CONTENT_ENCODING, HttpConstants.COMPRESS);
                out = new ZipOutputStream(out);
            }
        } catch (IOException e) {
            // do nothing
        }
    }
    return out;
}

From source file:org.ardverk.daap.DaapUtil.java

/**
 * Serializes the <code>chunk</code> and compresses it optionally. The
 * serialized data is returned as a byte-Array.
 *//*  w  ww. ja va 2 s .c o m*/
public static final byte[] serialize(Chunk chunk, boolean compress) throws IOException {
    ByteArrayOutputStream buffer = new ByteArrayOutputStream(255);
    DaapOutputStream out = null;

    if (DaapUtil.COMPRESS && compress) {
        GZIPOutputStream gzip = new GZIPOutputStream(buffer);
        out = new DaapOutputStream(gzip);
    } else {
        out = new DaapOutputStream(buffer);
    }

    out.writeChunk(chunk);
    out.close();

    return buffer.toByteArray();
}

From source file:de.kapsi.net.daap.DaapUtil.java

/**
 * Serializes the <code>chunk</code> and compresses it optionally.
 * The serialized data is returned as a byte-Array.
 */// w w w  . j a  va  2s. co  m
public static final byte[] serialize(Chunk chunk, boolean compress) throws IOException {

    ByteArrayOutputStream buffer = new ByteArrayOutputStream(chunk.getSize());

    if (compress) {
        GZIPOutputStream gzip = new GZIPOutputStream(buffer);
        chunk.serialize(gzip);
        gzip.finish();
        gzip.close();
    } else {
        chunk.serialize(buffer);
        buffer.flush();
        buffer.close();
    }

    return buffer.toByteArray();
}

From source file:de.tudarmstadt.lt.n2n.annotators.JoBimPrinter.java

private void openPrintToFileStream() throws IOException {
    OutputStream os = new FileOutputStream(_printstream_as_string, true);
    if (_printstream_as_string.endsWith(".gz"))
        os = new GZIPOutputStream(os) {
            {/*from w  ww .  jav  a 2 s  . co  m*/
                def.setLevel(Deflater.BEST_COMPRESSION);
            }
        };
    _printstream = new PrintStream(os);
    _printstream.flush();
    _prints_to_sys = false;
}

From source file:com.ignorelist.kassandra.steam.scraper.FileCache.java

private void putNonBlocking(String key, InputStream value) throws IOException {
    final Path cacheFile = buildCacheFile(key);
    OutputStream outputStream = new GZIPOutputStream(Files.newOutputStream(cacheFile));
    try {/* ww  w .  j  a va 2 s.  co  m*/
        IOUtils.copy(value, outputStream);
    } catch (IOException e) {
        IOUtils.closeQuietly(outputStream);
        Files.delete(cacheFile);
        throw e;
    } finally {
        IOUtils.closeQuietly(outputStream);
    }
}

From source file:com.atlassian.clover.reporters.xml.XMLReporter.java

private XMLWriter initWriter() throws IOException {
    OutputStream os;/* w w w .  j av a2 s  .  co m*/

    final File outFile = reportConfig.getOutFile();
    if (outFile.getParent() != null && !outFile.getParentFile().exists()) {
        outFile.getParentFile().mkdirs();
    }
    if (reportConfig.isCompress()) {
        os = new BufferedOutputStream(new GZIPOutputStream(new FileOutputStream(outFile)));
    } else {
        os = new BufferedOutputStream(new FileOutputStream(outFile));
    }
    return new XMLWriter(os, "UTF-8");
}

From source file:com.nextdoor.bender.ipc.http.HttpTransportTest.java

@Test(expected = TransportException.class)
public void testGzipErrorsResponse() throws TransportException, IOException {
    byte[] respPayload = "gzip resp".getBytes(StandardCharsets.UTF_8);

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    GZIPOutputStream os = new GZIPOutputStream(baos);
    os.write(respPayload);//  ww w .  j  a  v a 2s  .c  om
    os.close();
    byte[] compressedResponse = baos.toByteArray();

    HttpClient client = getMockClientWithResponse(compressedResponse, ContentType.DEFAULT_BINARY,
            HttpStatus.SC_INTERNAL_SERVER_ERROR, true);
    HttpTransport transport = new HttpTransport(client, "", true, 1, 1);

    try {
        transport.sendBatch("foo".getBytes());
    } catch (Exception e) {
        assertEquals("http transport call failed because \"expected failure\" payload response \"gzip resp\"",
                e.getCause().getMessage());
        throw e;
    }
}

From source file:de.tudarmstadt.lt.lm.app.GenerateNgrams.java

public static File generateNgrams(File src_dir, AbstractStringProvider prvdr, int from_cardinality,
        int to_cardinality, boolean overwrite) {
    final File ngram_file = new File(src_dir, String.format("%s.%s", src_dir.getName(), "ngrams.txt.gz"));
    int n_b = from_cardinality, n_e = to_cardinality;
    if (ngram_file.exists()) {
        LOG.info("Output file already exists: '{}'.", ngram_file.getAbsolutePath());
        if (overwrite) {
            ngram_file.delete();/*from   w  ww . j  a v a 2s .c om*/
            LOG.info("Overwriting file: '{}'.", ngram_file.getAbsolutePath());
        } else
            return ngram_file;
    }

    File[] src_files = src_dir.listFiles(new FileFilter() {
        @Override
        public boolean accept(File f) {
            return f.isFile() && f.getName().endsWith(".txt") && (!f.equals(ngram_file));
        }
    });

    String[] basenames = new String[src_files.length];
    for (int i = 0; i < basenames.length; i++)
        basenames[i] = src_files[i].getName();

    LOG.info(String.format("Reading txt files from dir: '%s'; Files: %s.", src_dir.getAbsolutePath(),
            StringUtils.abbreviate(Arrays.toString(basenames), 200)));
    LOG.info(String.format("Writing ngrams to file: '%s'.", ngram_file.getAbsolutePath()));

    PrintWriter pw = null;
    try {
        pw = new PrintWriter(
                new OutputStreamWriter(new GZIPOutputStream(new FileOutputStream(ngram_file)), "UTF-8"));
    } catch (IOException e) {
        LOG.error("Could not open writer for file: '{}'.", ngram_file.getAbsolutePath(), e);
        return null;
    }

    long num_ngrams = 0l;
    List<String>[] ngrams = null;
    for (int i = 0; i < src_files.length; i++) {
        File src_file = src_files[i];
        LOG.info("Processing file {} / {} ('{}')", i + 1, src_files.length, src_file.getAbsolutePath());
        long num_ngrams_f = 0l;

        try {
            LineIterator liter = new LineIterator(
                    new BufferedReader(new InputStreamReader(new FileInputStream(src_file), "UTF-8")));
            int lc = 0;
            while (liter.hasNext()) {
                if (++lc % 1000 == 0)
                    LOG.debug("Processing line {} ({})", lc, src_file);
                String line = liter.next();
                for (String sentence : prvdr.splitSentences(line)) {
                    for (int n = n_b; n <= n_e; n++) {
                        ngrams = null;
                        try {
                            List<String> tokens = prvdr.tokenizeSentence(sentence);
                            if (tokens.isEmpty())
                                continue;
                            ngrams = AbstractLanguageModel.getNgramSequence(tokens, n);
                        } catch (Exception e) {
                            LOG.warn(
                                    "Could not get ngram of cardinality {} from String '{}' in line '{}' from file '{}'.",
                                    n, StringUtils.abbreviate(line, 100), lc, src_file.getAbsolutePath());
                            continue;
                        }
                        for (List<String> ngram : ngrams)
                            pw.println(StringUtils.join(ngram, " "));
                        pw.flush();
                        num_ngrams_f += ngrams.length;
                    }
                }
            }
            liter.close();
        } catch (Exception e) {
            LOG.warn("Could not read file '{}'.", src_file.getAbsolutePath(), e);
        }
        LOG.debug("Generated {} ngrams from file {}.", num_ngrams_f, src_file);
        num_ngrams += num_ngrams_f;
    }

    if (pw != null)
        pw.close();

    LOG.info("Generated {} ngrams.", num_ngrams);

    return ngram_file;
}

From source file:com.insightml.utils.io.IoUtils.java

public static BufferedWriter writer(final File file, final boolean gzip) {
    try {//from  w w  w  .  ja va  2s. com
        final int sz = (int) Math.pow(1024, 2);
        if (gzip) {
            return new BufferedWriter(new OutputStreamWriter(new GZIPOutputStream(new FileOutputStream(file))),
                    sz);
        }
        return new BufferedWriter(new FileWriter(file), sz);
    } catch (final IOException e) {
        throw new IllegalArgumentException(e);
    }
}