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.chenshu.compress.CompressOldTest.java

@Benchmark
public int jdkGzip2Compress() {
    ByteArrayInputStream bin = null;
    ByteArrayOutputStream bout = null;
    GZIPOutputStream gzout = null;
    try {//from w  ww.  j  a v  a 2 s.com
        bin = new ByteArrayInputStream(data);
        bout = new ByteArrayOutputStream(data.length);
        gzout = new GZIPOutputStream(bout) {
            {
                def.setLevel(level);
            }
        };
        int count;
        byte ret[] = new byte[1024];
        while ((count = bin.read(ret, 0, 1024)) != -1) {
            gzout.write(ret, 0, count);
        }
        gzout.finish();
        gzout.flush();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (gzout != null) {
            try {
                gzout.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if (bout != null) {
            try {
                bout.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    byte[] bs = bout.toByteArray();
    return bs.length;
}

From source file:com.iflytek.spider.util.GZIPUtils.java

/**
 * Returns an gzipped copy of the input array.
 *//*from w  w w . jav  a  2s .  c  o  m*/
public static final byte[] zip(byte[] in) {
    try {
        // compress using GZIPOutputStream 
        ByteArrayOutputStream byteOut = new ByteArrayOutputStream(in.length / EXPECTED_COMPRESSION_RATIO);

        GZIPOutputStream outStream = new GZIPOutputStream(byteOut);

        try {
            outStream.write(in);
        } catch (Exception e) {
            e.printStackTrace(LogUtil.getWarnStream(LOG));
        }

        try {
            outStream.close();
        } catch (IOException e) {
            e.printStackTrace(LogUtil.getWarnStream(LOG));
        }

        return byteOut.toByteArray();

    } catch (IOException e) {
        e.printStackTrace(LogUtil.getWarnStream(LOG));
        return null;
    }
}

From source file:com.eryansky.common.web.utils.WebUtils.java

/**
 * Gzip HeaderGZIPOutputStream.//from   w  ww.ja v a2s  .c  o m
 */
public static OutputStream buildGzipOutputStream(HttpServletResponse response) throws IOException {
    response.setHeader("Content-Encoding", "gzip");
    response.setHeader("Vary", "Accept-Encoding");
    return new GZIPOutputStream(response.getOutputStream());
}

From source file:com.faceye.feature.util.http.GZIPUtils.java

/**
 * Returns an gzipped copy of the input array.
 *//*  ww  w.  j a  v a2 s. co m*/
public static final byte[] zip(byte[] in) {
    try {
        // compress using GZIPOutputStream 
        ByteArrayOutputStream byteOut = new ByteArrayOutputStream(in.length / EXPECTED_COMPRESSION_RATIO);

        GZIPOutputStream outStream = new GZIPOutputStream(byteOut);

        try {
            outStream.write(in);
        } catch (Exception e) {
            LOG.error("Error writing outStream: ", e);
        }

        try {
            outStream.close();
        } catch (IOException e) {
            LOG.error("Error closing outStream: ", e);
        }

        return byteOut.toByteArray();

    } catch (IOException e) {
        LOG.error("Error: ", e);
        return null;
    }
}

From source file:de.tudarmstadt.ukp.dkpro.tc.mallet.util.MalletUtils.java

public static void writeFeatureValuesToFile(String featureValues[], String outcome, File outputFile)
        throws IOException {
    BufferedWriter bw = new BufferedWriter(
            new OutputStreamWriter(new GZIPOutputStream(new FileOutputStream(outputFile, true)), "UTF-8"));
    bw.write("\n");
    for (String featureValue : featureValues) {
        bw.write(featureValue + " ");
    }/*from   w w w  .j a  v a2s  .c om*/
    bw.write(outcome);
    bw.flush();
    bw.close();
}

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 ww.j a  v  a2  s.com*/
    zipOut.close();
    assertEquals(viewInfo, ViewInfo.fromCompressedJson(out.toByteArray()));
}

From source file:nl.opengeogroep.filesetsync.client.SyncJobState.java

public static void writeCachedFileList(String name, List<FileRecord> fileList) throws IOException {
    try (GZIPOutputStream gzOut = new GZIPOutputStream(new FileOutputStream(getFileListCacheFile(name)));
            BufferedFileListEncoder encoder = new BufferedFileListEncoder(gzOut)) {
        for (FileRecord fr : fileList) {
            encoder.write(fr);//  w w  w  .  j a  va  2  s  . c o  m
        }
    }
}

From source file:org.opendatakit.common.utils.WebUtils.java

/**
 * Safely encode a string for use as a query parameter.
 * /*from   w  w w  .  jav a2  s .  c  o m*/
 * @param rawString
 * @return encoded string
 */
public static String safeEncode(String rawString) {
    if (rawString == null || rawString.length() == 0) {
        return null;
    }

    try {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        GZIPOutputStream gzip = new GZIPOutputStream(out);
        gzip.write(rawString.getBytes(CharEncoding.UTF_8));
        gzip.finish();
        gzip.close();
        String candidate = Base64.encodeBase64URLSafeString(out.toByteArray());
        return candidate;
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
        throw new IllegalArgumentException("Unexpected failure: " + e.toString());
    } catch (IOException e) {
        e.printStackTrace();
        throw new IllegalArgumentException("Unexpected failure: " + e.toString());
    }
}

From source file:edu.umd.umiacs.clip.tools.io.GZIPFiles.java

protected static Path write(Path path, Iterable<? extends CharSequence> lines, Charset cs,
        OpenOption... options) throws IOException {
    Objects.requireNonNull(lines);
    OutputStream out = newOutputStream(path, options);
    try (BufferedWriter writer = new BufferedWriter(
            new OutputStreamWriter(new GZIPOutputStream(out), UTF_8.newEncoder().onMalformedInput(IGNORE)),
            BUFFER_SIZE)) {/*from w w  w . j  a  va  2 s  .c  o m*/
        for (CharSequence line : lines) {
            writer.append(line);
            writer.newLine();
        }
    }
    return path;
}

From source file:edu.cornell.med.icb.goby.modes.VcfToTabMode.java

/**
 * Combine tab delimited files and adjusts P-values for multiple testing.
 *
 * @throws java.io.IOException error reading / writing
 *//*from   ww w.j ava  2s  . co m*/
@Override
public void execute() throws IOException {
    Writer stream = null;
    try {
        stream = outputFilename == null ? new OutputStreamWriter(System.out)
                : outputFilename.endsWith(".gz")
                        ? new OutputStreamWriter(new GZIPOutputStream(new FileOutputStream(outputFilename)))
                        : new FileWriter(outputFilename);
        // start with an array of size 1M. This improves loading time for large datasets.

        for (String filename : inputFiles) {
            System.out.printf("Converting %s%n", filename);
            VCFParser parser = new VCFParser(filename);
            try {
                parser.readHeader();
                IntSet selectedInfoFieldGlobalIndices = new IntArraySet();
                IntSet selectedFormatFieldGlobalIndices = new IntArraySet();
                ColumnInfo infoColumn = parser.getColumns().find("INFO");
                ColumnInfo formatColumn = parser.getColumns().find("FORMAT");
                selectDefaultFields(infoColumn, formatColumn);
                // find the global field indices for the INFO fields we need to load:

                for (String selectedFieldName : selectedInfoFieldIds) {

                    if (infoColumn == null) {
                        System.err.printf("Could not find INFO column in file %s.", filename);
                        System.exit(1);
                    }
                    ColumnField selectedField = infoColumn.fields.find(selectedFieldName);

                    if (selectedField == null) {
                        System.err.printf("Could not find selection column %s in file %s.", selectedFieldName,
                                filename);
                        System.exit(1);
                    }

                    stream.write(selectedFieldName);
                    stream.write("\t");
                    selectedInfoFieldGlobalIndices.add(selectedField.globalFieldIndex);
                }
                // setup global field indices for selected FORMAT fields
                String samples[] = parser.getColumnNamesUsingFormat();
                if (formatColumn == null) {
                    System.err.printf("Could not find FORMAT column in file %s.", filename);
                    System.exit(1);
                }

                for (String sample : samples) {
                    for (String selectedGenotypeField : selectedFormatFieldIds) {
                        final String selectedFieldName = String.format("%s[%s]", sample, selectedGenotypeField);
                        final int selectedFieldIndex = parser.getGlobalFieldIndex(sample,
                                selectedGenotypeField);
                        stream.write(selectedFieldName);
                        stream.write("\t");
                        selectedFormatFieldGlobalIndices.add(selectedFieldIndex);
                    }
                }
                stream.write("\n");
                ProgressLogger pg = new ProgressLogger(LOG);
                /**
                 * This will hold global field indices for either INFO or FORMAT fields:
                 */
                IntSet selectedCombinedFieldGlobalIndices = new IntArraySet();
                selectedCombinedFieldGlobalIndices.addAll(selectedInfoFieldGlobalIndices);
                selectedCombinedFieldGlobalIndices.addAll(selectedFormatFieldGlobalIndices);
                pg.priority = org.apache.log4j.Level.INFO;
                pg.itemsName = "line";
                pg.displayFreeMemory = true;
                pg.start();
                while (parser.hasNextDataLine()) {
                    int i = 0;
                    for (final int globalFieldIndex : selectedCombinedFieldGlobalIndices) {

                        final String stringFieldValue = parser.getStringFieldValue(globalFieldIndex);
                        stream.write(stringFieldValue);
                        if (i++ < selectedCombinedFieldGlobalIndices.size()) {
                            stream.write("\t");
                        }
                    }

                    stream.write("\n");
                    parser.next();
                }
            } catch (VCFParser.SyntaxException e) {
                System.err.println(
                        "A syntax error was encountered when parsing input file. Details may be provided below. "
                                + filename);
                e.printStackTrace();
            }
        }
    } finally {
        if (outputFilename != null) {
            IOUtils.closeQuietly(stream);
        }
    }
}