Example usage for java.util.zip GZIPOutputStream close

List of usage examples for java.util.zip GZIPOutputStream close

Introduction

In this page you can find the example usage for java.util.zip GZIPOutputStream close.

Prototype

public void close() throws IOException 

Source Link

Document

Writes remaining compressed data to the output stream and closes the underlying stream.

Usage

From source file:org.rhq.enterprise.server.plugins.rhnhosted.RHNHelper.java

private byte[] gzip(byte[] input) throws IOException {

    ByteArrayOutputStream zipped = new ByteArrayOutputStream();
    GZIPOutputStream gzip = new GZIPOutputStream(zipped);
    try {//from  w w  w  .  j a  va 2s.  c om
        gzip.write(input);
    } finally {
        gzip.close();
    }
    return zipped.toByteArray();
}

From source file:net.nifheim.beelzebu.coins.common.utils.FileManager.java

private void gzipFile(InputStream in, String to) throws IOException {
    GZIPOutputStream out = new GZIPOutputStream(new FileOutputStream(to));
    byte[] buffer = new byte[4096];
    int bytesRead;
    while ((bytesRead = in.read(buffer)) != -1) {
        out.write(buffer, 0, bytesRead);
    }/*from   w w w.j av a 2  s.co  m*/
    in.close();
    out.close();
}

From source file:eu.domibus.ebms3.common.CompressionService.java

/**
 * Compress given Stream via GZIP [RFC1952].
 *
 * @param sourceStream Stream of uncompressed data
 * @param targetStream Stream of compressed data
 * @throws IOException//from w w w . j  ava2s. com
 */
private void compress(final InputStream sourceStream, final GZIPOutputStream targetStream) throws IOException {
    final byte[] buffer = new byte[1024];

    try {
        int i;
        while ((i = sourceStream.read(buffer)) > 0) {
            targetStream.write(buffer, 0, i);
        }

        sourceStream.close();

        targetStream.finish();
        targetStream.close();

    } catch (IOException e) {
        CompressionService.LOG.error(
                "I/O exception during gzip compression. method: compress(Inputstream, GZIPOutputStream)", e);
        throw e;
    }
}

From source file:org.pentaho.di.www.GetTransStatusServlet.java

protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    if (isJettyMode() && !request.getContextPath().startsWith(CONTEXT_PATH)) {
        return;/*from w ww.  j ava2 s.com*/
    }

    if (log.isDebug())
        logDebug(BaseMessages.getString(PKG, "TransStatusServlet.Log.TransStatusRequested"));

    String transName = request.getParameter("name");
    String id = request.getParameter("id");
    boolean useXML = "Y".equalsIgnoreCase(request.getParameter("xml"));
    int startLineNr = Const.toInt(request.getParameter("from"), 0);

    response.setStatus(HttpServletResponse.SC_OK);

    if (useXML) {
        response.setContentType("text/xml");
        response.setCharacterEncoding(Const.XML_ENCODING);
    } else {
        response.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");
    }

    PrintWriter out = response.getWriter();

    // ID is optional...
    //
    Trans trans;
    CarteObjectEntry entry;
    if (Const.isEmpty(id)) {
        // get the first transformation that matches...
        //
        entry = getTransformationMap().getFirstCarteObjectEntry(transName);
        if (entry == null) {
            trans = null;
        } else {
            id = entry.getId();
            trans = getTransformationMap().getTransformation(entry);
        }
    } else {
        // Take the ID into account!
        //
        entry = new CarteObjectEntry(transName, id);
        trans = getTransformationMap().getTransformation(entry);
    }

    if (trans != null) {
        String status = trans.getStatus();
        int lastLineNr = CentralLogStore.getLastBufferLineNr();
        String logText = CentralLogStore.getAppender()
                .getBuffer(trans.getLogChannel().getLogChannelId(), false, startLineNr, lastLineNr).toString();

        if (useXML) {
            response.setContentType("text/xml");
            response.setCharacterEncoding(Const.XML_ENCODING);
            out.print(XMLHandler.getXMLHeader(Const.XML_ENCODING));

            SlaveServerTransStatus transStatus = new SlaveServerTransStatus(transName, entry.getId(), status);
            transStatus.setFirstLoggingLineNr(startLineNr);
            transStatus.setLastLoggingLineNr(lastLineNr);

            for (int i = 0; i < trans.nrSteps(); i++) {
                StepInterface baseStep = trans.getRunThread(i);
                if ((baseStep.isRunning()) || baseStep.getStatus() != StepExecutionStatus.STATUS_EMPTY) {
                    StepStatus stepStatus = new StepStatus(baseStep);
                    transStatus.getStepStatusList().add(stepStatus);
                }
            }

            // The log can be quite large at times, we are going to put a base64 encoding around a compressed stream
            // of bytes to handle this one.

            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            GZIPOutputStream gzos = new GZIPOutputStream(baos);
            gzos.write(logText.getBytes());
            gzos.close();

            String loggingString = new String(Base64.encodeBase64(baos.toByteArray()));
            transStatus.setLoggingString(loggingString);

            // Also set the result object...
            //
            transStatus.setResult(trans.getResult());

            // Is the transformation paused?
            //
            transStatus.setPaused(trans.isPaused());

            // Send the result back as XML
            //
            try {
                out.println(transStatus.getXML());
            } catch (KettleException e) {
                throw new ServletException("Unable to get the transformation status in XML format", e);
            }
        } else {
            response.setContentType("text/html;charset=UTF-8");

            out.println("<HTML>");
            out.println("<HEAD>");
            out.println("<TITLE>" + BaseMessages.getString(PKG, "TransStatusServlet.KettleTransStatus")
                    + "</TITLE>");
            out.println("<META http-equiv=\"Refresh\" content=\"10;url=" + convertContextPath(CONTEXT_PATH)
                    + "?name=" + URLEncoder.encode(transName, "UTF-8") + "&id=" + id + "\">");
            out.println("<META http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">");
            out.println("</HEAD>");
            out.println("<BODY>");
            out.println("<H1>" + BaseMessages.getString(PKG, "TransStatusServlet.TopTransStatus", transName)
                    + "</H1>");

            try {
                out.println("<table border=\"1\">");
                out.print("<tr> <th>" + BaseMessages.getString(PKG, "TransStatusServlet.TransName")
                        + "</th> <th>" + BaseMessages.getString(PKG, "TransStatusServlet.CarteObjectId")
                        + "</th> <th>" + BaseMessages.getString(PKG, "TransStatusServlet.TransStatus")
                        + "</th> </tr>");

                out.print("<tr>");
                out.print("<td>" + transName + "</td>");
                out.print("<td>" + id + "</td>");
                out.print("<td>" + status + "</td>");
                out.print("</tr>");
                out.print("</table>");

                out.print("<p>");

                if ((trans.isFinished() && trans.isRunning())
                        || (!trans.isRunning() && !trans.isPreparing() && !trans.isInitializing())) {
                    out.print("<a href=\"" + convertContextPath(StartTransServlet.CONTEXT_PATH) + "?name="
                            + URLEncoder.encode(transName, "UTF-8") + "&id=" + id + "\">"
                            + BaseMessages.getString(PKG, "TransStatusServlet.StartTrans") + "</a>");
                    out.print("<p>");
                    out.print("<a href=\"" + convertContextPath(PrepareExecutionTransServlet.CONTEXT_PATH)
                            + "?name=" + URLEncoder.encode(transName, "UTF-8") + "&id=" + id + "\">"
                            + BaseMessages.getString(PKG, "TransStatusServlet.PrepareTrans") + "</a><br>");
                } else if (trans.isRunning()) {
                    out.print("<a href=\"" + convertContextPath(PauseTransServlet.CONTEXT_PATH) + "?name="
                            + URLEncoder.encode(transName, "UTF-8") + "&id=" + id + "\">"
                            + BaseMessages.getString(PKG, "PauseStatusServlet.PauseResumeTrans") + "</a><br>");
                    out.print("<a href=\"" + convertContextPath(StopTransServlet.CONTEXT_PATH) + "?name="
                            + URLEncoder.encode(transName, "UTF-8") + "&id=" + id + "\">"
                            + BaseMessages.getString(PKG, "TransStatusServlet.StopTrans") + "</a>");
                    out.print("<p>");
                }
                out.print("<a href=\"" + convertContextPath(CleanupTransServlet.CONTEXT_PATH) + "?name="
                        + URLEncoder.encode(transName, "UTF-8") + "&id=" + id + "\">"
                        + BaseMessages.getString(PKG, "TransStatusServlet.CleanupTrans") + "</a>");
                out.print("<p>");

                out.println("<table border=\"1\">");
                out.print("<tr> <th>" + BaseMessages.getString(PKG, "TransStatusServlet.Stepname")
                        + "</th> <th>" + BaseMessages.getString(PKG, "TransStatusServlet.CopyNr") + "</th> <th>"
                        + BaseMessages.getString(PKG, "TransStatusServlet.Read") + "</th> <th>"
                        + BaseMessages.getString(PKG, "TransStatusServlet.Written") + "</th> <th>"
                        + BaseMessages.getString(PKG, "TransStatusServlet.Input") + "</th> <th>"
                        + BaseMessages.getString(PKG, "TransStatusServlet.Output") + "</th> " + "<th>"
                        + BaseMessages.getString(PKG, "TransStatusServlet.Updated") + "</th> <th>"
                        + BaseMessages.getString(PKG, "TransStatusServlet.Rejected") + "</th> <th>"
                        + BaseMessages.getString(PKG, "TransStatusServlet.Errors") + "</th> <th>"
                        + BaseMessages.getString(PKG, "TransStatusServlet.Active") + "</th> <th>"
                        + BaseMessages.getString(PKG, "TransStatusServlet.Time") + "</th> " + "<th>"
                        + BaseMessages.getString(PKG, "TransStatusServlet.Speed") + "</th> <th>"
                        + BaseMessages.getString(PKG, "TransStatusServlet.prinout") + "</th> </tr>");

                for (int i = 0; i < trans.nrSteps(); i++) {
                    StepInterface step = trans.getRunThread(i);
                    if ((step.isRunning()) || step.getStatus() != StepExecutionStatus.STATUS_EMPTY) {
                        StepStatus stepStatus = new StepStatus(step);

                        if (step.isRunning() && !step.isStopped() && !step.isPaused()) {
                            String sniffLink = " <a href=\"" + convertContextPath(SniffStepServlet.CONTEXT_PATH)
                                    + "?trans=" + URLEncoder.encode(transName, "UTF-8") + "&id=" + id
                                    + "&lines=50" + "&copynr=" + step.getCopy() + "&type="
                                    + SniffStepServlet.TYPE_OUTPUT + "&step="
                                    + URLEncoder.encode(step.getStepname(), "UTF-8") + "\">"
                                    + stepStatus.getStepname() + "</a>";
                            stepStatus.setStepname(sniffLink);
                        }

                        out.print(stepStatus.getHTMLTableRow());
                    }
                }
                out.println("</table>");
                out.println("<p>");

                out.print("<a href=\"" + convertContextPath(GetTransStatusServlet.CONTEXT_PATH) + "?name="
                        + URLEncoder.encode(transName, "UTF-8") + "&id=" + id + "&xml=y\">"
                        + BaseMessages.getString(PKG, "TransStatusServlet.ShowAsXml") + "</a><br>");
                out.print("<a href=\"" + convertContextPath(GetStatusServlet.CONTEXT_PATH) + "\">"
                        + BaseMessages.getString(PKG, "TransStatusServlet.BackToStatusPage") + "</a><br>");
                out.print("<p><a href=\"" + convertContextPath(GetTransStatusServlet.CONTEXT_PATH) + "?name="
                        + URLEncoder.encode(transName, "UTF-8") + "&id=" + id + "\">"
                        + BaseMessages.getString(PKG, "TransStatusServlet.Refresh") + "</a>");

                // Put the logging below that.

                out.println("<p>");
                out.println(
                        "<textarea id=\"translog\" cols=\"120\" rows=\"20\" wrap=\"off\" name=\"Transformation log\" readonly=\"readonly\">"
                                + logText + "</textarea>");

                out.println("<script type=\"text/javascript\"> ");
                out.println("  translog.scrollTop=translog.scrollHeight; ");
                out.println("</script> ");
                out.println("<p>");
            } catch (Exception ex) {
                out.println("<p>");
                out.println("<pre>");
                ex.printStackTrace(out);
                out.println("</pre>");
            }

            out.println("<p>");
            out.println("</BODY>");
            out.println("</HTML>");
        }
    } else {
        if (useXML) {
            out.println(new WebResult(WebResult.STRING_ERROR,
                    BaseMessages.getString(PKG, "TransStatusServlet.Log.CoundNotFindSpecTrans", transName)));
        } else {
            out.println(
                    "<H1>" + BaseMessages.getString(PKG, "TransStatusServlet.Log.CoundNotFindTrans", transName)
                            + "</H1>");
            out.println("<a href=\"" + convertContextPath(GetStatusServlet.CONTEXT_PATH) + "\">"
                    + BaseMessages.getString(PKG, "TransStatusServlet.BackToStatusPage") + "</a><p>");
        }
    }
}

From source file:com.nokia.helium.core.EmailDataSender.java

/**
 * GZipping a string./*from w w w  .j a v a 2  s  .c  om*/
 * 
 * @param data the content to be gzipped.
 * @param filename the name for the file.
 * @return a ByteArrayDataSource.
 */
protected ByteArrayDataSource compressFile(File fileName) throws IOException {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    GZIPOutputStream gz = new GZIPOutputStream(out);
    BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream(fileName));
    byte[] dataBuffer = new byte[512];
    while ((bufferedInputStream.read(dataBuffer)) != -1) {
        gz.write(dataBuffer);
    }
    gz.close();
    bufferedInputStream.close();
    ByteArrayDataSource dataSrc = new ByteArrayDataSource(out.toByteArray(), "application/x-gzip");
    return dataSrc;
}

From source file:net.sf.ehcache.constructs.web.PageInfoTest.java

/**
 * Create gzip file in tmp//from   w  ww  .  j  a  v a2  s  . co m
 *
 * @throws Exception
 */
protected void setUp() throws Exception {
    String testGzipFile = System.getProperty("java.io.tmpdir") + File.separator + "test.gzip";
    testFile = new File(testGzipFile);
    FileOutputStream fout = new FileOutputStream(testFile);
    GZIPOutputStream gzipOutputStream = new GZIPOutputStream(fout);
    for (int j = 0; j < 100; j++) {
        for (int i = 0; i < 1000; i++) {
            gzipOutputStream.write(i);
        }
    }
    gzipOutputStream.close();
}

From source file:org.apache.ojb.broker.Identity.java

/**
 * Return the serialized form of this Identity.
 * /*from  w w  w. j  a v  a2 s . com*/
 * @return The serialized representation
 * @see #fromByteArray
 * @deprecated
 */
public byte[] serialize() throws PersistenceBrokerException {
    // Identity is serialized and written to an ObjectOutputStream
    // This ObjectOutputstream is compressed by a GZIPOutputStream
    // and finally written to a ByteArrayOutputStream.
    // the resulting byte[] is returned
    try {
        final ByteArrayOutputStream bao = new ByteArrayOutputStream();
        final GZIPOutputStream gos = new GZIPOutputStream(bao);
        final ObjectOutputStream oos = new ObjectOutputStream(gos);
        oos.writeObject(this);
        oos.close();
        gos.close();
        bao.close();
        return bao.toByteArray();
    } catch (Exception ignored) {
        throw new PersistenceBrokerException(ignored);
    }
}

From source file:com.chenshu.compress.CompressOldTest.java

@Benchmark
public int jdkGzipCompress() {
    ByteArrayOutputStream bout = null;
    GZIPOutputStream gzout = null;
    try {/*from   www .  jav  a 2 s. c om*/
        bout = new ByteArrayOutputStream(data.length);
        gzout = new GZIPOutputStream(bout) {
            {
                def.setLevel(level);
            }
        };
        gzout.write(data);
    } 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:org.apache.pig.test.TestCompressedFiles.java

@Override
@Before/*from w  ww.j a  v  a2s  . c o m*/
protected void setUp() throws Exception {
    datFile = File.createTempFile("compTest", ".dat");
    gzFile = File.createTempFile("compTest", ".gz");
    FileOutputStream dat = new FileOutputStream(datFile);
    GZIPOutputStream gz = new GZIPOutputStream(new FileOutputStream(gzFile));
    Random rand = new Random();
    for (int i = 0; i < 1024; i++) {
        StringBuffer sb = new StringBuffer();
        int x = rand.nextInt();
        int y = rand.nextInt();
        sb.append(x);
        sb.append('\t');
        sb.append(y);
        sb.append('\n');
        byte bytes[] = sb.toString().getBytes();
        dat.write(bytes);
        gz.write(bytes);
    }
    dat.close();
    gz.close();
}

From source file:org.efaps.admin.program.bundle.TempFileBundle.java

/**
 * @param _gziped zip the file//from   www .  j  a va 2 s  .c om
 * @return File
 * @throws EFapsException on error
 */
private File setFile(final boolean _gziped) throws EFapsException {
    final String filename = _gziped ? this.key + "GZIP" : this.key;
    final File ret = FileUtils.getFile(TempFileBundle.getTempFolder(), filename);
    try {
        final FileOutputStream out = new FileOutputStream(ret);
        final byte[] buffer = new byte[1024];
        int bytesRead;
        if (_gziped) {
            final GZIPOutputStream zout = new GZIPOutputStream(out);
            for (final String oid : this.oids) {
                final Checkout checkout = new Checkout(oid);
                final InputStream bis = checkout.execute();
                while ((bytesRead = bis.read(buffer)) != -1) {
                    zout.write(buffer, 0, bytesRead);
                }
            }
            zout.close();
        } else {
            for (final String oid : this.oids) {
                final Checkout checkout = new Checkout(oid);
                final InputStream bis = checkout.execute();
                while ((bytesRead = bis.read(buffer)) != -1) {
                    out.write(buffer, 0, bytesRead);
                }
            }
        }
        out.close();
    } catch (final IOException e) {
        throw new EFapsException(this.getClass(), "setFile", e, filename);
    }
    return ret;
}