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:mzmatch.ipeak.align.CowCoda.java

@SuppressWarnings("unchecked")
public static void main(String args[]) {
    final String lbl_mcq = "mcq";

    try {/*  w  w  w .  j  av a  2  s  .c  o  m*/
        Tool.init();

        // parse the commandline options
        final Options options = new Options();
        CmdLineParser cmdline = new CmdLineParser(options);

        // check whether we need to show the help
        cmdline.parse(args);
        if (options.help) {
            Tool.printHeader(System.out, application, version);
            cmdline.printUsage(System.out, "");
            return;
        }

        if (options.verbose) {
            Tool.printHeader(System.out, application, version);
            cmdline.printOptions();
        }

        // check the command-line parameters
        int filetype = JFreeChartTools.PDF;
        {
            if (options.ppm == -1) {
                System.err.println("[ERROR]: the ppm-value needs to be set.");
                System.exit(0);
            }
            if (options.order == -1) {
                System.err.println("[ERROR]: the order for the polynomial fit needs to be set.");
                System.exit(0);
            }
            if (options.maxrt == -1) {
                System.err.println("[ERROR]: the maximum retention time shift is not set.");
                System.exit(0);
            }

            if (options.image != null) {
                String extension = options.image.substring(options.image.lastIndexOf('.') + 1);
                if (extension.toLowerCase().equals("png"))
                    filetype = JFreeChartTools.PNG;
                else if (extension.toLowerCase().equals("pdf"))
                    filetype = JFreeChartTools.PDF;
                else {
                    System.err.println(
                            "[ERROR]: file extension of the image file needs to be either PDF or PNG.");
                    System.exit(0);
                }
            }

            // if the output directories do not exist, create them
            if (options.output != null)
                Tool.createFilePath(options.output, true);
            if (options.image != null)
                Tool.createFilePath(options.image, true);
            if (options.selection != null)
                Tool.createFilePath(options.selection, true);
        }

        // load the data
        if (options.verbose)
            System.out.println("Loading the data");
        double maxrt = 0;
        Vector<ParseResult> data = new Vector<ParseResult>();
        Vector<IPeakSet<IPeak>> matchdata = new Vector<IPeakSet<IPeak>>();
        for (String file : options.input) {
            System.out.println("- " + new File(file).getName());

            // load the mass chromatogram data
            ParseResult result = PeakMLParser.parse(new FileInputStream(file), true);
            data.add(result);

            // select the best mass chromatograms
            Vector<IPeak> selection = new Vector<IPeak>();
            for (IPeak peak : (IPeakSet<IPeak>) result.measurement) {
                maxrt = Math.max(maxrt, maxRT(peak));

                double mcq = codaDW(peak);
                peak.addAnnotation(lbl_mcq, Double.toString(mcq), Annotation.ValueType.DOUBLE);
                if (mcq >= options.codadw)
                    selection.add(peak);
            }

            // keep track of the selected mass chromatograms
            int id = options.input.indexOf(file);
            IPeakSet<IPeak> peakset = new IPeakSet<IPeak>(selection);
            peakset.setMeasurementID(id);
            for (IPeak mc : peakset)
                mc.setMeasurementID(id);
            matchdata.add(peakset);
        }

        // match the selection together
        if (options.verbose)
            System.out.println("Matching the data");
        Vector<IPeakSet<IPeak>> matches = IPeak.match((Vector) matchdata, options.ppm,
                new IPeak.MatchCompare<IPeak>() {
                    public double distance(IPeak peak1, IPeak peak2) {
                        double diff = Math.abs(peak1.getRetentionTime() - peak2.getRetentionTime());
                        if (diff > options.maxrt)
                            return -1;

                        Signal signal1 = new Signal(peak1.getSignal());
                        signal1.normalize();
                        Signal signal2 = new Signal(peak2.getSignal());
                        signal2.normalize();

                        double offset = bestOffSet(peak1, peak2, options.maxrt);
                        for (int i = 0; i < signal2.getSize(); ++i)
                            signal2.getX()[i] += offset;

                        double correlation = signal2
                                .pearsonsCorrelation(signal1)[Statistical.PEARSON_CORRELATION];
                        if (correlation < 0.5)
                            return -1;

                        // the match-function optimizes toward 0 (it's a distance)
                        return 1 - correlation;
                    }
                });

        // filter out all incomplete sets
        Vector<IPeakSet<IPeak>> valids = new Vector<IPeakSet<IPeak>>();
        for (IPeakSet<IPeak> set : matches) {
            if (set.size() < options.input.size())
                continue;
            valids.add((IPeakSet) set);
        }

        // calculate the alignment factors
        if (options.verbose)
            System.out.println("Calculating the alignment factors");
        double medians[] = new double[valids.size() + 2];
        DataFrame.Double dataframe = new DataFrame.Double(valids.size() + 2, options.input.size());

        medians[0] = 0;
        medians[medians.length - 1] = maxrt;
        for (int i = 0; i < options.input.size(); ++i) {
            dataframe.set(0, i, 0.1);
            dataframe.set(dataframe.getNrRows() - 1, i, 0);
        }

        for (int matchid = 0; matchid < valids.size(); ++matchid) {
            IPeakSet<IPeak> match = valids.get(matchid);

            // find the most central
            double offsets[][] = new double[match.size()][match.size()];
            for (int i = 0; i < match.size(); ++i)
                for (int j = i + 1; j < match.size(); ++j) {
                    offsets[i][j] = bestOffSet(match.get(i), match.get(j), options.maxrt);
                    offsets[j][i] = -offsets[i][j];
                }

            int besti = 0;
            double bestabssum = Double.MAX_VALUE;
            for (int i = 0; i < match.size(); ++i) {
                double abssum = 0;
                for (int j = 0; j < match.size(); ++j)
                    abssum += Math.abs(offsets[i][j]);
                if (abssum < bestabssum) {
                    besti = i;
                    bestabssum = abssum;
                }
            }

            for (int i = 0; i < match.size(); ++i)
                dataframe.set(matchid + 1, match.get(i).getMeasurementID(),
                        (i == besti ? 0 : offsets[i][besti]));

            medians[matchid + 1] = match.get(besti).getRetentionTime();
            dataframe.setRowName(matchid, Double.toString(match.get(besti).getRetentionTime()));
        }
        double minmedian = Statistical.min(medians);
        double maxmedian = Statistical.max(medians);

        // calculate for each profile the correction function
        PolynomialFunction functions[] = new PolynomialFunction[valids.size()];
        for (int i = 0; i < options.input.size(); ++i)
            functions[i] = PolynomialFunction.fit(options.order, medians, dataframe.getCol(i));

        // make a nice plot out of the whole thing
        if (options.verbose)
            System.out.println("Writing results");
        if (options.image != null) {
            org.jfree.data.xy.XYSeriesCollection dataset = new org.jfree.data.xy.XYSeriesCollection();
            JFreeChart linechart = ChartFactory.createXYLineChart(null, "Retention Time (seconds)", "offset",
                    dataset, PlotOrientation.VERTICAL, true, // legend
                    false, // tooltips
                    false // urls
            );

            // setup the colorkey
            Colormap colormap = new Colormap(Colormap.EXCEL);

            // get the structure behind the graph
            XYPlot plot = (XYPlot) linechart.getPlot();
            XYLineAndShapeRenderer renderer = (XYLineAndShapeRenderer) plot.getRenderer();

            // setup the plot area
            linechart.setBackgroundPaint(java.awt.Color.WHITE);
            linechart.setBorderVisible(false);
            linechart.setAntiAlias(true);

            plot.setBackgroundPaint(java.awt.Color.WHITE);
            plot.setDomainGridlinesVisible(true);
            plot.setRangeGridlinesVisible(true);

            // create the datasets
            for (int i = 0; i < options.input.size(); ++i) {
                org.jfree.data.xy.XYSeries series = new org.jfree.data.xy.XYSeries(dataframe.getColName(i));
                org.jfree.data.xy.XYSeries function = new org.jfree.data.xy.XYSeries(
                        dataframe.getColName(i) + "-function");
                dataset.addSeries(series);
                dataset.addSeries(function);

                renderer.setSeriesPaint(dataset.getSeriesCount() - 1, new java.awt.Color(colormap.getColor(i)));
                renderer.setSeriesPaint(dataset.getSeriesCount() - 2, new java.awt.Color(colormap.getColor(i)));

                renderer.setSeriesLinesVisible(dataset.getSeriesCount() - 2, false);
                renderer.setSeriesShapesVisible(dataset.getSeriesCount() - 2, true);

                // add the data-points
                for (int j = 0; j < valids.size(); ++j)
                    series.add(medians[j], dataframe.get(j, i));
                for (double x = minmedian; x < maxmedian; ++x)
                    function.add(x, functions[i].getY(x));
            }

            dataset.removeAllSeries();
            for (int i = 0; i < options.input.size(); ++i) {
                Function function = functions[i];

                org.jfree.data.xy.XYSeries series = new org.jfree.data.xy.XYSeries(dataframe.getColName(i));
                dataset.addSeries(series);

                renderer.setSeriesPaint(i, new java.awt.Color(colormap.getColor(i)));
                renderer.setSeriesLinesVisible(i, false);
                renderer.setSeriesShapesVisible(i, true);

                // add the data-points
                for (int j = 0; j < valids.size(); ++j)
                    series.add(medians[j], dataframe.get(j, i) - function.getY(medians[j]));
            }

            JFreeChartTools.writeAs(filetype, new FileOutputStream(options.image), linechart, 800, 500);
        }

        // save the selected
        if (options.selection != null) {
            Header header = new Header();

            // set the number of peaks to be stored
            header.setNrPeaks(valids.size());

            // create a set for the measurements
            SetInfo set = new SetInfo("", SetInfo.SET);
            header.addSetInfo(set);

            // create the measurement infos
            for (int i = 0; i < options.input.size(); ++i) {
                String file = options.input.get(i);

                // create the measurement info
                MeasurementInfo measurement = new MeasurementInfo(i, data.get(i).header.getMeasurementInfo(0));
                measurement.addFileInfo(new FileInfo(file, file));

                header.addMeasurementInfo(measurement);

                // add the file to the set
                set.addChild(new SetInfo(file, SetInfo.SET, i));
            }

            // write the data
            PeakMLWriter.write(header, (Vector) valids, null,
                    new GZIPOutputStream(new FileOutputStream(options.selection)), null);
        }

        // correct the values with the found function and save them
        for (int i = 0; i < options.input.size(); ++i) {
            Function function = functions[i];
            ParseResult result = data.get(i);

            IPeakSet<MassChromatogram<Peak>> peakset = (IPeakSet<MassChromatogram<Peak>>) result.measurement;
            for (IPeak peak : peakset)
                align(peak, function);

            File filename = new File(options.input.get(i));
            String name = filename.getName();

            PeakMLWriter.write(result.header, (Vector) peakset.getPeaks(), null,
                    new GZIPOutputStream(new FileOutputStream(options.output + "/" + name)), null);
        }
    } catch (Exception e) {
        Tool.unexpectedError(e, application);
    }
}

From source file:br.com.anteros.android.synchronism.communication.AndroidHttpClient.java

/**
 * Compress data to send to server. Creates a Http Entity holding the
 * gzipped data. The data will not be compressed if it is too short.
 * /*from   www  .ja va 2 s .c o m*/
 * @param data
 *            The bytes to compress
 * @return Entity holding the data
 */
public static AbstractHttpEntity getCompressedEntity(byte data[], ContentResolver resolver) throws IOException {
    AbstractHttpEntity entity;
    ByteArrayOutputStream arr = new ByteArrayOutputStream();
    OutputStream zipper = new GZIPOutputStream(arr);
    zipper.write(data);
    zipper.close();
    entity = new ByteArrayEntity(arr.toByteArray());
    entity.setContentEncoding("gzip");
    return entity;
}

From source file:com.krawler.common.util.ByteUtil.java

/**
 * compress the supplied data using GZIPOutputStream and return the
 * compressed data.//  w ww  . j  a  v  a  2 s . c o  m
 * 
 * @param data
 *            data to compress
 * @return compressesd data
 */
public static byte[] compress(byte[] data) throws IOException {
    ByteArrayOutputStream baos = null;
    GZIPOutputStream gos = null;
    try {
        baos = new ByteArrayOutputStream(data.length); // data.length
        // overkill
        gos = new GZIPOutputStream(baos);
        gos.write(data);
        gos.finish();
        return baos.toByteArray();
    } finally {
        if (gos != null) {
            gos.close();
        } else if (baos != null)
            baos.close();
    }
}

From source file:com.att.nsa.mr.client.impl.MRSimplerBatchPublisher.java

private synchronized boolean sendBatch() {
    // it's possible for this call to be made with an empty list. in this case, just return.
    if (fPending.size() < 1) {
        return true;
    }//from   w w w .  j av a2  s  .  com

    final long nowMs = Clock.now();

    host = this.fHostSelector.selectBaseHost();

    final String httpurl = MRConstants.makeUrl(host, fTopic, props.getProperty("Protocol"),
            props.getProperty("partition"));

    try {
        /*final String contentType =
           fCompress ?
              MRFormat.CAMBRIA_ZIP.toString () :
              MRFormat.CAMBRIA.toString () 
        ;*/

        final ByteArrayOutputStream baseStream = new ByteArrayOutputStream();
        OutputStream os = baseStream;
        final String contentType = props.getProperty("contenttype");
        if (contentType.equalsIgnoreCase("application/json")) {
            JSONArray jsonArray = new JSONArray();
            for (TimestampedMessage m : fPending) {
                JSONObject jsonObject = new JSONObject(m.fMsg);

                jsonArray.put(jsonObject);

            }
            os.write(jsonArray.toString().getBytes());
            os.close();

        } else if (contentType.equalsIgnoreCase("text/plain")) {
            for (TimestampedMessage m : fPending) {
                os.write(m.fMsg.getBytes());
                os.write('\n');
            }
            os.close();
        } else if (contentType.equalsIgnoreCase("application/cambria")
                || (contentType.equalsIgnoreCase("application/cambria-zip"))) {
            if (contentType.equalsIgnoreCase("application/cambria-zip")) {
                os = new GZIPOutputStream(baseStream);
            }
            for (TimestampedMessage m : fPending) {

                os.write(("" + m.fPartition.length()).getBytes());
                os.write('.');
                os.write(("" + m.fMsg.length()).getBytes());
                os.write('.');
                os.write(m.fPartition.getBytes());
                os.write(m.fMsg.getBytes());
                os.write('\n');
            }
            os.close();
        } else {
            for (TimestampedMessage m : fPending) {
                os.write(m.fMsg.getBytes());

            }
            os.close();
        }

        final long startMs = Clock.now();
        if (ProtocolTypeConstants.DME2.getValue().equalsIgnoreCase(protocolFlag)) {

            DME2Configue();

            Thread.sleep(5);
            getLog().info("sending " + fPending.size() + " msgs to " + url + subContextPath + ". Oldest: "
                    + (nowMs - fPending.peek().timestamp) + " ms");
            sender.setPayload(os.toString());
            String dmeResponse = sender.sendAndWait(5000L);

            final String logLine = "MR reply ok (" + (Clock.now() - startMs) + " ms):" + dmeResponse.toString();
            getLog().info(logLine);
            fPending.clear();
            return true;
        }

        if (ProtocolTypeConstants.AUTH_KEY.getValue().equalsIgnoreCase(protocolFlag)) {
            getLog().info("sending " + fPending.size() + " msgs to " + httpurl + ". Oldest: "
                    + (nowMs - fPending.peek().timestamp) + " ms");
            final JSONObject result = postAuth(httpurl, baseStream.toByteArray(), contentType, authKey,
                    authDate, username, password, protocolFlag);
            //System.out.println(result.getInt("status"));
            //Here we are checking for error response. If HTTP status
            //code is not within the http success response code
            //then we consider this as error and return false
            if (result.getInt("status") < 200 || result.getInt("status") > 299) {
                return false;
            }
            final String logLine = "MR reply ok (" + (Clock.now() - startMs) + " ms):" + result.toString();
            getLog().info(logLine);
            fPending.clear();
            return true;
        }

        if (ProtocolTypeConstants.AAF_AUTH.getValue().equalsIgnoreCase(protocolFlag)) {
            getLog().info("sending " + fPending.size() + " msgs to " + httpurl + ". Oldest: "
                    + (nowMs - fPending.peek().timestamp) + " ms");
            final JSONObject result = post(httpurl, baseStream.toByteArray(), contentType, username, password,
                    protocolFlag);

            //System.out.println(result.getInt("status"));
            //Here we are checking for error response. If HTTP status
            //code is not within the http success response code
            //then we consider this as error and return false
            if (result.getInt("status") < 200 || result.getInt("status") > 299) {
                return false;
            }
            final String logLine = "MR reply ok (" + (Clock.now() - startMs) + " ms):" + result.toString();
            getLog().info(logLine);
            fPending.clear();
            return true;
        }
    } catch (IllegalArgumentException x) {
        getLog().warn(x.getMessage(), x);
    } catch (IOException x) {
        getLog().warn(x.getMessage(), x);
    } catch (HttpException x) {
        getLog().warn(x.getMessage(), x);
    } catch (Exception x) {
        getLog().warn(x.getMessage(), x);
    }
    return false;
}

From source file:com.edgenius.wiki.service.impl.SitemapServiceImpl.java

private void zipToFile(File sizemapZipFile, byte[] bytes) throws FileNotFoundException, IOException {
    FileOutputStream gzos = new FileOutputStream(sizemapZipFile);
    GZIPOutputStream gzipstream = new GZIPOutputStream(gzos);
    gzipstream.write(bytes);/*from  w  ww  . ja  v  a  2  s .  c  o  m*/
    gzipstream.finish();
    IOUtils.closeQuietly(gzos);
}

From source file:net.sourceforge.fullsync.fs.connection.SyncFileBufferedConnection.java

public void saveToBuffer() throws IOException {
    File fsRoot = fs.getRoot();/*from   w  ww . j a  v a2 s  .  c o  m*/
    File node = fsRoot.getChild(BUFFER_FILENAME);
    if ((null == node) || !node.exists()) {
        node = root.createChild(BUFFER_FILENAME, false);
    }

    try {
        DocumentBuilder docBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
        Document doc = docBuilder.newDocument();

        Element e = doc.createElement(ELEMENT_SYNC_FILES);
        e.appendChild(serializeFile(root, doc));
        doc.appendChild(e);
        TransformerFactory fac = TransformerFactory.newInstance();
        fac.setAttribute("indent-number", 2); //$NON-NLS-1$
        Transformer tf = fac.newTransformer();
        tf.setOutputProperty(OutputKeys.METHOD, "xml"); //$NON-NLS-1$
        tf.setOutputProperty(OutputKeys.VERSION, "1.0"); //$NON-NLS-1$
        tf.setOutputProperty(OutputKeys.INDENT, "yes"); //$NON-NLS-1$
        tf.setOutputProperty(OutputKeys.STANDALONE, "no"); //$NON-NLS-1$
        DOMSource source = new DOMSource(doc);

        try (OutputStreamWriter osw = new OutputStreamWriter(new GZIPOutputStream(node.getOutputStream()),
                StandardCharsets.UTF_8)) {
            tf.transform(source, new StreamResult(osw));
            osw.flush();
        }
    } catch (IOException | ParserConfigurationException | FactoryConfigurationError | TransformerException e) {
        ExceptionHandler.reportException(e);
    }
}

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

public static void writeln(String path, String line) {
    try {//from w w w . j  a  v a 2  s . c om
        OutputStream os = new FileOutputStream(format(path), true);
        if (path.endsWith(".gz")) {
            os = new GZIPOutputStream(os);
        } else if (path.endsWith(".bz2")) {
            os = new BZip2CompressorOutputStream(os);
        }
        try (BufferedWriter out = new BufferedWriter(new OutputStreamWriter(os, "UTF-8"))) {
            out.write(line + "\n");
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:com.runwaysdk.controller.ErrorUtility.java

private static String compress(String message) {
    try {// ww  w .  jav a  2s . c o m
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        BufferedOutputStream bufos = new BufferedOutputStream(new GZIPOutputStream(bos));
        bufos.write(message.getBytes("UTF-8"));
        bufos.close();
        bos.close();

        byte[] bytes = bos.toByteArray();

        return Base64.encodeToString(bytes, false);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:de.dentrassi.build.apt.repo.AptWriter.java

private void compressFile(final File packagesFile) throws IOException {
    this.console.debug("Compressing: " + packagesFile);

    final File compressedFile = new File(packagesFile.getAbsolutePath() + ".gz");
    try (final OutputStream os = new GZIPOutputStream(new FileOutputStream(compressedFile))) {
        try (final InputStream is = new FileInputStream(packagesFile)) {
            IOUtils.copy(is, os);/* w w w  .  j  ava  2 s .  c  om*/
        }
    }
}

From source file:com.netflix.astyanax.AbstractColumnListMutation.java

@Override
public ColumnListMutation<C> putCompressedColumn(C columnName, String value, Integer ttl) {
    Preconditions.checkNotNull(value, "Can't insert null value");

    if (value == null) {
        putEmptyColumn(columnName, ttl);
        return this;
    }/*from  ww  w .j av  a  2s. c o  m*/

    ByteArrayOutputStream out = new ByteArrayOutputStream();
    GZIPOutputStream gzip;
    try {
        gzip = new GZIPOutputStream(out);
        gzip.write(StringUtils.getBytesUtf8(value));
        gzip.close();
        return this.putColumn(columnName, ByteBuffer.wrap(out.toByteArray()), ttl);
    } catch (IOException e) {
        throw new RuntimeException("Error compressing column " + columnName, e);
    }
}