我正在使用Deflater通过网络发送一些消息。

我在用着:

public static byte[] compress(byte[] data,int level) throws IOException {

     Instant starts = Instant.now();

     Deflater deflater = new Deflater();
     deflater.setInput(data);
     deflater.setLevel(/*level*/9);

     ByteArrayOutputStream outputStream = new ByteArrayOutputStream(data.length);
     deflater.finish();

     byte[] buffer = new byte[1024];
     while (!deflater.finished()) {
          int count = deflater.deflate(buffer); // returns the generated code... index
          outputStream.write(buffer, 0, count);
     }
     outputStream.close();
     byte[] output = outputStream.toByteArray();
     System.out.println("Original: " + data.length / 1024 + " Kb");
     System.out.println("Compressed: " + output.length / 1024 + " Kb");

     Instant ends = Instant.now();
     System.out.println("deflating time:"+Duration.between(starts, ends));

     return output;
}


压缩通用字节数组,

和:

msg.setDataString(Base64.getEncoder().encodeToString(data));


将对象嵌入到xml中。

一切运行良好一段时间,但今天使用调试器,我看到了:

msg.dataString


(这是一组几乎无效的图像,如rgb pixel(3byte)数组)

eNrt3e1i4jYQBdCR3/+hu+XTYM1I2IEku+fUbbdRDARMf1wmVxEAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA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+UWm5a20Q0OTu6Gm9ns+331SF1MnbdHfnzX5PXuTwGO9Oesb7nzVKzW
OgH0tzxTh+73C57F+ncf0tViw4LLWykpVwIAAAAAfprdLRltbrV7j6th387JRej9mLe/GoxH
Of883/Bd32/1kF7dPfTrYuYqM09z4OTcde/K6DOR7rlvCeSjrBSKGP24R5/ifb83UV110Qbv
hZC9AwAAAMBnHemdbhPHy3fcZufb66R3V+59sGV9ZoZ5x/0eiZ9jVEh+K1rp5N7lapWox/A1
SpL1Ud4e4zb85O4vgXuyl+7h3Lvt+wwoDr2C1d4E6+e5/+6TxgMAAADAV6szt5m8/dXbnL/j
PL1Mht8nAvmsi6bNnbivFWRiSnl3IczOo/7Z00rxzVOfp/H1ZyLFK5h+1nL5O6n+v9z+5fpJ
utKTkvVRu/v8tbFjdbKnqLsUxdVzf0KzR5W9u5scHgAAAAB+jDaceG/5qa/Nz28z2Zmkd8dU
9nSzR5WK93PgfEp5kORHGdXGTKI+0fvT2yD0+aXqvopteJlk10b+mG4z6hO39nxhPO5S2t/3
dLT66mciUWbmT5utvrzH7+xs/MufiQx3SZj734BWeQD49f4DmhUpMw==


由于确实缺乏信息熵,很明显我在做错误的事情以获得此结果,并且浪费了带宽,或者“ A”序列是有效信息。但这很可怕。

最佳答案

发生的事情是,对于数据的第一部分,deflate格式的压缩能力已达到饱和。放气中可能的最大压缩比为1032:1。

在压缩的数据中,前1,083,300字节全为零。几乎所有这些压缩到约1000个零字节。

一切都很好。你什么都没做错。别担心。

09-25 21:43