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

The following examples show how to use java.util.zip.GZIPOutputStream#write() . 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: StreamingIngestClientTest.java    From azure-kusto-java with MIT License 6 votes vote down vote up
@Test
void IngestFromStream_CompressedJsonStream() throws Exception {
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    GZIPOutputStream gzipOutputStream = new GZIPOutputStream(byteArrayOutputStream);
    String data = "{\"Name\": \"name\", \"Age\": \"age\", \"Weight\": \"weight\", \"Height\": \"height\"}";
    byte[] inputArray = Charset.forName("UTF-8").encode(data).array();
    gzipOutputStream.write(inputArray, 0, inputArray.length);
    gzipOutputStream.flush();
    gzipOutputStream.close();
    InputStream inputStream = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
    StreamSourceInfo streamSourceInfo = new StreamSourceInfo(inputStream);
    streamSourceInfo.setCompressionType(CompressionType.gz);
    ingestionProperties.setIngestionMapping("JsonMapping", IngestionMapping.IngestionMappingKind.Json);
    OperationStatus status = streamingIngestClient.ingestFromStream(streamSourceInfo, ingestionProperties).getIngestionStatusCollection().get(0).status;
    assertEquals(status, OperationStatus.Succeeded);
    verify(streamingClientMock, atLeastOnce()).executeStreamingIngest(any(String.class), any(String.class), argumentCaptor.capture(),
            isNull(), any(String.class), any(String.class), any(boolean.class));

    InputStream stream = argumentCaptor.getValue();
    verifyCompressedStreamContent(stream, data);
}
 
Example 2
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 3
Source File: GZipUtil.java    From pmq with Apache License 2.0 6 votes vote down vote up
public static String compress(String str) {
	if ((str == null) || (str.isEmpty())) {
		return "";
	}
	ByteArrayOutputStream out = new ByteArrayOutputStream();
	try {
		GZIPOutputStream gzip = new GZIPOutputStream(out);
		gzip.write(str.getBytes("UTF-8"));
		gzip.flush();
		gzip.close();
		byte[] tArray = out.toByteArray();
		out.close();
		BASE64Encoder tBase64Encoder = new BASE64Encoder();
		return tBase64Encoder.encode(tArray);
	} catch (Exception ex) {
		logger.error("压缩异常,异常信息:" + ex.getMessage());
	}
	return str;
}
 
Example 4
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 5
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 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: GzipTool.java    From netstrap with Apache License 2.0 5 votes vote down vote up
/**
 * 压缩
 */
public static byte[] compress(byte[] data) throws IOException {
    if (data == null || data.length == 0) {
        return null;
    }
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    GZIPOutputStream gzip = new GZIPOutputStream(out);
    gzip.write(data);
    gzip.close();
    return out.toByteArray();
}
 
Example 8
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 9
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 10
Source File: GzipCompressionInputStreamTest.java    From hop with Apache License 2.0 5 votes vote down vote up
protected InputStream createGZIPInputStream() throws IOException {
  // Create an in-memory GZIP output stream for use by the input stream (to avoid exceptions)
  ByteArrayOutputStream baos = new ByteArrayOutputStream();
  GZIPOutputStream gos = new GZIPOutputStream( baos );
  byte[] testBytes = "Test".getBytes();
  gos.write( testBytes );
  ByteArrayInputStream in = new ByteArrayInputStream( baos.toByteArray() );
  return in;
}
 
Example 11
Source File: GzipUtil.java    From Deta_DataBase with Apache License 2.0 5 votes vote down vote up
public static byte[] compress(byte[] data) throws IOException {
	if (data == null || data.length == 0) {
		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 12
Source File: IOUtils.java    From freehealth-connector with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * Compress.
 *
 * @param input
 *            the input
 * @return the byte[]
 * @throws IOException
 *             Signals that an I/O exception has occurred.
 */
public static byte[] compress(byte[] input) throws IOException {
	long size = input.length;
	ByteArrayOutputStream outstream = new ByteArrayOutputStream();
	GZIPOutputStream out = new GZIPOutputStream(outstream);
	out.write(input);
	out.flush();
	outstream.flush();
	out.close();
	outstream.close();
	byte[] ret = outstream.toByteArray();
	LOG.debug("Compression of data from " + size + " bytes to " + ret.length + " bytes");
	return ret;
}
 
Example 13
Source File: Metrics.java    From TAB with Apache License 2.0 5 votes vote down vote up
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 14
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 15
Source File: MetricsLite.java    From skript-yaml 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 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: GZIPInZip.java    From dragonwell8_jdk 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 18
Source File: GZIPInZip.java    From TencentKona-8 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: Message.java    From rpcx-java with Apache License 2.0 4 votes vote down vote up
private byte[] compress() throws IOException {
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    GZIPOutputStream zipStream = new GZIPOutputStream(bos);
    zipStream.write(payload);
    return bos.toByteArray();
}
 
Example 20
Source File: Zipping.java    From Image-Steganography-Library-Android with MIT License 3 votes vote down vote up
public static byte[] compress(String string) throws Exception {

        ByteArrayOutputStream os = new ByteArrayOutputStream(string.length());

        GZIPOutputStream gos = new GZIPOutputStream(os);

        gos.write(string.getBytes());
        gos.close();

        byte[] compressed = os.toByteArray();
        os.close();

        return compressed;
    }