在this thread的帮助下,我能够编写一个decompress()和一个compress()函数。我的程序以again格式接收数据,对其进行充气,有时对其进行修改,然后再对其进行重新压缩,并将其发送出去。经过数小时的头痛和bug跟踪,我发现有时(!),我使用的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
------------------------------------在那之后什么都没发生。任何对这个问题的帮助都将不胜感激!
发布于 2015-10-27 13:41:42
使用GZIP流可能没有什么问题,而是您使用管道流的方式。它们是用于线程间通信的,当您使用它们时,它们会被阻塞。
相反,使用ByteArrayOutptuStream来捕获输出。
https://stackoverflow.com/questions/33369459
复制相似问题