java.util.zip.GZIPOutputStream Java Examples

The following examples show how to use java.util.zip.GZIPOutputStream. 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: GZipPayloadCodec.java    From hermes with Apache License 2.0 7 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 #2
Source File: SamplesOutputStream.java    From netbeans with Apache License 2.0 7 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 #3
Source File: InventoryReportLineWriter.java    From s3-inventory-usage-examples with Apache License 2.0 7 votes vote down vote up
/**
 * 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 #4
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 #5
Source File: TimeCompression.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
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 #6
Source File: MessageDeframerTest.java    From grpc-nebula-java with 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 #7
Source File: DebuggingWebConnectionTest.java    From htmlunit with 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 #8
Source File: WritableUtils.java    From hadoop with 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 #9
Source File: Gzip.java    From cstc with 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 #10
Source File: CubeOutput.java    From hop with 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 #11
Source File: PDFExtractor.java    From inception with 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 #12
Source File: BurstCryptoImpl.java    From burstkit4j with 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 File: ac.java    From letv with 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 File: QueryOptions.java    From datawave with 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 File: GzipInterceptor.java    From Tomcat8-Source-Read with 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 File: TestGzipOutputFilter.java    From Tomcat8-Source-Read with 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
Source File: CompressionManager.java    From Angelia-core with GNU General Public License v3.0 5 votes vote down vote up
/**
 * 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 File: RequestBodyUtil.java    From react-native-GPay with 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 File: GzipInflatingBufferTest.java    From grpc-nebula-java with 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
Source File: CctTransportBackendTest.java    From firebase-android-sdk with Apache License 2.0 5 votes vote down vote up
@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 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 #22
Source File: GZip.java    From krpc with 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
Source File: EncodedResourceResolverTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
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 File: RTBServer.java    From bidder with 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 File: Plugin.java    From BlueMap with 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 File: CompressionUtils.java    From mantis with 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
Source File: MetricsLite2.java    From bStats-Metrics with GNU Lesser General Public License v3.0 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();
    try (GZIPOutputStream gzip = new GZIPOutputStream(outputStream)) {
        gzip.write(str.getBytes(StandardCharsets.UTF_8));
    }
    return outputStream.toByteArray();
}
 
Example #28
Source File: Metrics.java    From Geyser with 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 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 #30
Source File: Utils.java    From PacketProxy with 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();
}