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.twentyn.patentExtractor.Util.java

public static byte[] compressXMLDocument(Document doc)
        throws IOException, TransformerConfigurationException, TransformerException {
    Transformer transformer = getTransformerFactory().newTransformer();
    // The OutputKeys.INDENT configuration key determines whether the output is indented.

    DOMSource w3DomSource = new DOMSource(doc);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    Writer w = new BufferedWriter(new OutputStreamWriter(
            new GZIPOutputStream(new Base64OutputStream(baos, true, 0, new byte[] { '\n' }))));
    StreamResult sResult = new StreamResult(w);
    transformer.transform(w3DomSource, sResult);
    w.close();//from  w w w.j av  a2 s .co  m
    return baos.toByteArray();
}

From source file:com.panet.imeta.www.GetTransStatusServlet.java

protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    if (!request.getContextPath().equals(CONTEXT_PATH))
        return;//  w  w  w .j  av a2s.co  m

    if (log.isDebug())
        log.logDebug(toString(), Messages.getString("TransStatusServlet.Log.TransStatusRequested"));

    String transName = request.getParameter("name");
    boolean useXML = "Y".equalsIgnoreCase(request.getParameter("xml"));

    response.setStatus(HttpServletResponse.SC_OK);

    if (useXML) {
        response.setContentType("text/xml");
        response.setCharacterEncoding(Const.XML_ENCODING);
    } else {
        response.setContentType("text/html");
    }

    PrintWriter out = response.getWriter();

    Trans trans = transformationMap.getTransformation(transName);

    if (trans != null) {
        String status = trans.getStatus();

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

            SlaveServerTransStatus transStatus = new SlaveServerTransStatus(transName, status);

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

            Log4jStringAppender appender = (Log4jStringAppender) transformationMap.getAppender(transName);
            if (appender != null) {
                // 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(appender.getBuffer().toString().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
            //
            out.println(transStatus.getXML());
        } else {
            response.setContentType("text/html");

            out.println("<HTML>");
            out.println("<HEAD>");
            out.println("<TITLE>" + Messages.getString("TransStatusServlet.KettleTransStatus") + "</TITLE>");
            out.println("<META http-equiv=\"Refresh\" content=\"10;url=/kettle/transStatus?name="
                    + URLEncoder.encode(transName, "UTF-8") + "\">");
            out.println("</HEAD>");
            out.println("<BODY>");
            out.println("<H1>" + Messages.getString("TransStatusServlet.TopTransStatus", transName) + "</H1>");

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

                out.print("<tr>");
                out.print("<td>" + transName + "</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=\"/kettle/startTrans?name=" + URLEncoder.encode(transName, "UTF-8")
                            + "\">" + Messages.getString("TransStatusServlet.StartTrans") + "</a>");
                    out.print("<p>");
                    out.print("<a href=\"/kettle/prepareExec?name=" + URLEncoder.encode(transName, "UTF-8")
                            + "\">" + Messages.getString("TransStatusServlet.PrepareTrans") + "</a><br>");
                    //out.print("<a href=\"/kettle/startExec?name="+URLEncoder.encode(transName, "UTF-8")+"\">" + Messages.getString("TransStatusServlet.StartTrans") + "</a><p>");
                } else if (trans.isRunning()) {
                    out.print("<a href=\"/kettle/pauseTrans?name=" + URLEncoder.encode(transName, "UTF-8")
                            + "\">" + Messages.getString("PauseStatusServlet.PauseResumeTrans") + "</a><br>");
                    out.print("<a href=\"/kettle/stopTrans?name=" + URLEncoder.encode(transName, "UTF-8")
                            + "\">" + Messages.getString("TransStatusServlet.StopTrans") + "</a>");
                    out.print("<p>");
                }
                out.print("<a href=\"/kettle/cleanupTrans?name=" + URLEncoder.encode(transName, "UTF-8") + "\">"
                        + Messages.getString("TransStatusServlet.CleanupTrans") + "</a>");
                out.print("<p>");

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

                for (int i = 0; i < trans.nrSteps(); i++) {
                    BaseStep baseStep = trans.getRunThread(i);
                    if ((baseStep.isAlive()) || baseStep.getStatus() != StepDataInterface.STATUS_EMPTY) {
                        StepStatus stepStatus = new StepStatus(baseStep);
                        out.print(stepStatus.getHTMLTableRow());
                    }
                }
                out.println("</table>");
                out.println("<p>");

                out.print("<a href=\"/kettle/transStatus/?name=" + URLEncoder.encode(transName, "UTF-8")
                        + "&xml=y\">" + Messages.getString("TransStatusServlet.ShowAsXml") + "</a><br>");
                out.print("<a href=\"/kettle/status\">"
                        + Messages.getString("TransStatusServlet.BackToStatusPage") + "</a><br>");
                out.print("<p><a href=\"/kettle/transStatus?name=" + URLEncoder.encode(transName, "UTF-8")
                        + "\">" + Messages.getString("TransStatusServlet.Refresh") + "</a>");

                // Put the logging below that.
                Log4jStringAppender appender = (Log4jStringAppender) transformationMap.getAppender(transName);
                if (appender != null) {
                    out.println("<p>");
                    /*
                    out.println("<pre>");
                    out.println(appender.getBuffer().toString());
                    out.println("</pre>");
                    */
                    out.println(
                            "<textarea id=\"translog\" cols=\"120\" rows=\"20\" wrap=\"off\" name=\"Transformation log\" readonly=\"readonly\">"
                                    + appender.getBuffer().toString() + "</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,
                    Messages.getString("TransStatusServlet.Log.CoundNotFindSpecTrans", transName)));
        } else {
            out.println("<H1>" + Messages.getString("TransStatusServlet.Log.CoundNotFindTrans", transName)
                    + "</H1>");
            out.println("<a href=\"/kettle/status\">"
                    + Messages.getString("TransStatusServlet.BackToStatusPage") + "</a><p>");

        }
    }
}

From source file:com.wbtech.dao.NetworkUitlity.java

public static AbstractHttpEntity initEntity(byte[] paramArrayOfByte) {
    ByteArrayEntity localByteArrayEntity = null;
    ByteArrayOutputStream localByteArrayOutputStream = new ByteArrayOutputStream();
    GZIPOutputStream localGZIPOutputStream;
    if (paramArrayOfByte.length < paramleng) {
        localByteArrayEntity = new ByteArrayEntity(paramArrayOfByte);
    } else {/*from  w ww. j  a v  a 2 s. c o m*/

        try {
            localGZIPOutputStream = new GZIPOutputStream(localByteArrayOutputStream);
            localGZIPOutputStream.write(paramArrayOfByte);
            localGZIPOutputStream.close();
            localByteArrayEntity = new ByteArrayEntity(localByteArrayOutputStream.toByteArray());
            localByteArrayEntity.setContentEncoding("gzip");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return localByteArrayEntity;
}

From source file:com.xyxy.platform.examples.showcase.demos.web.StaticContentServlet.java

/**
 * Gzip HeaderGZIPOutputStream.// w  ww  .j av  a  2 s  . c  o m
 */
private 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.github.jrh3k5.flume.mojo.plugin.io.ArchiveUtils.java

/**
 * GZIP a file.//from  ww  w .j  a va 2 s  . c o  m
 * 
 * @param toZip
 *            A {@link File} representing the file to be GZIP'ed.
 * @param toFile
 *            A {@link File} representing the location at which the GZIP
 *            file is to be created.
 * @throws IllegalArgumentException
 *             If the given source file is not a file or does not exist, or
 *             if the given destination file exists but is not a file.
 * @throws IOException
 *             If any errors occur during the GZIP'ing.
 * @see #gunzipFile(File, File)
 */
public void gzipFile(File toZip, File toFile) throws IOException {
    if (!toZip.isFile()) {
        throw new IllegalArgumentException("Source file " + toZip + " must be an existent file.");
    }

    if (toFile.exists() && !toFile.isFile()) {
        throw new IllegalArgumentException("Destination file " + toFile
                + " exists, but is not a file and, as such, cannot be written to.");
    }

    GZIPOutputStream zipOut = null;
    FileInputStream tarIn = null;
    try {
        tarIn = new FileInputStream(toZip);
        zipOut = new GZIPOutputStream(new FileOutputStream(toFile));
        IOUtils.copy(tarIn, zipOut);
    } finally {
        IOUtils.closeQuietly(zipOut);
        IOUtils.closeQuietly(tarIn);
    }
}

From source file:com.kloudtek.buildmagic.tools.createinstaller.deb.CreateDebTask.java

private void prepare() throws IOException {
    // validate parameters
    if (name == null || name.trim().length() == 0) {
        throw new BuildException("name attribute is missing or invalid");
    }/*www .  j ava2s . c o m*/
    if (version == null || version.trim().length() == 0) {
        throw new BuildException("version attribute is missing or invalid");
    }
    if (arch == null || arch.trim().length() == 0) {
        throw new BuildException("arch attribute is missing or invalid");
    }
    if (maintName == null || maintName.trim().length() == 0) {
        throw new BuildException("maintName attribute is missing");
    }
    if (maintEmail == null || maintEmail.trim().length() == 0) {
        throw new BuildException("maintEmail attribute is missing");
    }
    if (priority != null && !PRIORITIES.contains(priority.toLowerCase())) {
        log("Priority '" + priority + "' isn't recognized as a valid value", Project.MSG_WARN);
    }
    if (control != null) {
        for (final TemplateEntry entry : control.getTemplateEntries()) {
            if (entry.getPackageName() == null) {
                entry.setPackageName(name);
            }
            templateEntries.add(entry);
        }
    }
    // generate copyright file
    if (copyrights.isEmpty()) {
        if (license == null) {
            throw new BuildException("No license has been specified");
        }
        if (licenseFile == null) {
            licenseFile = new File("license.txt");
        }
        copyrights.add(new Copyright(license, licenseFile));
    }
    copyrights.validate();
    copyright = dataBufferManager.createBuffer();
    copyrights.write(copyright, this);
    // generate changelog
    changelog = new Changelog(name);
    Changelog.Entry entry = changelog.createEntry();
    changelog.setDistributions("main"); // TODO
    entry.setMaintName(maintName);
    entry.setMaintEmail(maintEmail);
    entry.setChanges("Released");
    entry.setVersion(version);
    entry.setDate(new Date());
    changelog.validate();
    changelogData = changelog.export(Changelog.Type.DEBIAN);
    ByteArrayOutputStream changelogBuf = new ByteArrayOutputStream();
    GZIPOutputStream changelogGzipBuf = new GZIPOutputStream(changelogBuf);
    changelogGzipBuf.write(changelogData.getBytes());
    changelogGzipBuf.finish();
    changelogGzipBuf.close();
    changelogDataGzipped = changelogBuf.toByteArray();
}

From source file:edu.ucsb.eucalyptus.cloud.ws.PutMethodWithProgress.java

@Override
protected boolean writeRequestBody(HttpState state, HttpConnection conn) throws IOException {
    InputStream inputStream;/*from www.  java  2 s  . co m*/
    if (outFile != null) {
        inputStream = new FileInputStream(outFile);

        ChunkedOutputStream chunkedOut = new ChunkedOutputStream(conn.getRequestOutputStream());
        byte[] buffer = new byte[StorageProperties.TRANSFER_CHUNK_SIZE];
        int bytesRead;
        int numberProcessed = 0;
        long totalBytesProcessed = 0;
        while ((bytesRead = inputStream.read(buffer)) > 0) {
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            GZIPOutputStream zip = new GZIPOutputStream(out);
            zip.write(buffer, 0, bytesRead);
            zip.close();
            chunkedOut.write(out.toByteArray());
            totalBytesProcessed += bytesRead;
            if (++numberProcessed >= callback.getUpdateThreshold()) {
                callback.run();
                numberProcessed = 0;
            }
        }
        if (totalBytesProcessed > 0) {
            callback.finish();
        } else {
            callback.failed();
        }
        chunkedOut.finish();
        inputStream.close();
    } else {
        return false;
    }
    return true;
}

From source file:com.eucalyptus.blockstorage.PutMethodWithProgress.java

@Override
protected boolean writeRequestBody(HttpState state, HttpConnection conn) throws IOException {
    InputStream inputStream;// w  ww .  ja v a  2  s  .  com
    if (outFile != null) {
        inputStream = new FileInputStream(outFile);

        ChunkedOutputStream chunkedOut = new ChunkedOutputStream(conn.getRequestOutputStream());
        byte[] buffer = new byte[StorageProperties.TRANSFER_CHUNK_SIZE];
        int bytesRead;
        long totalBytesProcessed = 0;
        while ((bytesRead = inputStream.read(buffer)) > 0) {
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            GZIPOutputStream zip = new GZIPOutputStream(out);
            zip.write(buffer, 0, bytesRead);
            zip.close();
            chunkedOut.write(out.toByteArray());
            totalBytesProcessed += bytesRead;
            callback.update(totalBytesProcessed);
        }
        if (totalBytesProcessed > 0) {
            callback.finish();
        } else {
            callback.failed();
        }
        chunkedOut.finish();
        inputStream.close();
    } else {
        return false;
    }
    return true;
}

From source file:com.android.tradefed.util.TarUtil.java

/**
 * Utility function to gzip (.gz) a file. the .gz extension will be added to base file name.
 *
 * @param inputFile the {@link File} to be gzipped.
 * @return the gzipped file.//www.ja v  a2 s .  co m
 * @throws IOException
 */
public static File gzip(final File inputFile) throws IOException {
    File outputFile = FileUtil.createTempFile(inputFile.getName(), ".gz");
    GZIPOutputStream out = null;
    FileInputStream in = null;
    try {
        out = new GZIPOutputStream(new FileOutputStream(outputFile));
        in = new FileInputStream(inputFile);
        IOUtils.copy(in, out);
    } catch (IOException e) {
        // delete the tmp file if we failed to gzip.
        FileUtil.deleteFile(outputFile);
        throw e;
    } finally {
        StreamUtil.close(in);
        StreamUtil.close(out);
    }
    return outputFile;
}

From source file:io.druid.storage.s3.S3DataSegmentPullerTest.java

@Test
public void testGZUncompressRetries() throws S3ServiceException, IOException, SegmentLoadingException {
    final String bucket = "bucket";
    final String keyPrefix = "prefix/dir/0";
    final RestS3Service s3Client = EasyMock.createStrictMock(RestS3Service.class);
    final byte[] value = bucket.getBytes("utf8");

    final File tmpFile = Files.createTempFile("gzTest", ".gz").toFile();
    tmpFile.deleteOnExit();/*ww w .j  a va2s  .c  o m*/
    try (OutputStream outputStream = new GZIPOutputStream(new FileOutputStream(tmpFile))) {
        outputStream.write(value);
    }

    S3Object object0 = new S3Object();

    object0.setBucketName(bucket);
    object0.setKey(keyPrefix + "/renames-0.gz");
    object0.setLastModifiedDate(new Date(0));
    object0.setDataInputStream(new FileInputStream(tmpFile));

    File tmpDir = Files.createTempDirectory("gzTestDir").toFile();

    S3ServiceException exception = new S3ServiceException();
    exception.setErrorCode("NoSuchKey");
    exception.setResponseCode(404);
    try {
        EasyMock.expect(
                s3Client.getObjectDetails(EasyMock.<S3Bucket>anyObject(), EasyMock.eq(object0.getKey())))
                .andReturn(null).once();
        EasyMock.expect(s3Client.getObject(EasyMock.eq(bucket), EasyMock.eq(object0.getKey())))
                .andThrow(exception).once().andReturn(object0).once();
        S3DataSegmentPuller puller = new S3DataSegmentPuller(s3Client);

        EasyMock.replay(s3Client);
        FileUtils.FileCopyResult result = puller
                .getSegmentFiles(new S3DataSegmentPuller.S3Coords(bucket, object0.getKey()), tmpDir);
        EasyMock.verify(s3Client);

        Assert.assertEquals(value.length, result.size());
        File expected = new File(tmpDir, "renames-0");
        Assert.assertTrue(expected.exists());
        Assert.assertEquals(value.length, expected.length());
    } finally {
        org.apache.commons.io.FileUtils.deleteDirectory(tmpDir);
    }
}