Java Code Examples for java.util.zip.GZIPOutputStream#finish()

The following examples show how to use java.util.zip.GZIPOutputStream#finish() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: LoadosophiaAPIClient.java    From jmeter-bzm-plugins with Apache License 2.0 6 votes vote down vote up
private File gzipFile(File src) throws IOException {
    // Never try to make it stream-like on the fly, because content-length still required
    // Create the GZIP output stream
    String outFilename = src.getAbsolutePath() + ".gz";
    notifier.notifyAbout("Gzipping " + src.getAbsolutePath());
    GZIPOutputStream out = new GZIPOutputStream(new FileOutputStream(outFilename), 1024 * 8, true);

    // Open the input file
    FileInputStream in = new FileInputStream(src);

    // Transfer bytes from the input file to the GZIP output stream
    byte[] buf = new byte[10240];
    int len;
    while ((len = in.read(buf)) > 0) {
        out.write(buf, 0, len);
    }
    in.close();

    // Complete the GZIP file
    out.finish();
    out.close();

    src.delete();

    return new File(outFilename);
}
 
Example 2
Source File: MetricsHttpServer.java    From dts with Apache License 2.0 6 votes vote down vote up
public void handle(HttpExchange t) throws IOException {
    String query = t.getRequestURI().getRawQuery();
    ByteArrayOutputStream response = this.response.get();
    response.reset();
    OutputStreamWriter osw = new OutputStreamWriter(response);
    TextFormat.write004(osw, registry.filteredMetricFamilySamples(parseQuery(query)));
    osw.flush();
    osw.close();
    response.flush();
    response.close();
    t.getResponseHeaders().set("Content-Type", TextFormat.CONTENT_TYPE_004);
    t.getResponseHeaders().set("Content-Length", String.valueOf(response.size()));
    if (shouldUseCompression(t)) {
        t.getResponseHeaders().set("Content-Encoding", "gzip");
        t.sendResponseHeaders(HttpURLConnection.HTTP_OK, 0);
        final GZIPOutputStream os = new GZIPOutputStream(t.getResponseBody());
        response.writeTo(os);
        os.finish();
    } else {
        t.sendResponseHeaders(HttpURLConnection.HTTP_OK, response.size());
        response.writeTo(t.getResponseBody());
    }
    t.close();
}
 
Example 3
Source File: GzipCompressor.java    From multiple-dimension-spread with Apache License 2.0 6 votes vote down vote up
@Override
public byte[] compress( final byte[] data , final int start , final int length , final DataType dataType ) throws IOException{
  ByteArrayOutputStream bOut = new ByteArrayOutputStream();
  GZIPOutputStream out = new GZIPOutputStream( bOut );

  out.write( data , start , length );
  out.flush();
  out.finish();
  byte[] compressByte = bOut.toByteArray();
  byte[] retVal = new byte[ Integer.BYTES + compressByte.length ];
  ByteBuffer wrapBuffer = ByteBuffer.wrap( retVal );
  wrapBuffer.putInt( length );
  wrapBuffer.put( compressByte );

  bOut.close();
  out.close();

  return retVal;
}
 
Example 4
Source File: GelfMessage.java    From xian with Apache License 2.0 6 votes vote down vote up
private byte[] gzipMessage(byte[] message) {
    ByteArrayOutputStream bos = new ByteArrayOutputStream();

    try {
        GZIPOutputStream stream = new GZIPOutputStream(bos);

        stream.write(message);
        stream.finish();
        Closer.close(stream);
        byte[] zipped = bos.toByteArray();
        Closer.close(bos);
        return zipped;
    } catch (IOException e) {
        return null;
    }
}
 
Example 5
Source File: XMLHttpRequest2Test.java    From htmlunit with Apache License 2.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
protected void doGet(final HttpServletRequest request, final HttpServletResponse response) throws IOException {
    final byte[] bytes = RESPONSE.getBytes(UTF_8);
    final ByteArrayOutputStream bos = new ByteArrayOutputStream();
    final GZIPOutputStream gout = new GZIPOutputStream(bos);
    gout.write(bytes);
    gout.finish();

    final byte[] encoded = bos.toByteArray();

    response.setContentType(MimeType.TEXT_XML);
    response.setCharacterEncoding(UTF_8.name());
    response.setStatus(200);
    response.setContentLength(encoded.length);
    response.setHeader("Content-Encoding", "gzip");

    final OutputStream rout = response.getOutputStream();
    rout.write(encoded);
}
 
Example 6
Source File: WebResponseTest.java    From htmlunit with Apache License 2.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
protected void doGet(final HttpServletRequest request, final HttpServletResponse response) throws IOException {
    final byte[] bytes = RESPONSE.getBytes(UTF_8);
    final ByteArrayOutputStream bos = new ByteArrayOutputStream();
    final GZIPOutputStream gout = new GZIPOutputStream(bos);
    gout.write(bytes);
    gout.finish();

    final byte[] encoded = bos.toByteArray();

    response.setContentType(MimeType.TEXT_HTML);
    response.setCharacterEncoding(UTF_8.name());
    response.setStatus(200);
    response.setContentLength(encoded.length);
    response.setHeader("Content-Encoding", "gzip");

    final OutputStream rout = response.getOutputStream();
    rout.write(encoded);
}
 
Example 7
Source File: RequestUtil.java    From eagle with Apache License 2.0 5 votes vote down vote up
public static byte[] compress(byte[] data) throws IOException {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    GZIPOutputStream gos = new GZIPOutputStream(out);
    gos.write(data);
    gos.finish();
    gos.flush();
    gos.close();
    byte[] ret = out.toByteArray();
    return ret;
}
 
Example 8
Source File: KcUtils.java    From GotoBrowser with GNU General Public License v3.0 5 votes vote down vote up
public static byte[] gzipcompress(String value) throws IOException {
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    GZIPOutputStream gzipOutStream = new GZIPOutputStream(
            new BufferedOutputStream(byteArrayOutputStream));
    gzipOutStream.write(value.getBytes());
    gzipOutStream.finish();
    gzipOutStream.close();

    return byteArrayOutputStream.toByteArray();
}
 
Example 9
Source File: GzipUtil.java    From Data_Processor with Apache License 2.0 5 votes vote down vote up
public static byte[] compress(byte[] data) throws IOException {
	if (null== data|| 0== data.length) {
		return null;
	}
	ByteArrayOutputStream out= new ByteArrayOutputStream();
	GZIPOutputStream gzip= new GZIPOutputStream(out);
	gzip.write(data);
	gzip.finish();
	gzip.close();
	byte[] ret= out.toByteArray();
	out.close();
	return  ret;//out.toString("ISO-8859-1");
}
 
Example 10
Source File: NanoHTTPD.java    From JsDroidCmd with Mozilla Public License 2.0 5 votes vote down vote up
private void sendBodyWithCorrectEncoding(OutputStream outputStream, long pending) throws IOException {
    if (encodeAsGzip) {
        GZIPOutputStream gzipOutputStream = new GZIPOutputStream(outputStream);
        sendBody(gzipOutputStream, -1);
        gzipOutputStream.finish();
    } else {
        sendBody(outputStream, pending);
    }
}
 
Example 11
Source File: GzipCompressionProvider.java    From xian with Apache License 2.0 5 votes vote down vote up
@Override
public byte[] compress(String path, byte[] data) throws Exception
{
    ByteArrayOutputStream       bytes = new ByteArrayOutputStream();
    GZIPOutputStream            out = new GZIPOutputStream(bytes);
    try {
        out.write(data);
        out.finish();
    } finally {
        out.close();
    }
    return bytes.toByteArray();
}
 
Example 12
Source File: OmcTextDecoder.java    From OmcTextDecoder with Apache License 2.0 5 votes vote down vote up
private byte[] _compressGzip(byte[] data) throws IOException {
    ByteArrayOutputStream bos = new ByteArrayOutputStream(data.length);
    GZIPOutputStream gzip = new GZIPOutputStream(bos) {
        {
            this.def.setLevel(Deflater.BEST_COMPRESSION);
        }
    };
    gzip.write(data);
    gzip.finish();
    gzip.close();
    byte[] compressed = bos.toByteArray();
    bos.close();
    return compressed;
}
 
Example 13
Source File: KcaUtils.java    From kcanotify_h5-master with GNU General Public License v3.0 5 votes vote down vote up
public static byte[] gzipcompress(String value) throws IOException {
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    GZIPOutputStream gzipOutStream = new GZIPOutputStream(
            new BufferedOutputStream(byteArrayOutputStream));
    gzipOutStream.write(value.getBytes());
    gzipOutStream.finish();
    gzipOutStream.close();

    return byteArrayOutputStream.toByteArray();
}
 
Example 14
Source File: GZipUtils.java    From util4j with Apache License 2.0 5 votes vote down vote up
/**
 * 数据压缩
 * @param is
 * @param os
 * @throws Exception
 */
public static void compress(InputStream is, OutputStream os) throws Exception {
	GZIPOutputStream gos = new GZIPOutputStream(os);
	try {
		int count;
		byte data[] = new byte[BUFFER];
		while ((count = is.read(data, 0, BUFFER)) != -1) {
			gos.write(data, 0, count);
		}
	}finally {
		gos.finish();
		gos.flush();
		gos.close();
	}
}
 
Example 15
Source File: NanoHTTPD.java    From VBrowser-Android with GNU General Public License v2.0 5 votes vote down vote up
private void sendBodyWithCorrectEncoding(OutputStream outputStream, long pending) throws IOException {
    if (encodeAsGzip) {
        GZIPOutputStream gzipOutputStream = new GZIPOutputStream(outputStream);
        sendBody(gzipOutputStream, -1);
        gzipOutputStream.finish();
    } else {
        sendBody(outputStream, pending);
    }
}
 
Example 16
Source File: Installer.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private static void uploadGZFile(PrintStream os, File f) throws IOException {
    GZIPOutputStream gzip = new GZIPOutputStream(os);
    try (BufferedInputStream is = new BufferedInputStream(new FileInputStream(f))) {
        byte[] buffer = new byte[4096];
        int readLength = is.read(buffer);
        while (readLength != -1){
            gzip.write(buffer, 0, readLength);
            readLength = is.read(buffer);
        }
    } finally {
        gzip.finish();
    }
}
 
Example 17
Source File: PreCompressedResourceTestCase.java    From quarkus-http with Apache License 2.0 5 votes vote down vote up
private void generateGZipFile(Path source, Path target) throws IOException {
    byte[] buffer = new byte[1024];

    GZIPOutputStream gzos = new GZIPOutputStream(new FileOutputStream(target.toFile()));
    FileInputStream in = new FileInputStream(source.toFile());

    int len;
    while ((len = in.read(buffer)) > 0) {
        gzos.write(buffer, 0, len);
    }

    in.close();
    gzos.finish();
    gzos.close();
}
 
Example 18
Source File: GZIPInZip.java    From jdk8u60 with GNU General Public License v2.0 4 votes vote down vote up
private static void doTest(final boolean appendGarbage,
                           final boolean limitGISBuff)
        throws Throwable {

    byte[] buf;

    try (ByteArrayOutputStream baos = new ByteArrayOutputStream();
         ZipOutputStream zos = new ZipOutputStream(baos)) {

        final byte[] xbuf = { 'x' };

        zos.putNextEntry(new ZipEntry("a.gz"));
        GZIPOutputStream gos1 = new GZIPOutputStream(zos);
        gos1.write(xbuf);
        gos1.finish();
        if (appendGarbage)
            zos.write(xbuf);
        zos.closeEntry();

        zos.putNextEntry(new ZipEntry("b.gz"));
        GZIPOutputStream gos2 = new GZIPOutputStream(zos);
        gos2.write(xbuf);
        gos2.finish();
        zos.closeEntry();

        zos.flush();
        buf = baos.toByteArray();
    }

    try (ByteArrayInputStream bais = new ByteArrayInputStream(buf);
         ZipInputStream zis = new ZipInputStream(bais)) {

        zis.getNextEntry();
        GZIPInputStream gis1 = limitGISBuff ?
                new GZIPInputStream(zis, 4) :
                new GZIPInputStream(zis);
        // try to read more than the entry has
        gis1.skip(2);

        try {
            zis.getNextEntry();
        } catch (IOException e) {
            throw new RuntimeException("ZIP stream was prematurely closed", e);
        }
    }
}
 
Example 19
Source File: GZIPInZip.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
private static void doTest(final boolean appendGarbage,
                           final boolean limitGISBuff)
        throws Throwable {

    byte[] buf;

    try (ByteArrayOutputStream baos = new ByteArrayOutputStream();
         ZipOutputStream zos = new ZipOutputStream(baos)) {

        final byte[] xbuf = { 'x' };

        zos.putNextEntry(new ZipEntry("a.gz"));
        GZIPOutputStream gos1 = new GZIPOutputStream(zos);
        gos1.write(xbuf);
        gos1.finish();
        if (appendGarbage)
            zos.write(xbuf);
        zos.closeEntry();

        zos.putNextEntry(new ZipEntry("b.gz"));
        GZIPOutputStream gos2 = new GZIPOutputStream(zos);
        gos2.write(xbuf);
        gos2.finish();
        zos.closeEntry();

        zos.flush();
        buf = baos.toByteArray();
    }

    try (ByteArrayInputStream bais = new ByteArrayInputStream(buf);
         ZipInputStream zis = new ZipInputStream(bais)) {

        zis.getNextEntry();
        GZIPInputStream gis1 = limitGISBuff ?
                new GZIPInputStream(zis, 4) :
                new GZIPInputStream(zis);
        // try to read more than the entry has
        gis1.skip(2);

        try {
            zis.getNextEntry();
        } catch (IOException e) {
            throw new RuntimeException("ZIP stream was prematurely closed", e);
        }
    }
}
 
Example 20
Source File: GZIPInZip.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
private static void doTest(final boolean appendGarbage,
                           final boolean limitGISBuff)
        throws Throwable {

    byte[] buf;

    try (ByteArrayOutputStream baos = new ByteArrayOutputStream();
         ZipOutputStream zos = new ZipOutputStream(baos)) {

        final byte[] xbuf = { 'x' };

        zos.putNextEntry(new ZipEntry("a.gz"));
        GZIPOutputStream gos1 = new GZIPOutputStream(zos);
        gos1.write(xbuf);
        gos1.finish();
        if (appendGarbage)
            zos.write(xbuf);
        zos.closeEntry();

        zos.putNextEntry(new ZipEntry("b.gz"));
        GZIPOutputStream gos2 = new GZIPOutputStream(zos);
        gos2.write(xbuf);
        gos2.finish();
        zos.closeEntry();

        zos.flush();
        buf = baos.toByteArray();
    }

    try (ByteArrayInputStream bais = new ByteArrayInputStream(buf);
         ZipInputStream zis = new ZipInputStream(bais)) {

        zis.getNextEntry();
        GZIPInputStream gis1 = limitGISBuff ?
                new GZIPInputStream(zis, 4) :
                new GZIPInputStream(zis);
        // try to read more than the entry has
        gis1.skip(2);

        try {
            zis.getNextEntry();
        } catch (IOException e) {
            throw new RuntimeException("ZIP stream was prematurely closed", e);
        }
    }
}