Java Code Examples for java.util.zip.GZIPOutputStream

The following examples show how to use java.util.zip.GZIPOutputStream. These examples are extracted from open source projects. 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 Project: htmlunit   Source File: XMLHttpRequest2Test.java    License: 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 2
static public void gzipRandom(String filenameOut, boolean buffer) throws IOException {
  FileOutputStream fout = new FileOutputStream(filenameOut);
  OutputStream out = new GZIPOutputStream(fout);
  if (buffer)
    out = new BufferedOutputStream(out, 1000); // 3X performance by having this !!
  DataOutputStream dout = new DataOutputStream(out);

  Random r = new Random();
  long start = System.currentTimeMillis();
  for (int i = 0; i < (1000 * 1000); i++) {
    dout.writeFloat(r.nextFloat());
  }
  fout.flush();
  fout.close();
  double took = .001 * (System.currentTimeMillis() - start);
  File f = new File(filenameOut);
  long len = f.length();
  double ratio = 4 * 1000.0 * 1000.0 / len;
  System.out.println(" gzipRandom took = " + took + " sec; compress = " + ratio);
}
 
Example 3
Source Project: grpc-nebula-java   Source File: MessageDeframerTest.java    License: Apache License 2.0 6 votes vote down vote up
@Before
public void setUp() {
  if (useGzipInflatingBuffer) {
    deframer.setFullStreamDecompressor(new GzipInflatingBuffer() {
      @Override
      public void addGzippedBytes(ReadableBuffer buffer) {
        try {
          ByteArrayOutputStream gzippedOutputStream = new ByteArrayOutputStream();
          OutputStream gzipCompressingStream = new GZIPOutputStream(
                  gzippedOutputStream);
          buffer.readBytes(gzipCompressingStream, buffer.readableBytes());
          gzipCompressingStream.close();
          super.addGzippedBytes(ReadableBuffers.wrap(gzippedOutputStream.toByteArray()));
        } catch (IOException e) {
          throw new RuntimeException(e);
        }
      }
    });
  }
}
 
Example 4
Source Project: htmlunit   Source File: DebuggingWebConnectionTest.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Ensures that Content-Encoding headers are removed when JavaScript is uncompressed.
 * (was causing java.io.IOException: Not in GZIP format).
 * @throws Exception if the test fails
 */
@Test
public void gzip() throws Exception {
    final ByteArrayOutputStream baos = new ByteArrayOutputStream();
    try (GZIPOutputStream gzipOutputStream = new GZIPOutputStream(baos)) {
        IOUtils.write("alert(1)", gzipOutputStream, UTF_8);
    }

    final MockWebConnection mockConnection = new MockWebConnection();
    final List<NameValuePair> responseHeaders = Arrays.asList(
        new NameValuePair("Content-Encoding", "gzip"));
    mockConnection.setResponse(URL_FIRST, baos.toByteArray(), 200, "OK", MimeType.APPLICATION_JAVASCRIPT,
        responseHeaders);

    final String dirName = "test-" + getClass().getSimpleName();
    try (DebuggingWebConnection dwc = new DebuggingWebConnection(mockConnection, dirName)) {

        final WebRequest request = new WebRequest(URL_FIRST);
        final WebResponse response = dwc.getResponse(request); // was throwing here
        assertNull(response.getResponseHeaderValue("Content-Encoding"));

        FileUtils.deleteDirectory(dwc.getReportFolder());
    }
}
 
Example 5
Source Project: hadoop   Source File: WritableUtils.java    License: Apache License 2.0 6 votes vote down vote up
public static int  writeCompressedByteArray(DataOutput out, 
                                            byte[] bytes) throws IOException {
  if (bytes != null) {
    ByteArrayOutputStream bos =  new ByteArrayOutputStream();
    GZIPOutputStream gzout = new GZIPOutputStream(bos);
    try {
      gzout.write(bytes, 0, bytes.length);
      gzout.close();
      gzout = null;
    } finally {
      IOUtils.closeStream(gzout);
    }
    byte[] buffer = bos.toByteArray();
    int len = buffer.length;
    out.writeInt(len);
    out.write(buffer, 0, len);
    /* debug only! Once we have confidence, can lose this. */
    return ((bytes.length != 0) ? (100*buffer.length)/bytes.length : 0);
  } else {
    out.writeInt(-1);
    return -1;
  }
}
 
Example 6
Source Project: hermes   Source File: GZipPayloadCodec.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public byte[] doEncode(String topic, Object obj) {
	if (!(obj instanceof byte[])) {
		throw new IllegalArgumentException(String.format("Can not encode input of type %s",
		      obj == null ? "null" : obj.getClass()));
	}

	ByteArrayInputStream input = new ByteArrayInputStream((byte[]) obj);
	ByteArrayOutputStream bout = new ByteArrayOutputStream();
	try {
		GZIPOutputStream gout = new GZIPOutputStream(bout);
		IO.INSTANCE.copy(input, gout, AutoClose.INPUT_OUTPUT);
	} catch (IOException e) {
		throw new RuntimeException(String.format("Unexpected exception when encode %s of topic %s", obj, topic), e);
	}

	return bout.toByteArray();
}
 
Example 7
Source Project: cstc   Source File: Gzip.java    License: GNU General Public License v3.0 6 votes vote down vote up
@Override
  protected byte[] perform(byte[] input) throws Exception {
  	ByteArrayOutputStream out = new ByteArrayOutputStream();
  	GZIPOutputStream gzos = new GZIPOutputStream(out);     
  	ByteArrayInputStream in = new ByteArrayInputStream(input);
      
      byte[] buffer = new byte[1024];   
      int len;
      while ((len = in.read(buffer)) > 0) {
      	gzos.write(buffer, 0, len);
      }
      
in.close();
gzos.close();
out.close();
      return out.toByteArray();
  }
 
Example 8
Source Project: netbeans   Source File: SamplesOutputStream.java    License: Apache License 2.0 6 votes vote down vote up
void close() throws IOException {
    steCache = null;
    GZIPOutputStream stream = new GZIPOutputStream(outStream, 64 * 1024);
    ObjectOutputStream out = new ObjectOutputStream(stream);
    int size = samples.size();
    out.writeInt(size);
    out.writeLong(getSample(size-1).getTime());
    openProgress();
    for (int i=0; i<size;i++) {
        Sample s = getSample(i);
        removeSample(i);
        if (i > 0 && i % RESET_THRESHOLD == 0) {
            out.reset();
        }
        s.writeToStream(out);
        if ((i+40) % 50 == 0) step((STEPS*i)/size);
    }
    step(STEPS); // set progress at 100%
    out.close();
    closeProgress();
}
 
Example 9
Source Project: hop   Source File: CubeOutput.java    License: Apache License 2.0 6 votes vote down vote up
private void prepareFile() throws HopFileException {
  try {
    String filename = environmentSubstitute( meta.getFilename() );
    if ( meta.isAddToResultFiles() ) {
      // Add this to the result file names...
      ResultFile resultFile =
        new ResultFile(
          ResultFile.FILE_TYPE_GENERAL, HopVfs.getFileObject( filename ), getPipelineMeta()
          .getName(), getTransformName() );
      resultFile.setComment( "This file was created with a cube file output transform" );
      addResultFile( resultFile );
    }

    data.fos = HopVfs.getOutputStream( filename, false );
    data.zip = new GZIPOutputStream( data.fos );
    data.dos = new DataOutputStream( data.zip );
  } catch ( Exception e ) {
    throw new HopFileException( e );
  }
}
 
Example 10
Source Project: inception   Source File: PDFExtractor.java    License: Apache License 2.0 6 votes vote down vote up
private static void processFile(File file)
{
    String outPath = String.format("%s.0-3-1.txt.gz", file);
    try (PDDocument doc = PDDocument.load(file);
        GZIPOutputStream gzip = new GZIPOutputStream(new FileOutputStream(outPath));
        Writer w = new BufferedWriter(new OutputStreamWriter(gzip, "UTF-8"));
    ) {
        for (int i = 0; i < doc.getNumberOfPages(); i++) {
            PDFExtractor ext = new PDFExtractor(doc.getPage(i), i + 1, w);
            ext.processPage(doc.getPage(i));
            ext.write();
        }
    }
    catch (Exception e) {
        System.out.println(e.getMessage());
    }
}
 
Example 11
/**
 * Write a new inventory report to S3 and returns a locator which includes this inventory report's information
 * @return Locator which includes the information of this new report
 * @throws IOException thrown when GZIPOutputStream not created successfully or csvMapper.write() fails
 */
public InventoryManifest.Locator writeCsvFile(List<InventoryReportLine> inventoryReportLine) throws IOException{
    CsvMapper csvMapper = new CsvMapper();
    csvMapper.enable(JsonGenerator.Feature.IGNORE_UNKNOWN);
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    GZIPOutputStream gzipOutputStream = new GZIPOutputStream(byteArrayOutputStream);
    csvMapper.writer(schema).writeValues(gzipOutputStream).writeAll(inventoryReportLine).close();
    byte[] zipByteArray = byteArrayOutputStream.toByteArray();

    InputStream zipInputStream = new ByteArrayInputStream(zipByteArray);
    ObjectMetadata metaData = new ObjectMetadata();
    metaData.setContentLength(zipByteArray.length);
    PutObjectRequest request = new PutObjectRequest(bucketName, outputInventoryReportKey, zipInputStream, metaData);
    s3Client.putObject(request);

    return this.buildLocator(zipByteArray);
}
 
Example 12
Source Project: burstkit4j   Source File: BurstCryptoImpl.java    License: Apache License 2.0 6 votes vote down vote up
private BurstEncryptedMessage encryptPlainMessage(byte[] message, boolean isText, byte[] myPrivateKey, byte[] theirPublicKey) {
    if (message.length == 0) {
        return new BurstEncryptedMessage(new byte[0], new byte[0], isText);
    }
    try (ByteArrayOutputStream bos = new ByteArrayOutputStream()) {
        GZIPOutputStream gzip = new GZIPOutputStream(bos);
        gzip.write(message);
        gzip.flush();
        gzip.close();
        byte[] compressedPlaintext = bos.toByteArray();
        byte[] nonce = new byte[32];
        secureRandom.nextBytes(nonce);
        byte[] data = aesSharedEncrypt(compressedPlaintext, myPrivateKey, theirPublicKey, nonce);
        return new BurstEncryptedMessage(data, nonce, isText);
    } catch (IOException e) {
        throw new RuntimeException(e.getMessage(), e);
    }
}
 
Example 13
Source Project: letv   Source File: ac.java    License: Apache License 2.0 6 votes vote down vote up
private static String c(String str) {
    String str2 = null;
    try {
        byte[] bytes = str.getBytes(z[5]);
        OutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        OutputStream gZIPOutputStream = new GZIPOutputStream(byteArrayOutputStream);
        gZIPOutputStream.write(bytes);
        gZIPOutputStream.close();
        bytes = byteArrayOutputStream.toByteArray();
        byteArrayOutputStream.close();
        str2 = a.a(bytes);
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (IOException e2) {
        e2.printStackTrace();
    }
    return str2;
}
 
Example 14
Source Project: datawave   Source File: QueryOptions.java    License: Apache License 2.0 6 votes vote down vote up
public static String compressOption(final String data, final Charset characterSet) throws IOException {
    final ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
    final GZIPOutputStream gzipStream = new GZIPOutputStream(byteStream);
    final DataOutputStream dataOut = new DataOutputStream(gzipStream);
    
    byte[] arr = data.getBytes(characterSet);
    final int length = arr.length;
    
    dataOut.writeInt(length);
    dataOut.write(arr);
    
    dataOut.close();
    byteStream.close();
    
    return new String(Base64.encodeBase64(byteStream.toByteArray()));
}
 
Example 15
Source Project: Tomcat8-Source-Read   Source File: GzipInterceptor.java    License: MIT License 5 votes vote down vote up
public static byte[] compress(byte[] data) throws IOException {
    ByteArrayOutputStream bout = new ByteArrayOutputStream();
    GZIPOutputStream gout = new GZIPOutputStream(bout);
    gout.write(data);
    gout.flush();
    gout.close();
    return bout.toByteArray();
}
 
Example 16
Source Project: Tomcat8-Source-Read   Source File: TestGzipOutputFilter.java    License: MIT License 5 votes vote down vote up
@Test
public void testFlushingWithGzip() throws Exception {
    // set up response, InternalOutputBuffer, and ByteArrayOutputStream
    Response res = new Response();
    TesterOutputBuffer tob = new TesterOutputBuffer(res, 8 * 1024);
    res.setOutputBuffer(tob);

    // set up GzipOutputFilter to attach to the TesterOutputBuffer
    GzipOutputFilter gf = new GzipOutputFilter();
    tob.addFilter(gf);
    tob.addActiveFilter(gf);

    // write a chunk out
    byte[] d = "Hello there tomcat developers, there is a bug in JDK".getBytes();
    ByteBuffer bb = ByteBuffer.wrap(d);
    tob.doWrite(bb);

    // flush the InternalOutputBuffer
    tob.flush();

    // read from the ByteArrayOutputStream to find out what's being written
    // out (flushed)
    byte[] dataFound = tob.toByteArray();

    // find out what's expected by writing to GZIPOutputStream and close it
    // (to force flushing)
    ByteArrayOutputStream gbos = new ByteArrayOutputStream(1024);
    GZIPOutputStream gos = new GZIPOutputStream(gbos);
    gos.write(d);
    gos.close();

    // read the expected data
    byte[] dataExpected = gbos.toByteArray();

    // most of the data should have been flushed out
    Assert.assertTrue(dataFound.length >= (dataExpected.length - 20));
}
 
Example 17
/**
 * Compresses the given data with gzip
 * 
 * @param data Data to compress
 * @return Compressed data
 * @throws IOException In case something goes wrong with the stream internally
 *                     used
 */
public static byte[] compressGZip(final byte[] data) throws IOException {
	if (data == null) {
		return null;
	}
	if (data.length == 0) {
		return new byte[0];
	}
	ByteArrayOutputStream obj = new ByteArrayOutputStream();
	GZIPOutputStream gzip = new GZIPOutputStream(obj);
	gzip.write(data);
	gzip.flush();
	gzip.close();
	return obj.toByteArray();
}
 
Example 18
Source Project: react-native-GPay   Source File: RequestBodyUtil.java    License: MIT License 5 votes vote down vote up
/** Creates a RequestBody from a mediaType and gzip-ed body string */
public static @Nullable RequestBody createGzip(final MediaType mediaType, final String body) {
  ByteArrayOutputStream gzipByteArrayOutputStream = new ByteArrayOutputStream();
  try {
    OutputStream gzipOutputStream = new GZIPOutputStream(gzipByteArrayOutputStream);
    gzipOutputStream.write(body.getBytes());
    gzipOutputStream.close();
  } catch (IOException e) {
    return null;
  }
  return RequestBody.create(mediaType, gzipByteArrayOutputStream.toByteArray());
}
 
Example 19
Source Project: grpc-nebula-java   Source File: GzipInflatingBufferTest.java    License: Apache License 2.0 5 votes vote down vote up
@Before
public void setUp() {
  gzipInflatingBuffer = new GzipInflatingBuffer();
  try {
    originalData = ByteStreams.toByteArray(getClass().getResourceAsStream(UNCOMPRESSABLE_FILE));
    truncatedData = Arrays.copyOf(originalData, TRUNCATED_DATA_SIZE);

    ByteArrayOutputStream gzippedOutputStream = new ByteArrayOutputStream();
    OutputStream gzippingOutputStream = new GZIPOutputStream(gzippedOutputStream);
    gzippingOutputStream.write(originalData);
    gzippingOutputStream.close();
    gzippedData = gzippedOutputStream.toByteArray();
    gzippedOutputStream.close();

    gzipHeader = Arrays.copyOf(gzippedData, GZIP_HEADER_MIN_SIZE);
    deflatedBytes =
        Arrays.copyOfRange(
            gzippedData, GZIP_HEADER_MIN_SIZE, gzippedData.length - GZIP_TRAILER_SIZE);
    gzipTrailer =
        Arrays.copyOfRange(
            gzippedData, gzippedData.length - GZIP_TRAILER_SIZE, gzippedData.length);

    ByteArrayOutputStream truncatedGzippedOutputStream = new ByteArrayOutputStream();
    OutputStream smallerGzipCompressingStream =
        new GZIPOutputStream(truncatedGzippedOutputStream);
    smallerGzipCompressingStream.write(truncatedData);
    smallerGzipCompressingStream.close();
    gzippedTruncatedData = truncatedGzippedOutputStream.toByteArray();
    truncatedGzippedOutputStream.close();
  } catch (Exception e) {
    throw new RuntimeException("Failed to set up compressed data", e);
  }
}
 
Example 20
@Test
public void send_CompressedResponseIsUncompressed() throws IOException {
  ByteArrayOutputStream output = new ByteArrayOutputStream();
  GZIPOutputStream gzipOutputStream = new GZIPOutputStream(output);
  gzipOutputStream.write("{\"nextRequestWaitMillis\":3}".getBytes(Charset.forName("UTF-8")));
  gzipOutputStream.close();

  stubFor(
      post(urlEqualTo("/api"))
          .willReturn(
              aResponse()
                  .withStatus(200)
                  .withHeader("Content-Type", "application/json;charset=UTF8;hello=world")
                  .withHeader("Content-Encoding", "gzip")
                  .withBody(output.toByteArray())));

  BackendRequest backendRequest = getCCTBackendRequest();
  wallClock.tick();
  uptimeClock.tick();

  BackendResponse response = BACKEND.send(backendRequest);

  verify(
      postRequestedFor(urlEqualTo("/api"))
          .withHeader("Content-Type", equalTo("application/json"))
          .withHeader("Content-Encoding", equalTo("gzip")));

  assertEquals(BackendResponse.ok(3), response);
}
 
Example 21
Source Project: GotoBrowser   Source File: KcUtils.java    License: 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 22
Source Project: krpc   Source File: GZip.java    License: Apache License 2.0 5 votes vote down vote up
public void zip(byte[] in, ByteBuf out) throws IOException {

        try (ByteBufOutputStream bos = new ByteBufOutputStream(out);
             GZIPOutputStream gzip = new GZIPOutputStream(bos);) {
            gzip.write(in);
            gzip.finish();
        }
    }
 
Example 23
static void createGzippedFile(String filePath) throws IOException {
	Resource location = new ClassPathResource("test/", EncodedResourceResolverTests.class);
	Resource resource = new FileSystemResource(location.createRelative(filePath).getFile());

	Path gzFilePath = Paths.get(resource.getFile().getAbsolutePath() + ".gz");
	Files.deleteIfExists(gzFilePath);

	File gzFile = Files.createFile(gzFilePath).toFile();
	GZIPOutputStream out = new GZIPOutputStream(new FileOutputStream(gzFile));
	FileCopyUtils.copy(resource.getInputStream(), out);
	gzFile.deleteOnExit();
}
 
Example 24
Source Project: bidder   Source File: RTBServer.java    License: Apache License 2.0 5 votes vote down vote up
private static byte[] compressGZip(String uncompressed) throws Exception {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    GZIPOutputStream gzos = new GZIPOutputStream(baos);

    byte[] uncompressedBytes = uncompressed.getBytes();

    gzos.write(uncompressedBytes, 0, uncompressedBytes.length);
    gzos.close();

    return baos.toByteArray();
}
 
Example 25
Source Project: BlueMap   Source File: Plugin.java    License: MIT License 5 votes vote down vote up
public void saveRenderManagerState() throws IOException {
	File saveFile = config.getDataPath().resolve("rmstate").toFile();
	saveFile.getParentFile().mkdirs();
	if (saveFile.exists()) saveFile.delete();
	saveFile.createNewFile();
	
	try (DataOutputStream out = new DataOutputStream(new GZIPOutputStream(new FileOutputStream(saveFile)))) {
		renderManager.writeState(out);
	}
}
 
Example 26
Source Project: mantis   Source File: CompressionUtils.java    License: Apache License 2.0 5 votes vote down vote up
static byte[] gzipCompressData(String data) throws IOException, UnsupportedEncodingException {
    ByteArrayOutputStream obj = new ByteArrayOutputStream();
    GZIPOutputStream gzip = new GZIPOutputStream(obj);
    gzip.write(data.getBytes("UTF-8"));
    gzip.close();
    byte[] compressedBytes = obj.toByteArray();
    return compressedBytes;
}
 
Example 27
/**
 * Gzips the given String.
 *
 * @param str The string to gzip.
 * @return The gzipped String.
 * @throws IOException If the compression failed.
 */
private static byte[] compress(final String str) throws IOException {
    if (str == null) {
        return null;
    }
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    try (GZIPOutputStream gzip = new GZIPOutputStream(outputStream)) {
        gzip.write(str.getBytes(StandardCharsets.UTF_8));
    }
    return outputStream.toByteArray();
}
 
Example 28
Source Project: Geyser   Source File: Metrics.java    License: MIT License 5 votes vote down vote up
/**
 * Gzips the given String.
 *
 * @param str The string to gzip.
 * @return The gzipped String.
 * @throws IOException If the compression failed.
 */
private static byte[] compress(final String str) throws IOException {
    if (str == null) {
        return null;
    }
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    GZIPOutputStream gzip = new GZIPOutputStream(outputStream);
    gzip.write(str.getBytes("UTF-8"));
    gzip.close();
    return outputStream.toByteArray();
}
 
Example 29
Source Project: netbeans   Source File: Installer.java    License: 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 30
Source Project: PacketProxy   Source File: Utils.java    License: Apache License 2.0 5 votes vote down vote up
public static byte[] gzip(byte[] src) throws Exception {
	ByteArrayOutputStream out = new ByteArrayOutputStream();
	GZIPOutputStream gzos = new GZIPOutputStream(out);
	gzos.write(src);
	gzos.flush();
	gzos.finish();
	return out.toByteArray();
}