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:aot.util.IOUtil.java

public static byte[] compress(byte[] data, int offset, int length) {
    try (ByteArrayOutputStream buffer = new ByteArrayOutputStream(data.length)) {
        try (GZIPOutputStream output = new GZIPOutputStream(buffer)) {
            output.write(data, offset, length);
        }/*from  ww w  .jav  a  2 s. co m*/
        return buffer.toByteArray();
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

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

@Benchmark
public int jdkGzipCompress() {
    ByteArrayOutputStream bout = null;
    GZIPOutputStream gzout = null;
    try {//from  w  w  w  . j  a  v  a 2s  . c  o m
        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:com.taobao.tair.etc.TranscoderUtil.java

public static byte[] compress(byte[] in) {
    if (in == null) {
        throw new NullPointerException("Can't compress null");
    }/*ww w .  java  2  s. co  m*/

    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    GZIPOutputStream gz = null;

    try {
        gz = new GZIPOutputStream(bos);
        gz.write(in);
    } catch (IOException e) {
        throw new RuntimeException("IO exception compressing data", e);
    } finally {
        try {
            gz.close();
            bos.close();
        } catch (Exception e) {
            // should not happen
        }
    }

    byte[] rv = bos.toByteArray();

    if (log.isInfoEnabled()) {
        log.info("compressed value, size from [" + in.length + "] to [" + rv.length + "]");
    }

    return rv;
}

From source file:com.asksunny.tool.RemoteDataStreamer.java

public void sendCompress() throws Exception {
    try (Socket client = SocketFactory.getDefault().createSocket(getHost(), getPort());
            OutputStream out = client.getOutputStream();
            GZIPOutputStream gout = new GZIPOutputStream(out);
            FileInputStream fin = new FileInputStream(getFilePath())) {
        StreamCopier.copy(fin, gout);/*from   w w w .  j  av  a 2s .co  m*/
    }
}

From source file:com.bt.download.android.gui.httpserver.BrowseHandler.java

@Override
public void handle(HttpExchange exchange) throws IOException {
    assertUPnPActive();/*from  www  .j  a  v a  2 s  . co  m*/

    GZIPOutputStream os = null;

    byte type = -1;

    try {

        List<NameValuePair> query = URLEncodedUtils.parse(exchange.getRequestURI(), "UTF-8");

        for (NameValuePair item : query) {
            if (item.getName().equals("type")) {
                type = Byte.parseByte(item.getValue());
            }
        }

        if (type == -1) {
            exchange.sendResponseHeaders(Code.HTTP_BAD_REQUEST, 0);
            return;
        }

        String response = getResponse(exchange, type);

        exchange.getResponseHeaders().set("Content-Encoding", "gzip");
        exchange.getResponseHeaders().set("Content-Type", "text/json; charset=UTF-8");
        exchange.sendResponseHeaders(Code.HTTP_OK, 0);

        os = new GZIPOutputStream(exchange.getResponseBody());

        os.write(response.getBytes("UTF-8"));
        os.finish();

    } catch (IOException e) {
        LOG.warning("Error browsing files type=" + type);
        throw e;
    } finally {
        if (os != null) {
            os.close();
        }
        exchange.close();
    }
}

From source file:de.tudarmstadt.ukp.dkpro.core.api.resources.CompressionUtils.java

/**
 * Make sure the target directory exists and get a stream writing to the specified file within.
 * If the file name ends with a typical extension for compressed files, the stream will be
 * compressed.//w w  w .  ja v  a2 s.com
 * 
 * @param aFile
 *            the target file.
 * @return a stream to write to.
 * 
 * @see CompressionMethod
 */
public static OutputStream getOutputStream(File aFile) throws IOException {
    // Create parent folders for output file and set up stream
    if (aFile.getParentFile() != null) {
        forceMkdir(aFile.getParentFile());
    }

    String lcFilename = aFile.getName().toLowerCase();

    OutputStream os = new FileOutputStream(aFile);
    if (lcFilename.endsWith(GZIP.getExtension())) {
        os = new GZIPOutputStream(os);
    } else if (lcFilename.endsWith(BZIP2.getExtension()) || lcFilename.endsWith(".bzip2")) {
        os = new BZip2CompressorOutputStream(os);
    } else if (lcFilename.endsWith(XZ.getExtension())) {
        os = new XZCompressorOutputStream(os);
    }
    return os;
}

From source file:z.hol.net.http.entity.compress.GzipCompressingEntity.java

@Override
public void writeTo(final OutputStream outstream) throws IOException {
    if (outstream == null) {
        throw new IllegalArgumentException("Output stream may not be null");
    }/*from www .  java2s  .c o  m*/
    GZIPOutputStream gzip = new GZIPOutputStream(outstream);
    InputStream in = wrappedEntity.getContent();
    byte[] tmp = new byte[2048];
    int l;
    while ((l = in.read(tmp)) != -1) {
        gzip.write(tmp, 0, l);
    }
    gzip.close();
}

From source file:com.nebhale.cyclinglibrary.web.GzipFilter.java

@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response,
        FilterChain filterChain) throws ServletException, IOException {

    HttpServletRequest newRequest;/*ww w.jav  a  2s. c om*/
    if (sendsGzipEncoding(request)) {
        newRequest = new DelegatingHttpServletRequest(request, new GZIPInputStream(request.getInputStream()));
    } else {
        String method = request.getMethod();
        if ("POST".equals(method) || "PUT".equals(method)) {
            this.logger.warn("Uncompressed input received for '{} {}'", method, request.getRequestURI());
        }

        newRequest = request;
    }

    HttpServletResponse newResponse;
    OutputStream outputStream;
    if (acceptsGzipEncoding(request)) {
        response.setHeader("Content-Encoding", "gzip");
        outputStream = new GZIPOutputStream(response.getOutputStream());
        newResponse = new DelegatingHttpServletResponse(response, outputStream);
    } else {
        String method = request.getMethod();
        if (!"DELETE".equals(method)) {
            this.logger.warn("Uncompressed output requested for '{} {}'", method, request.getRequestURI());
        }

        outputStream = response.getOutputStream();
        newResponse = response;
    }

    filterChain.doFilter(newRequest, newResponse);
}

From source file:com.metamolecular.pubcouch.test.SnapshotTest.java

private byte[] zipStringToBytes(String input) throws IOException {
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    BufferedOutputStream bufos = new BufferedOutputStream(new GZIPOutputStream(bos));
    bufos.write(input.getBytes("UTF-8"));
    bufos.close();//from  w  w w .  j  a v  a2  s .c o m
    byte[] retval = bos.toByteArray();
    bos.close();
    return retval;
}

From source file:cascading.util.Util.java

public static String serializeBase64(Object object, boolean compress) throws IOException {
    ByteArrayOutputStream bytes = new ByteArrayOutputStream();

    ObjectOutputStream out = new ObjectOutputStream(compress ? new GZIPOutputStream(bytes) : bytes);

    try {/*from   www .  j  a  v a 2  s.co  m*/
        out.writeObject(object);
    } finally {
        out.close();
    }

    return new String(Base64.encodeBase64(bytes.toByteArray()));
}