このスレッドの助けを借りて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.Out を追加しました。コンソール出力は次のとおりです。
------------------------------------
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
------------------------------------
その後、何も起こりません。この問題の助けをいただければ幸いです。