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:io.aino.agents.core.Sender.java

private byte[] getRequestContent() {
    if (!agentConfig.isGzipEnabled()) {
        return stringToSend.getBytes();
    }/*from w  w  w.j a  v a  2  s .  com*/

    try {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        GZIPOutputStream gzipStream = new GZIPOutputStream(baos);
        gzipStream.write(stringToSend.getBytes());

        gzipStream.finish();
        baos.flush();
        byte[] returnBytes = baos.toByteArray();
        baos.close();
        gzipStream.close();

        return returnBytes;
    } catch (IOException e) {
        throw new AgentCoreException("Failed to compress Aino log message using gzip.");
    }
}

From source file:fungus.JungGraphWriter.java

public boolean execute() {
    try {/*from w  w w. java  2  s .c o  m*/
        StringBuffer fn = new StringBuffer();
        Formatter f = new Formatter(fn, Locale.US);
        f.format("%s/graphml-%s-%d-%03d.xml.gz", dir.getPath(), basename, CommonState.seed, CDState.getCycle());
        filename = fn.toString();
        FileOutputStream fos = new FileOutputStream(filename, true);
        GZIPOutputStream gzos = new GZIPOutputStream(fos);

        PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(gzos)));

        writer.save(JungGraphObserver.getGraph(), out);
    } catch (IOException e) {
        log.severe("Couldn't open file " + filename + " for writing");
    }
    return false;
}

From source file:com.ichi2.libanki.sync.BasicHttpSyncer.java

public HttpResponse req(String method, InputStream fobj, int comp, boolean hkey, JSONObject registerData,
        Connection.CancelCallback cancelCallback) {
    File tmpFileBuffer = null;//from   w ww  . j a v a 2 s .c o m
    try {
        String bdry = "--" + BOUNDARY;
        StringWriter buf = new StringWriter();
        // compression flag and session key as post vars
        buf.write(bdry + "\r\n");
        buf.write("Content-Disposition: form-data; name=\"c\"\r\n\r\n" + (comp != 0 ? 1 : 0) + "\r\n");
        if (hkey) {
            buf.write(bdry + "\r\n");
            buf.write("Content-Disposition: form-data; name=\"k\"\r\n\r\n" + mHKey + "\r\n");
        }
        tmpFileBuffer = File.createTempFile("syncer", ".tmp",
                new File(AnkiDroidApp.getCacheStorageDirectory()));
        FileOutputStream fos = new FileOutputStream(tmpFileBuffer);
        BufferedOutputStream bos = new BufferedOutputStream(fos);
        GZIPOutputStream tgt;
        // payload as raw data or json
        if (fobj != null) {
            // header
            buf.write(bdry + "\r\n");
            buf.write(
                    "Content-Disposition: form-data; name=\"data\"; filename=\"data\"\r\nContent-Type: application/octet-stream\r\n\r\n");
            buf.close();
            bos.write(buf.toString().getBytes("UTF-8"));
            // write file into buffer, optionally compressing
            int len;
            BufferedInputStream bfobj = new BufferedInputStream(fobj);
            byte[] chunk = new byte[65536];
            if (comp != 0) {
                tgt = new GZIPOutputStream(bos);
                while ((len = bfobj.read(chunk)) >= 0) {
                    tgt.write(chunk, 0, len);
                }
                tgt.close();
                bos = new BufferedOutputStream(new FileOutputStream(tmpFileBuffer, true));
            } else {
                while ((len = bfobj.read(chunk)) >= 0) {
                    bos.write(chunk, 0, len);
                }
            }
            bos.write(("\r\n" + bdry + "--\r\n").getBytes("UTF-8"));
        } else {
            buf.close();
            bos.write(buf.toString().getBytes("UTF-8"));
        }
        bos.flush();
        bos.close();
        // connection headers
        String url = Collection.SYNC_URL;
        if (method.equals("register")) {
            url = url + "account/signup" + "?username=" + registerData.getString("u") + "&password="
                    + registerData.getString("p");
        } else if (method.startsWith("upgrade")) {
            url = url + method;
        } else {
            url = url + "sync/" + method;
        }
        HttpPost httpPost = new HttpPost(url);
        HttpEntity entity = new ProgressByteEntity(tmpFileBuffer);

        // body
        httpPost.setEntity(entity);
        httpPost.setHeader("Content-type", "multipart/form-data; boundary=" + BOUNDARY);

        // HttpParams
        HttpParams params = new BasicHttpParams();
        params.setParameter(ConnManagerPNames.MAX_TOTAL_CONNECTIONS, 30);
        params.setParameter(ConnManagerPNames.MAX_CONNECTIONS_PER_ROUTE, new ConnPerRouteBean(30));
        params.setParameter(CoreProtocolPNames.USE_EXPECT_CONTINUE, false);
        params.setParameter(CoreProtocolPNames.USER_AGENT, "AnkiDroid-" + AnkiDroidApp.getPkgVersion());
        HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
        HttpConnectionParams.setSoTimeout(params, Connection.CONN_TIMEOUT);

        // Registry
        SchemeRegistry registry = new SchemeRegistry();
        registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
        registry.register(new Scheme("https", new EasySSLSocketFactory(), 443));
        ThreadSafeClientConnManager cm = new ThreadSafeClientConnManager(params, registry);
        if (cancelCallback != null) {
            cancelCallback.setConnectionManager(cm);
        }

        try {
            HttpClient httpClient = new DefaultHttpClient(cm, params);
            return httpClient.execute(httpPost);
        } catch (SSLException e) {
            Log.e(AnkiDroidApp.TAG, "SSLException while building HttpClient", e);
            return null;
        }
    } catch (UnsupportedEncodingException e) {
        throw new RuntimeException(e);
    } catch (IOException e) {
        Log.e(AnkiDroidApp.TAG, "BasicHttpSyncer.sync: IOException", e);
        return null;
    } catch (JSONException e) {
        throw new RuntimeException(e);
    } finally {
        if (tmpFileBuffer != null && tmpFileBuffer.exists()) {
            tmpFileBuffer.delete();
        }
    }
}

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

/**
 * Run the map2text mode.//from w w w  .  j  a va2  s .com
 *
 * @throws java.io.IOException error reading / writing
 */
@Override
public void execute() throws IOException {
    PrintWriter writer = null;
    try {
        writer = new PrintWriter(
                new GZIPOutputStream(new FastBufferedOutputStream(new FileOutputStream(outputFile + ".gz"))));
        writer.write("track type=wiggle_0 name=" + label + " visibility=full viewLimits=1:200\n");
        final AlignmentReaderImpl alignment = new AlignmentReaderImpl(inputBasename);
        alignment.readHeader();
        alignment.close();
        final IndexedIdentifier referenceIds = alignment.getTargetIdentifiers();
        final DoubleIndexedIdentifier backwards = new DoubleIndexedIdentifier(referenceIds);
        final CountsArchiveReader reader = new CountsArchiveReader(inputBasename,
                alternativeCountArchiveExtension);
        final WiggleWindow wiggleWindow = new WiggleWindow(writer, resolution, 0);

        for (int referenceIndex = 0; referenceIndex < reader.getNumberOfIndices(); referenceIndex++) {
            String referenceId = backwards.getId(referenceIndex).toString();
            boolean processThisSequence = true;
            // prepare reference ID for UCSC genome browser.
            if ("MT".equalsIgnoreCase(referenceId)) {
                // patch chromosome name for UCSC genome browser:
                referenceId = "M";
            }

            // ignore c22_H2, c5_H2, and other contigs but not things like chr1 (mm9)
            if (referenceId.startsWith("c") && !referenceId.startsWith("chr")) {
                processThisSequence = false;
            }

            // ignore NT_*
            if (referenceId.startsWith("NT_")) {
                processThisSequence = false;
            }

            if (filterByReferenceNames && !includeReferenceNames.contains(referenceId)) {
                processThisSequence = false;
            }

            if (processThisSequence) {
                // prepend the reference id with "chr" if it doesn't use that already
                final String chromosome;
                if (referenceId.startsWith("chr")) {
                    chromosome = referenceId;
                } else {
                    chromosome = "chr" + referenceId;
                }

                long sumCount = 0;
                int numCounts = 0;

                CountsReader counts = reader.getCountReader(referenceIndex);
                int lastLength = 0;
                int lastPosition = 0;
                while (counts.hasNextTransition()) {
                    counts.nextTransition();
                    lastPosition = counts.getPosition();
                    lastLength = counts.getLength();
                }
                final int maxWritePosition = (lastPosition + lastLength - 1);
                wiggleWindow.reset();
                wiggleWindow.setMaxDataSize(maxWritePosition);

                writer.printf("variableStep chrom=%s span=%d\n", chromosome, resolution);
                counts = reader.getCountReader(referenceIndex);

                while (counts.hasNextTransition()) {
                    counts.nextTransition();
                    final int length = counts.getLength();

                    final int count = counts.getCount();
                    final int position = counts.getPosition();

                    wiggleWindow.addData(position, length, count);

                    sumCount += count;
                    numCounts++;
                }
                wiggleWindow.finish();
                final double averageCount = sumCount / (double) numCounts;
                System.out.println("average count for sequence " + referenceId + " " + averageCount);
            }
        }
    } finally {
        IOUtils.closeQuietly(writer);
    }
}

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

/**
 * Combine tab delimited files and adjusts P-values for multiple testing.
 *
 * @throws java.io.IOException error reading / writing
 *//*from   w  w w.j  av a 2 s .c o  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.
        DifferentialExpressionResults data = new DifferentialExpressionResults(10000000);

        ObjectList<String> columnIdList = vcf ? getVCFColumns(inputFiles) : getTSVColumns(inputFiles);
        // supplement selectedPValueColumns with the columns that match the selection filters:
        ObjectSet<String> selection = new ObjectOpenHashSet<String>();
        selection.addAll(ObjectArrayList.wrap(selectedPValueColumns));
        for (String col : columnIdList) {
            selection.addAll(ObjectArrayList.wrap(selectedPValueColumns));
            for (String filter : columnSelectionFilter) {
                if (col.contains(filter)) {
                    selection.add(col);
                    System.out.printf("Adding column %s to the selected columns.%n", col);

                }
            }
            System.out.println("column: " + col);
        }

        selectedPValueColumns = selection.toArray(new String[selection.size()]);
        DifferentialExpressionCalculator deCalculator = new DifferentialExpressionCalculator();

        if (vcf) {
            loadVCF(inputFiles, data, deCalculator, columnIdList);
        } else {
            loadTSV(inputFiles, data, deCalculator, columnIdList);
        }
        //    data.write(new PrintWriter(System.out), '\t', deCalculator);
        BenjaminiHochbergAdjustment fdr = new BenjaminiHochbergAdjustment();
        fdr.setNumberAboveThreshold(numIgnoredObservations);
        for (String column : selectedPValueColumns) {
            System.out.println("adjusting column: " + column);
            fdr.adjust(data, column.toLowerCase());
        }
        recordTopHits(data);
        for (int i = 0; i < data.getNumberOfStatistics(); i++) {
            MutableString statisticId = data.getStatisticIdForIndex(i);
            final String statId = statisticId.toString();
            if (!contains(selectedPValueColumns, statId)) {
                adjustedColumnIds.add(statId);
            }
        }

        Collections.sort(data, new ElementIndexComparator());
        if (vcf) {
            combineVCF(inputFiles, data, deCalculator, columnIdList, stream);

        } else {
            combineTSV(inputFiles, data, deCalculator, columnIdList, stream);
        }

    } finally {
        if (outputFilename != null) {
            IOUtils.closeQuietly(stream);
        }
    }
}

From source file:eu.delving.sip.files.StorageHelper.java

public static OutputStream zipOut(File file) throws StorageException {
    try {//w  w  w . j  av a 2s  .  c  o  m
        return new GZIPOutputStream(new FileOutputStream(file));
    } catch (IOException e) {
        throw new StorageException(
                String.format("Unable to create output stream from %s", file.getAbsolutePath()), e);
    }
}

From source file:edu.cornell.med.icb.goby.alignments.AlignmentWriterImpl.java

public AlignmentWriterImpl(final String outputBasename) throws IOException {
    alignmentEntries = new FileOutputStream(outputBasename + ".entries");
    headerOutput = new GZIPOutputStream(new FileOutputStream(outputBasename + ".header"));
    statsWriter = new FileWriter(outputBasename + ".stats");
    this.basename = outputBasename;
    collectionBuilder = Alignments.AlignmentCollection.newBuilder();
    entriesChunkWriter = new MessageChunksWriter(alignmentEntries);
    entriesChunkWriter.setParser(new AlignmentCollectionHandler());
    newEntry = Alignments.AlignmentEntry.newBuilder();
    queryIdentifiers = new IndexedIdentifier();
    targetIdentifiers = new IndexedIdentifier();
    stats = new Properties();
    // we assume stats were written until a client puts stats in this writer.
    statsWritten = true;/*from w w w  . j  a  va  2 s.co m*/
    setPermutation(doc.getBoolean("permutate-query-indices"));

}

From source file:com.hbs.common.josn.JSONUtil.java

public static void writeJSONToResponse(HttpServletResponse response, String encoding, boolean wrapWithComments,
        String serializedJSON, boolean smd, boolean gzip, String contentType) throws IOException {
    String json = serializedJSON == null ? "" : serializedJSON;
    if (wrapWithComments) {
        StringBuilder sb = new StringBuilder("/* ");
        sb.append(json);//  w  w  w  .ja  v  a  2  s. c  om
        sb.append(" */");
        json = sb.toString();
    }
    if (log.isDebugEnabled()) {
        log.debug("[JSON]" + json);
    }

    if (contentType == null) {
        contentType = "application/json";
    }
    response.setContentType((smd ? "application/json-rpc;charset=" : contentType + ";charset=") + encoding);
    if (gzip) {
        response.addHeader("Content-Encoding", "gzip");
        GZIPOutputStream out = null;
        InputStream in = null;
        try {
            out = new GZIPOutputStream(response.getOutputStream());
            in = new ByteArrayInputStream(json.getBytes());
            byte[] buf = new byte[1024];
            int len;
            while ((len = in.read(buf)) > 0) {
                out.write(buf, 0, len);
            }
        } finally {
            if (in != null)
                in.close();
            if (out != null) {
                out.finish();
                out.close();
            }
        }

    } else {
        response.setContentLength(json.getBytes(encoding).length);
        PrintWriter out = response.getWriter();
        out.print(json);
    }
}

From source file:com.codemarvels.ant.aptrepotask.AptRepoTask.java

public void execute() {
    if (repoDir == null) {
        log("repoDir attribute is empty !", LogLevel.ERR.getLevel());
        throw new RuntimeException("Bad attributes for apt-repo task");
    }/*from   www.j  a v  a 2  s . co m*/
    log("repo dir: " + repoDir);
    File repoFolder = new File(repoDir);
    if (!repoFolder.exists()) {
        repoFolder.mkdirs();
    }
    File[] files = repoFolder.listFiles(new FileFilter() {
        public boolean accept(File pathname) {
            if (pathname.getName().endsWith(FILE_DEB_EXT)) {
                return true;
            }
            return false;
        }
    });
    Packages packages = new Packages();
    for (int i = 0; i < files.length; i++) {
        File file = files[i];
        PackageEntry packageEntry = new PackageEntry();
        packageEntry.setSize(file.length());
        packageEntry.setSha1(Utils.getDigest("SHA-1", file));
        packageEntry.setSha256(Utils.getDigest("SHA-256", file));
        packageEntry.setMd5sum(Utils.getDigest("MD5", file));
        String fileName = file.getName();
        packageEntry.setFilename(fileName);
        log("found deb: " + fileName);
        try {
            ArchiveInputStream control_tgz;
            ArArchiveEntry entry;
            TarArchiveEntry control_entry;
            ArchiveInputStream debStream = new ArchiveStreamFactory().createArchiveInputStream("ar",
                    new FileInputStream(file));
            while ((entry = (ArArchiveEntry) debStream.getNextEntry()) != null) {
                if (entry.getName().equals("control.tar.gz")) {
                    ControlHandler controlHandler = new ControlHandler();
                    GZIPInputStream gzipInputStream = new GZIPInputStream(debStream);
                    control_tgz = new ArchiveStreamFactory().createArchiveInputStream("tar", gzipInputStream);
                    while ((control_entry = (TarArchiveEntry) control_tgz.getNextEntry()) != null) {
                        log("control entry: " + control_entry.getName(), LogLevel.DEBUG.getLevel());
                        if (control_entry.getName().trim().equals(CONTROL_FILE_NAME)) {
                            ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
                            IOUtils.copy(control_tgz, outputStream);
                            String content_string = outputStream.toString("UTF-8");
                            outputStream.close();
                            controlHandler.setControlContent(content_string);
                            log("control cont: " + outputStream.toString("utf-8"), LogLevel.DEBUG.getLevel());
                            break;
                        }
                    }
                    control_tgz.close();
                    if (controlHandler.hasControlContent()) {
                        controlHandler.handle(packageEntry);
                    } else {
                        throw new RuntimeException("no control content found for: " + file.getName());
                    }
                    break;
                }
            }
            debStream.close();
            packages.addPackageEntry(packageEntry);
        } catch (Exception e) {
            String msg = FAILED_TO_CREATE_APT_REPO + " " + file.getName();
            log(msg, e, LogLevel.ERR.getLevel());
            throw new RuntimeException(msg, e);
        }
    }
    try {
        File packagesFile = new File(repoDir, PACKAGES_GZ);
        packagesWriter = new BufferedWriter(
                new OutputStreamWriter(new GZIPOutputStream(new FileOutputStream(packagesFile))));
        packagesWriter.write(packages.toString());
        DefaultHashes hashes = Utils.getDefaultDigests(packagesFile);
        ReleaseInfo pinfo = new ReleaseInfo(PACKAGES_GZ, packagesFile.length(), hashes);
        Release release = new Release();
        release.addInfo(pinfo);
        final File releaseFile = new File(repoDir, RELEASE);
        FileUtils.fileWrite(releaseFile, release.toString());
    } catch (IOException e) {
        throw new RuntimeException("writing files failed", e);
    } finally {
        if (packagesWriter != null) {
            try {
                packagesWriter.close();
            } catch (IOException e) {
                throw new RuntimeException("writing files failed", e);
            }
        }
    }
}

From source file:hudson.model.UsageStatistics.java

/**
 * Gets the encrypted usage stat data to be sent to the Hudson server.
 *///from w  ww.  ja  v  a2 s .  com
public String getStatData() throws IOException {
    Hudson h = Hudson.getInstance();

    JSONObject o = new JSONObject();
    o.put("stat", 1);
    o.put("install", Util.getDigestOf(h.getSecretKey()));
    o.put("version", Hudson.VERSION);

    List<JSONObject> nodes = new ArrayList<JSONObject>();
    for (Computer c : h.getComputers()) {
        JSONObject n = new JSONObject();
        if (c.getNode() == h) {
            n.put("master", true);
            n.put("jvm-vendor", System.getProperty("java.vm.vendor"));
            n.put("jvm-version", System.getProperty("java.version"));
        }
        n.put("executors", c.getNumExecutors());
        DescriptorImpl descriptor = h.getDescriptorByType(DescriptorImpl.class);
        n.put("os", descriptor.get(c));
        nodes.add(n);
    }
    o.put("nodes", nodes);

    List<JSONObject> plugins = new ArrayList<JSONObject>();
    for (PluginWrapper pw : h.getPluginManager().getPlugins()) {
        if (!pw.isActive())
            continue; // treat disabled plugins as if they are uninstalled
        JSONObject p = new JSONObject();
        p.put("name", pw.getShortName());
        p.put("version", pw.getVersion());
        plugins.add(p);
    }
    o.put("plugins", plugins);

    JSONObject jobs = new JSONObject();
    List<TopLevelItem> items = h.getItems();
    for (TopLevelItemDescriptor d : Items.all()) {
        int cnt = 0;
        for (TopLevelItem item : items) {
            if (item.getDescriptor() == d)
                cnt++;
        }
        jobs.put(d.getJsonSafeClassName(), cnt);
    }
    o.put("jobs", jobs);

    try {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();

        // json -> UTF-8 encode -> gzip -> encrypt -> base64 -> string
        OutputStreamWriter w = new OutputStreamWriter(
                new GZIPOutputStream(new CombinedCipherOutputStream(baos, getCipher(), "AES")), "UTF-8");
        o.write(w);
        w.close();

        return new String(Base64.encode(baos.toByteArray()));
    } catch (GeneralSecurityException e) {
        throw new Error(e); // impossible
    }
}