Search in sources :

Example 81 with GZIPOutputStream

use of java.util.zip.GZIPOutputStream in project uavstack by uavorg.

the class CompressHelper method compressByteArrWithGZIP.

/**
 * Compress a byte array with GZIP
 *
 * @param byteArr
 *            a byte array
 * @return a compressed byte array
 * @throws IOException
 */
public static byte[] compressByteArrWithGZIP(byte[] byteArr) throws IOException {
    ByteArrayOutputStream bos = null;
    GZIPOutputStream gos = null;
    try {
        bos = new ByteArrayOutputStream();
        gos = new GZIPOutputStream(bos);
        gos.write(byteArr);
        gos.flush();
    } finally {
        closeStream(gos);
        closeStream(bos);
    }
    return bos.toByteArray();
}
Also used : GZIPOutputStream(java.util.zip.GZIPOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 82 with GZIPOutputStream

use of java.util.zip.GZIPOutputStream in project gocd by gocd.

the class StageStorage method save.

// File writes are not atomic in nature. There can be cases where the file is not completely written to disk.
// This is why we write to a tmp file and move it over to the actual file. File moves are atomic operations.
// If rename failed because target file already exists, we just ignore it because some other thread may have generated it.
public void save(Graph graph) {
    StageIdentifier identifier = extractStageIdentifier(graph);
    if (isStageStored(identifier)) {
        return;
    }
    synchronized (stageKey(identifier)) {
        if (isStageStored(identifier)) {
            return;
        }
        File file = new File(tmpStagePath(identifier));
        file.getParentFile().mkdirs();
        try {
            OutputStream os = new BufferedOutputStream(new GZIPOutputStream(new FileOutputStream(file)));
            graph.persistToTurtle(os);
            os.flush();
            os.close();
            file.renameTo(new File(stagePath(identifier)));
        } catch (IOException e) {
            throw new ShineRuntimeException(e);
        } finally {
            file.delete();
        }
    }
}
Also used : StageIdentifier(com.thoughtworks.go.domain.StageIdentifier) ShineRuntimeException(com.thoughtworks.studios.shine.ShineRuntimeException) GZIPOutputStream(java.util.zip.GZIPOutputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) BufferedOutputStream(java.io.BufferedOutputStream) GZIPOutputStream(java.util.zip.GZIPOutputStream) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) File(java.io.File) BufferedOutputStream(java.io.BufferedOutputStream)

Example 83 with GZIPOutputStream

use of java.util.zip.GZIPOutputStream in project felix by apache.

the class Utils method merge.

public static void merge(File targetIndex, File target, File sourceIndex, File source) throws IOException {
    List targetFiles = readIndex(targetIndex);
    List sourceFiles = readIndex(sourceIndex);
    List result = new ArrayList(targetFiles);
    File manifestFile = new File(source, (String) sourceFiles.remove(0));
    Manifest resultManifest = Utils.readManifest(manifestFile);
    resultManifest.getMainAttributes().remove(new Name(Constants.DEPLOYMENTPACKAGE_FIXPACK));
    for (Iterator i = result.iterator(); i.hasNext(); ) {
        String targetFile = (String) i.next();
        if (!MANIFEST_NAME.equals(targetFile) && !resultManifest.getEntries().containsKey(targetFile)) {
            i.remove();
        }
    }
    for (Iterator iter = sourceFiles.iterator(); iter.hasNext(); ) {
        String path = (String) iter.next();
        File from = new File(source, path);
        File to = new File(target, path);
        if (targetFiles.contains(path)) {
            if (!to.delete()) {
                throw new IOException("Could not delete " + to);
            }
        } else {
            result.add(path);
        }
        if (!rename(from, to)) {
            throw new IOException("Could not rename " + from + " to " + to);
        }
    }
    targetFiles.removeAll(sourceFiles);
    for (Iterator iter = resultManifest.getEntries().keySet().iterator(); iter.hasNext(); ) {
        String path = (String) iter.next();
        Attributes sourceAttribute = (Attributes) resultManifest.getEntries().get(path);
        if ("true".equals(sourceAttribute.remove(new Name(Constants.DEPLOYMENTPACKAGE_MISSING)))) {
            targetFiles.remove(path);
        }
    }
    for (Iterator iter = targetFiles.iterator(); iter.hasNext(); ) {
        String path = (String) iter.next();
        File targetFile = new File(target, path);
        if (!targetFile.delete()) {
            throw new IOException("Could not delete " + targetFile);
        }
    }
    GZIPOutputStream outputStream = new GZIPOutputStream(new FileOutputStream(new File(target, MANIFEST_NAME)));
    try {
        resultManifest.write(outputStream);
    } finally {
        outputStream.close();
    }
    writeIndex(targetIndex, result);
}
Also used : GZIPOutputStream(java.util.zip.GZIPOutputStream) FileOutputStream(java.io.FileOutputStream) ArrayList(java.util.ArrayList) Iterator(java.util.Iterator) Attributes(java.util.jar.Attributes) ArrayList(java.util.ArrayList) List(java.util.List) IOException(java.io.IOException) Manifest(java.util.jar.Manifest) JarFile(java.util.jar.JarFile) File(java.io.File) Name(java.util.jar.Attributes.Name)

Example 84 with GZIPOutputStream

use of java.util.zip.GZIPOutputStream in project felix by apache.

the class GogoPlugin method doPost.

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
    String encoding = request.getHeader("Accept-Encoding");
    boolean supportsGzip = (encoding != null && encoding.toLowerCase().indexOf("gzip") > -1);
    SessionTerminal st = terminalManager.getSessionTerminal(request);
    if (st != null) {
        String str = request.getParameter("k");
        String f = request.getParameter("f");
        String dump = st.handle(str, f != null && f.length() > 0);
        if (dump != null) {
            if (supportsGzip) {
                response.setHeader("Content-Encoding", "gzip");
                response.setHeader("Content-Type", "text/html; charset=UTF-8");
                try {
                    GZIPOutputStream gzos = new GZIPOutputStream(response.getOutputStream());
                    gzos.write(dump.getBytes("UTF-8"));
                    gzos.close();
                } catch (IOException ie) {
                    // handle the error here
                    ie.printStackTrace();
                }
            } else {
                response.setContentType("text/html; charset=UTF-8");
                response.getOutputStream().write(dump.getBytes("UTF-8"));
            }
        }
    } else {
        response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
    }
    response.flushBuffer();
}
Also used : GZIPOutputStream(java.util.zip.GZIPOutputStream) IOException(java.io.IOException)

Example 85 with GZIPOutputStream

use of java.util.zip.GZIPOutputStream in project openremote by openremote.

the class JSAPIWriter method writeJavaScript.

public void writeJavaScript(String base, HttpServletRequest req, HttpServletResponse resp, Map<String, ServiceRegistry> serviceRegistries) throws IOException {
    if (LOG.isLoggable(Level.FINE)) {
        LOG.fine("Starting JS API client");
    }
    // RESTEASY-776
    // before writing generated javascript, we generate Etag and compare it with client request.
    // If nothing changed, we send back 304 Not Modified for client browser to use cached js.
    String ifNoneMatch = req.getHeader("If-None-Match");
    String etag = generateEtag(serviceRegistries);
    resp.setHeader("Etag", etag);
    if (ifNoneMatch != null && ifNoneMatch.equals(etag)) {
        resp.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
        return;
    }
    for (Map.Entry<String, ServiceRegistry> entry : serviceRegistries.entrySet()) {
        String uri = base;
        if (entry.getKey() != null)
            uri += entry.getKey();
        StringWriter stringWriter = new StringWriter();
        PrintWriter writer = new PrintWriter(new BufferedWriter(stringWriter));
        writeJavaScript(uri, writer, entry.getValue());
        writer.flush();
        writer.close();
        if (clientIsGzipSupported(req)) {
            ByteArrayOutputStream compressedContent = new ByteArrayOutputStream();
            GZIPOutputStream gzipstream = new GZIPOutputStream(compressedContent);
            gzipstream.write(stringWriter.toString().getBytes(Charset.forName("utf-8")));
            gzipstream.finish();
            // get the compressed content
            byte[] compressedBytes = compressedContent.toByteArray();
            // set appropriate HTTP headers
            resp.setContentLength(compressedBytes.length);
            resp.addHeader("Content-Encoding", "gzip");
            ServletOutputStream output = resp.getOutputStream();
            output.write(compressedBytes);
            output.flush();
            output.close();
        } else {
            ServletOutputStream output = resp.getOutputStream();
            byte[] bytes = stringWriter.toString().getBytes(Charset.forName("utf-8"));
            resp.setContentLength(bytes.length);
            output.write(bytes);
            output.flush();
            output.close();
        }
    }
}
Also used : GZIPOutputStream(java.util.zip.GZIPOutputStream) ServletOutputStream(javax.servlet.ServletOutputStream) Map(java.util.Map)

Aggregations

GZIPOutputStream (java.util.zip.GZIPOutputStream)835 ByteArrayOutputStream (java.io.ByteArrayOutputStream)339 IOException (java.io.IOException)254 FileOutputStream (java.io.FileOutputStream)251 OutputStream (java.io.OutputStream)185 File (java.io.File)179 Test (org.junit.Test)93 BufferedOutputStream (java.io.BufferedOutputStream)84 GZIPInputStream (java.util.zip.GZIPInputStream)77 FileInputStream (java.io.FileInputStream)72 InputStream (java.io.InputStream)64 ByteArrayInputStream (java.io.ByteArrayInputStream)60 OutputStreamWriter (java.io.OutputStreamWriter)53 ObjectOutputStream (java.io.ObjectOutputStream)39 DataOutputStream (java.io.DataOutputStream)38 BufferedWriter (java.io.BufferedWriter)30 ByteBuffer (java.nio.ByteBuffer)28 Path (java.nio.file.Path)28 ArrayList (java.util.ArrayList)28 NodeSettings (org.knime.core.node.NodeSettings)28