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

The following examples show how to use java.util.zip.GZIPOutputStream#flush() . 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: 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 2
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 3
Source File: StreamingIngestClientTest.java    From azure-kusto-java with MIT License 6 votes vote down vote up
@Test
void IngestFromStream_CompressedCsvStream() throws Exception {
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    GZIPOutputStream gzipOutputStream = new GZIPOutputStream(byteArrayOutputStream);
    String data = "Name, Age, Weight, 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);
    // When ingesting compressed data, we should set this property true to avoid double compression.
    streamSourceInfo.setCompressionType(CompressionType.gz);
    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), isNull(), any(boolean.class));

    InputStream stream = argumentCaptor.getValue();
    verifyCompressedStreamContent(stream, data);
}
 
Example 4
Source File: StreamingIngestClient.java    From azure-kusto-java with MIT License 6 votes vote down vote up
private InputStream compressStream(InputStream uncompressedStream, boolean leaveOpen) throws IngestionClientException, IOException {
    log.debug("Compressing the stream.");
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    GZIPOutputStream gzipOutputStream = new GZIPOutputStream(byteArrayOutputStream);
    byte[] b = new byte[STREAM_COMPRESS_BUFFER_SIZE];
    int read = uncompressedStream.read(b);
    if (read == -1) {
        String message = "Empty stream.";
        log.error(message);
        throw new IngestionClientException(message);
    }
    do {
        gzipOutputStream.write(b, 0, read);
    } while ((read = uncompressedStream.read(b)) != -1);
    gzipOutputStream.flush();
    gzipOutputStream.close();
    InputStream inputStream = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
    byteArrayOutputStream.close();
    if (!leaveOpen) {
        uncompressedStream.close();
    }
    return inputStream;
}
 
Example 5
Source File: GZipUtils.java    From Xndroid with GNU General Public License v3.0 6 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);

    int count;
    byte data[] = new byte[BUFFER];
    while ((count = is.read(data, 0, BUFFER)) != -1) {
        gos.write(data, 0, count);
    }

    gos.finish();

    gos.flush();
    gos.close();
}
 
Example 6
Source File: CommonIOUtils.java    From freehealth-connector with GNU Affero General Public License v3.0 5 votes vote down vote up
public static byte[] compress(byte[] input) throws IOException {
   long size = (long)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.info("Compression of data from " + size + " bytes to " + ret.length + " bytes");
   return ret;
}
 
Example 7
Source File: GzipInterceptor.java    From Tomcat7.0.67 with Apache License 2.0 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 8
Source File: GzipUtils.java    From nem-apps-lib with MIT License 5 votes vote down vote up
/**
 * Compress.
 *
 * @param str the str
 * @return the byte[]
 * @throws IOException Signals that an I/O exception has occurred.
 */
public static byte[] compress(final String str) throws IOException {
	if ((str == null) || (str.length() == 0)) {
		return null;
	}
	ByteArrayOutputStream obj = new ByteArrayOutputStream();
	GZIPOutputStream gzip = new GZIPOutputStream(obj);
	gzip.write(str.getBytes("UTF-8"));
	gzip.flush();
	gzip.close();
	return obj.toByteArray();
}
 
Example 9
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 10
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 11
Source File: Compressor.java    From houdoku with MIT License 5 votes vote down vote up
/**
 * Compress an array of bytes.
 *
 * @param bytes the bytes to compress
 * @return the given bytes compressed
 */
static byte[] compress(byte[] bytes) {
    ByteArrayOutputStream result = new ByteArrayOutputStream();
    try {
        GZIPOutputStream gzip = new GZIPOutputStream(result);
        gzip.write(bytes);
        gzip.flush();
        gzip.close();
    } catch (IOException e) {
        // realistically, this should probably never happen, since we are
        // simply using data from a local variable
        e.printStackTrace();
    }
    return result.toByteArray();
}
 
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: 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 14
Source File: CommonIOUtils.java    From freehealth-connector with GNU Affero General Public License v3.0 5 votes vote down vote up
public static byte[] compress(byte[] input) throws IOException {
   long size = (long)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.info("Compression of data from " + size + " bytes to " + ret.length + " bytes");
   return ret;
}
 
Example 15
Source File: IOUtils.java    From freehealth-connector with GNU Affero General Public License v3.0 5 votes vote down vote up
public static byte[] compress(byte[] input) throws IOException {
   long size = (long)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 16
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 17
Source File: TestZip.java    From util4j with Apache License 2.0 5 votes vote down vote up
public static byte[] gzipData(byte[] data) {
	ByteArrayOutputStream bos=new ByteArrayOutputStream();
	try {
		GZIPOutputStream gos=new GZIPOutputStream(bos);
		gos.write(data);
		gos.flush();
		gos.close();
	} catch (Exception e) {
	}
	byte[] zip=bos.toByteArray();
	return zip;
}
 
Example 18
Source File: ClassCache.java    From OptiFabric with Mozilla Public License 2.0 5 votes vote down vote up
public void save(File output) throws IOException {
	if(output.exists()){
		output.delete();
	}
	FileOutputStream fos = new FileOutputStream(output);
	GZIPOutputStream gos = new GZIPOutputStream(fos);
	DataOutputStream dos = new DataOutputStream(gos);

	//Write the hash
	dos.writeInt(hash.length);
	dos.write(hash);

	//Write the number of classes
	dos.writeInt(classes.size());
	for(Map.Entry<String, byte[]> clazz : classes.entrySet()){
		String name = clazz.getKey();
		byte[] nameBytes = name.getBytes(StandardCharsets.UTF_8);
		byte[] bytes = clazz.getValue();

		//Write the name
		dos.writeInt(nameBytes.length);
		dos.write(nameBytes);

		//Write the actual bytes
		dos.writeInt(bytes.length);
		dos.write(bytes);
	}
	dos.flush();
	dos.close();
	gos.flush();
	gos.close();
	fos.flush();
	fos.close();
}
 
Example 19
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();
}
 
Example 20
Source File: GzipCompressor.java    From yosegi with Apache License 2.0 5 votes vote down vote up
@Override
public byte[] compress(
    final byte[] data ,
    final int start ,
    final int length ,
    final CompressResult compressResult ) throws IOException {
  int level = getCompressLevel( compressResult.getCompressionPolicy() ); 
  int optLevel = compressResult.getCurrentLevel();
  if ( ( level - optLevel ) < 1 ) {
    compressResult.setEnd();
    optLevel = compressResult.getCurrentLevel();
  }

  int setLevel = level - optLevel;
  ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();
  GZIPOutputStream out = new GZIPOutputStream( byteArrayOut ) {
    {
      this.def.setLevel( setLevel );
    }
  };

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

  byteArrayOut.close();
  out.close();

  compressResult.feedBack( length , compressByte.length );

  return retVal;
}