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.dumontierlab.pdb2rdf.Pdb2Rdf.java

private static void printRdf(final CommandLine cmd, final Map<String, Double> stats) {
    final File outDir = getOutputDirectory(cmd);
    final RDFWriter writer = getWriter(cmd);
    final ProgressMonitor monitor = getProgressMonitor();
    Pdb2RdfInputIterator i = processInput(cmd);
    final int inputSize = i.size();
    final AtomicInteger progressCount = new AtomicInteger();
    ExecutorService pool = null;//from w  w w .j  a  va 2 s. com
    if (outDir != null) {
        pool = getThreadPool(cmd);
    } else {
        // if output is going to the STDOUT then we need to do process in
        // sequential mode.
        pool = Executors.newSingleThreadExecutor();
    }

    final Object lock = new Object();

    while (i.hasNext()) {
        final InputSource input = i.next();
        pool.execute(new Runnable() {
            @Override
            public void run() {
                OutputStream out = System.out;
                PdbXmlParser parser = new PdbXmlParser();
                PdbRdfModel model = null;
                try {
                    if (cmd.hasOption("detailLevel")) {
                        try {
                            DetailLevel detailLevel = Enum.valueOf(DetailLevel.class,
                                    cmd.getOptionValue("detailLevel"));
                            model = parser.parse(input, new PdbRdfModel(), detailLevel);
                        } catch (IllegalArgumentException e) {
                            LOG.fatal("Invalid argument value for detailLevel option", e);
                            System.exit(1);
                        }
                    } else {
                        model = parser.parse(input, new PdbRdfModel());
                    }
                    // add the input file information
                    model.addInputFileInformation();
                    // add the outputFile information();
                    model.addRDFFileInformation();
                    if (outDir != null) {
                        File directory = new File(outDir, model.getPdbId().substring(1, 3));
                        synchronized (lock) {
                            if (!directory.exists()) {
                                directory.mkdir();
                            }
                        }
                        File file = new File(directory, model.getPdbId() + ".rdf.gz");
                        out = new GZIPOutputStream(new FileOutputStream(file));
                    }
                    if (cmd.hasOption("format")) {
                        if (cmd.getOptionValue("format").equalsIgnoreCase("NQUADs")) {
                            Dataset ds = TDBFactory.createDataset();
                            ds.addNamedModel(model.getDatasetResource().toString(), model);
                            StringWriter sw = new StringWriter();
                            RDFDataMgr.write(sw, ds, Lang.NQUADS);

                            out.write(sw.toString().getBytes(Charset.forName("UTF-8")));
                            ds.close();

                        }
                    }

                    writer.write(model, out, null);

                    if (stats != null) {
                        updateStats(stats, model);
                    }
                    if (monitor != null) {
                        monitor.setProgress(progressCount.incrementAndGet(), inputSize);
                    }

                } catch (Exception e) {
                    String id = null;
                    if (model != null) {
                        id = model.getPdbId();
                    }
                    LOG.error("Unable to parse input for PDB: " + id, e);
                } finally {
                    try {
                        out.close();
                    } catch (IOException e) {
                        LOG.error("Unable to close output stream", e);
                    }
                }
            }
        });
    }
    pool.shutdown();
    while (!pool.isTerminated()) {
        try {
            pool.awaitTermination(1, TimeUnit.SECONDS);
        } catch (InterruptedException e) {
            break;
        }
    }
}

From source file:org.runnerup.export.EndomondoSynchronizer.java

@Override
public Status upload(SQLiteDatabase db, long mID) {
    Status s;//from   w ww  . j  a  va2  s .com
    if ((s = connect()) != Status.OK) {
        return s;
    }

    EndomondoTrack tcx = new EndomondoTrack(db);
    HttpURLConnection conn = null;
    Exception ex = null;
    try {
        EndomondoTrack.Summary summary = new EndomondoTrack.Summary();
        StringWriter writer = new StringWriter();
        tcx.export(mID, writer, summary);

        String workoutId = deviceId + "-" + Long.toString(mID);
        Log.e(getName(), "workoutId: " + workoutId);

        StringBuilder url = new StringBuilder();
        url.append(UPLOAD_URL).append("?authToken=").append(authToken);
        url.append("&workoutId=").append(workoutId);
        url.append("&sport=").append(summary.sport);
        url.append("&duration=").append(summary.duration);
        url.append("&distance=").append(summary.distance);
        if (summary.hr != null) {
            url.append("&heartRateAvg=").append(summary.hr.toString());
        }
        url.append("&gzip=true");
        url.append("&extendedResponse=true");

        conn = (HttpURLConnection) new URL(url.toString()).openConnection();
        conn.setDoOutput(true);
        conn.setRequestMethod(RequestMethod.POST.name());
        conn.addRequestProperty("Content-Type", "application/octet-stream");
        OutputStream out = new GZIPOutputStream(new BufferedOutputStream(conn.getOutputStream()));
        out.write(writer.getBuffer().toString().getBytes());
        out.flush();
        out.close();

        BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
        JSONObject res = parseKVP(in);
        conn.disconnect();

        Log.e(getName(), "res: " + res.toString());

        int responseCode = conn.getResponseCode();
        String amsg = conn.getResponseMessage();
        if (responseCode == HttpStatus.SC_OK && "OK".contentEquals(res.getString("_0"))) {
            s.activityId = mID;
            return s;
        }
        ex = new Exception(amsg);
    } catch (IOException e) {
        ex = e;
    } catch (JSONException e) {
        ex = e;
    }

    s = Synchronizer.Status.ERROR;
    s.ex = ex;
    if (ex != null) {
        ex.printStackTrace();
    }
    return s;
}

From source file:com.github.jasonruckman.gzip.AbstractBenchmark.java

private void initializeJackson() {
    try {/*from ww  w.j a v a  2s . co  m*/
        ObjectMapper mapper = new ObjectMapper();
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        GZIPOutputStream gzos = new GZIPOutputStream(baos);
        mapper.writeValue(gzos, sampleData);
        gzos.close();
        serializedJacksonData = baos.toByteArray();
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:org.wso2.carbon.automation.extensions.servers.httpserver.SimpleHttpClient.java

/**
 * Send a HTTP PUT request to the specified URL
 *
 * @param url         Target endpoint URL
 * @param headers     Any HTTP headers that should be added to the request
 * @param payload     Content payload that should be sent
 * @param contentType Content-type of the request
 * @return Returned HTTP response/*  w ww .  j  a va2  s  .  c  o  m*/
 * @throws IOException If an error occurs while making the invocation
 */
public HttpResponse doPut(String url, final Map<String, String> headers, final String payload,
        String contentType) throws IOException {
    HttpUriRequest request = new HttpPut(url);
    setHeaders(headers, request);
    HttpEntityEnclosingRequest entityEncReq = (HttpEntityEnclosingRequest) request;
    final boolean zip = headers != null && "gzip".equals(headers.get(HttpHeaders.CONTENT_ENCODING));

    EntityTemplate ent = new EntityTemplate(new ContentProducer() {
        public void writeTo(OutputStream outputStream) throws IOException {
            OutputStream out = outputStream;
            if (zip) {
                out = new GZIPOutputStream(outputStream);
            }
            out.write(payload.getBytes(Charset.defaultCharset()));
            out.flush();
            out.close();
        }
    });
    ent.setContentType(contentType);
    if (zip) {
        ent.setContentEncoding("gzip");
    }
    entityEncReq.setEntity(ent);
    return client.execute(request);
}

From source file:de.tudarmstadt.lt.ltbot.writer.PlainTextDocumentWriter.java

protected PrintStream openPrintToFileStream(File outputfile) throws IOException {
    OutputStream os = new FileOutputStream(outputfile, true);
    if (getFilenameFormat().endsWith(".gz")) {
        os = new GZIPOutputStream(os); //{{ def.setLevel(Deflater.BEST_COMPRESSION); }};
    }/*from  ww  w  . j a  v  a  2 s .com*/
    PrintStream p = new PrintStream(os);
    p.flush();
    return p;
}

From source file:com.basistech.rosette.dm.json.array.CompareJsons.java

private static byte[] gzipCompress(byte[] data) {
    ByteArrayOutputStream sink = new ByteArrayOutputStream();
    try {//from   w  ww  .j av  a2s .c o  m
        GZIPOutputStream compressedStream = new GZIPOutputStream(sink);
        ByteStreams.copy(new ByteArrayInputStream(data), compressedStream);
        compressedStream.close();
        return sink.toByteArray();
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:com.meltmedia.cadmium.servlets.BasicFileServlet.java

public void processRequest(FileRequestContext context) throws ServletException, IOException {
    try {//  w w w. ja v a 2s.c  o  m
        // Find the file to serve in the file system.  This may redirect for welcome files or send 404 responses.
        if (locateFileToServe(context))
            return;

        // Handle any conditional headers that may be present.
        if (handleConditions(context))
            return;

        // Sets the content type header.
        resolveContentType(context);

        // Sets compress if Accept-Encoding allows for gzip or identity
        if (checkAccepts(context))
            return;

        try {
            if (context.file != null) {
                context.response.setHeader(CONTENT_DISPOSITION_HEADER,
                        "inline;filename=\"" + context.file.getName() + "\"");
            }
            context.response.setHeader(ACCEPT_RANGES_HEADER, "bytes");
            if (context.eTag != null) {
                context.response.setHeader(ETAG_HEADER, context.eTag);
            }
            context.response.setDateHeader(LAST_MODIFIED_HEADER, lastUpdated);
            if (!context.ranges.isEmpty()) {
                context.response.setStatus(HttpServletResponse.SC_PARTIAL_CONTENT);
                String rangeBoundary = RANGE_BOUNDARY + UUID.randomUUID().toString();
                if (context.ranges.size() > 1) {
                    context.response.setContentType("multipart/byteranges; boundary=" + rangeBoundary);
                    context.response.setStatus(HttpServletResponse.SC_PARTIAL_CONTENT);

                    if (context.sendEntity) {
                        context.in = new FileInputStream(context.file);
                        context.out = context.response.getOutputStream();
                        ServletOutputStream sout = (ServletOutputStream) context.out;
                        for (Range r : context.ranges) {
                            sout.println();
                            sout.println("--" + rangeBoundary);
                            sout.println("Content-Type: " + context.contentType);
                            sout.println("Context-Range: bytes " + r.start + "-" + r.end + "/"
                                    + context.file.length());

                            copyPartialContent(context.in, context.out, r);
                        }

                        sout.println();
                        sout.println("--" + rangeBoundary + "--");
                    }
                } else {
                    Range r = context.ranges.get(0);
                    context.response.setContentType(context.contentType);
                    Long rangeLength = calculateRangeLength(context, r);
                    context.response.setHeader(CONTENT_RANGE_HEADER,
                            "bytes " + r.start + "-" + r.end + "/" + context.file.length());
                    if (context.sendEntity) {
                        context.in = new FileInputStream(context.file);
                        context.out = context.response.getOutputStream();

                        context.response.setHeader(CONTENT_LENGTH_HEADER, rangeLength.toString());

                        copyPartialContent(context.in, context.out, r);
                    }
                }
            } else {
                context.response.setStatus(HttpServletResponse.SC_OK);
                context.response.setContentType(context.contentType);

                if (context.sendEntity) {
                    context.response.setHeader(CONTENT_RANGE_HEADER,
                            "bytes 0-" + context.file.length() + "/" + context.file.length());
                    context.in = new FileInputStream(context.file);
                    context.out = context.response.getOutputStream();
                    if (context.compress) {

                        context.response.setHeader(CONTENT_ENCODING_HEADER, "gzip");
                        context.out = new GZIPOutputStream(context.out);

                    } else
                        context.response.setHeader(CONTENT_LENGTH_HEADER,
                                new Long(context.file.length()).toString());

                    IOUtils.copy(context.in, context.out);
                }
            }
        } finally {
            IOUtils.closeQuietly(context.in);
            IOUtils.closeQuietly(context.out);
        }
    } catch (IOException ioe) {
        logger.error("Received an IOException serving request: " + context.path, ioe);
        throw ioe;
    } catch (Throwable t) {
        logger.error("Failed to serve request: " + context.path, t);
        throw new ServletException(t);
    }
}

From source file:co.cask.cdap.internal.app.runtime.LocalizationUtilsTest.java

private File createTgzFile(String tgzFileName, File... filesToAdd) throws IOException {
    File target = TEMP_FOLDER.newFile(tgzFileName + ".tgz");
    try (TarArchiveOutputStream tos = new TarArchiveOutputStream(
            new GZIPOutputStream(new BufferedOutputStream(new FileOutputStream(target))))) {
        addFilesToTar(tos, filesToAdd);//from   w  w  w . j  a  v  a  2 s  .  c  o  m
    }
    return target;
}

From source file:com.googlecode.jgenhtml.JGenHtmlUtils.java

public static void transformToFile(final File targetFile, final boolean asXml, final Document doc)
        throws TransformerConfigurationException, TransformerException, IOException {
    Transformer transformer;//from  w w w.  j  a  v  a  2 s.co m
    Config config = CoverageReport.getConfig();
    if (asXml) {
        transformer = getTransformer(null);
    } else {
        transformer = getTransformer('/' + JGenHtmlUtils.XSLT_NAME);
        transformer.setParameter("ext", config.getHtmlExt());
        File cssFile = config.getCssFile();
        if (cssFile != null) {
            transformer.setParameter("cssName", cssFile.getName());
        }
    }
    DOMSource src = new DOMSource(doc);
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    StreamResult res;
    if (config.isGzip()) {
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        GZIPOutputStream gzos = new GZIPOutputStream(new FileOutputStream(targetFile));
        res = new StreamResult(bos);
        transformer.transform(src, res);
        IOUtils.write(bos.toByteArray(), gzos);
        IOUtils.closeQuietly(gzos);
    } else {
        res = new StreamResult(targetFile);
        transformer.transform(src, res);
    }
}

From source file:cc.creativecomputing.io.CCIOUtil.java

/**
 * decode a gzip output stream/*from   ww w. j  a va2  s  .  c  om*/
 */
static public OutputStream gzipOutput(OutputStream output) {
    try {
        return new GZIPOutputStream(output);
    } catch (IOException e) {
        e.printStackTrace();
        throw new RuntimeException("Problem with gzip output");
    }
    //return null;
}