[英]Java gzip - GZIPOutputStream not closing / compressing arbitrarily
借助该线程,我能够编写一个decompress()
和compress()
函数。 我的程序以gzip压缩形式接收数据,对其进行膨胀,有时对其进行修改,然后再次对其进行重新压缩并发送。 经过数小时的头痛和错误跟踪,我发现有时候(!) ,我使用的GZIPOutputStream
无法正确关闭。 我可以冲洗它,但是此后什么也没发生。 但是,在其他时候,它只是可以正常工作。
这是我的compress()
方法的代码。
public static byte[] compress(String data) throws IOException {
try (PipedInputStream pipedIn = new PipedInputStream();
GZIPOutputStream compressor= new GZIPOutputStream(new PipedOutputStream(pipedIn))) {
compressor.write(data.getBytes("UTF-8"));
compressor.flush();
compressor.close();
// Sometimes does not reach this point.
return IOUtils.toByteArray(pipedIn);
}
}
为了调试,我添加了一些System.Outs。 控制台输出如下:
------------------------------------
Decompressing ...
compressed byte count: 628
uncompressed byte count: 1072
------------------------------------
Compressing ...
uncompressed byte count: 1072
compressed byte count: 628
------------------------------------
>>> That worked!
------------------------------------
Decompressing ...
compressed byte count: 526
uncompressed byte count: 2629
------------------------------------
uncompressed byte count: 2629
compressed byte count: 526
------------------------------------
>>> That worked!
------------------------------------
Decompressing ...
compressed byte count: 1888
uncompressed byte count: 6254
------------------------------------
在那之后,什么也没有发生。 任何有关此问题的帮助将不胜感激!
您对GZIP流的使用可能没有错; 而是使用管道流的方式。 这些是用于线程间通信的,在您按自己的方式使用它们时会阻塞。
而是使用ByteArrayOutptuStream
捕获输出。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.