这是我的测试代码,问题是resultLength!= original.length,结果数组与原始数组相同,压缩和解压缩后不应该这样吗?
public static void main(String[] args) {
// generateTyreAlarmEvent()
byte[] original = {10, 17, 97, 98, 99, 100, 101, 102, 103, 104, 105, 103, 107, 108, 109, 110, 111, 112, 113, 16, 2, 24, -50, -1, -113, -59, 5, 34, 20, 8, -35, 1, 16, 40, 24, -34, 1, 32, 60, 40, -33, 1, 48, 80, 56, -32, 1, 64, 100, 42, 16, 8, 2, 16, 1, 24, 3, 32, 0, 40, 1, 48, 1, 56, 1, 64, 0};
byte[] buffer = new byte[original.length];
Deflater compresser = new Deflater();
compresser.setInput(original);
compresser.finish();
int compressedLen = compresser.deflate(buffer);
compresser.end();
Inflater decompressor = new Inflater();
decompressor.setInput(buffer, 0, compressedLen);
byte[] result = new byte[original.length];
int resultLength = 0;
try {
resultLength = decompressor.inflate(result);
} catch (DataFormatException e) {
}
decompressor.end();
// the problem is resultLength(63) != original.length(67), and result array is same as original array
}
原始字节数组是(len是67):
{10, 17, 97, 98, 99, 100, 101, 102, 103, 104, 105, 103, 107, 108, 109, 110, 111, 112, 113, 16, 2, 24, -50, -1, -113, -59, 5, 34, 20, 8, -35, 1, 16, 40, 24, -34, 1, 32, 60, 40, -33, 1, 48, 80, 56, -32, 1, 64, 100, 42, 16, 8, 2, 16, 1, 24, 3, 32, 0, 40, 1, 48, 1, 56, 1, 64, 0}
结果是(len是63):
{10, 17, 97, 98, 99, 100, 101, 102, 103, 104, 105, 103, 107, 108, 109, 110, 111, 112, 113, 16, 2, 24, -50, -1, -113, -59, 5, 34, 20, 8, -35, 1, 16, 40, 24, -34, 1, 32, 60, 40, -33, 1, 48, 80, 56, -32, 1, 64, 100, 42, 16, 8, 2, 16, 1, 24, 3, 32, 0, 40, 1, 48, 1}
谢谢!
答案 0 :(得分:0)
您没有使输出缓冲区足够大。为了测试,我将byte[] buffer = new byte[original.length];
替换为byte[] buffer = new byte[1000];
,它运行正常。
如果数据不可压缩,那么输出将大于输入。