类java.util.zip.GZIPOutputStream源码实例Demo

下面列出了怎么用java.util.zip.GZIPOutputStream的API类实例代码及写法,或者点击链接到github查看源代码。

源代码1 项目: htmlunit   文件: XMLHttpRequest2Test.java
/**
 * {@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);
}
 
源代码2 项目: netcdf-java   文件: TimeCompression.java
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);
}
 
源代码3 项目: grpc-nebula-java   文件: MessageDeframerTest.java
@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);
        }
      }
    });
  }
}
 
源代码4 项目: htmlunit   文件: DebuggingWebConnectionTest.java
/**
 * 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());
    }
}
 
源代码5 项目: hadoop   文件: WritableUtils.java
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;
  }
}
 
源代码6 项目: hermes   文件: GZipPayloadCodec.java
@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();
}
 
源代码7 项目: cstc   文件: Gzip.java
@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();
  }
 
源代码8 项目: netbeans   文件: SamplesOutputStream.java
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();
}
 
源代码9 项目: hop   文件: CubeOutput.java
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 );
  }
}
 
源代码10 项目: inception   文件: PDFExtractor.java
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());
    }
}
 
/**
 * 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);
}
 
源代码12 项目: burstkit4j   文件: BurstCryptoImpl.java
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);
    }
}
 
源代码13 项目: letv   文件: ac.java
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;
}
 
源代码14 项目: datawave   文件: QueryOptions.java
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()));
}
 
源代码15 项目: Tomcat8-Source-Read   文件: GzipInterceptor.java
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();
}
 
源代码16 项目: Tomcat8-Source-Read   文件: TestGzipOutputFilter.java
@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));
}
 
源代码17 项目: Angelia-core   文件: CompressionManager.java
/**
 * 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();
}
 
源代码18 项目: react-native-GPay   文件: RequestBodyUtil.java
/** 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());
}
 
源代码19 项目: grpc-nebula-java   文件: GzipInflatingBufferTest.java
@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);
  }
}
 
@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);
}
 
源代码21 项目: GotoBrowser   文件: KcUtils.java
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();
}
 
源代码22 项目: krpc   文件: GZip.java
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();
        }
    }
 
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();
}
 
源代码24 项目: bidder   文件: RTBServer.java
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();
}
 
源代码25 项目: BlueMap   文件: Plugin.java
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);
	}
}
 
源代码26 项目: mantis   文件: CompressionUtils.java
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;
}
 
源代码27 项目: bStats-Metrics   文件: MetricsLite2.java
/**
 * 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();
}
 
源代码28 项目: Geyser   文件: Metrics.java
/**
 * 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();
}
 
源代码29 项目: netbeans   文件: Installer.java
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();
    }
}
 
源代码30 项目: PacketProxy   文件: Utils.java
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();
}
 
 类所在包
 同包方法