我编写了一个简单的Java代码片段,它接受一个String,将其转换为byte [],然后使用Gzip压缩它。然后它解压缩结果以返回byte [],现在包含一个额外的垃圾值字节。为什么这里有垃圾值字节?
public static void main(String [] args)抛出异常{
String testString = "Sample String here";
byte[] originalBytes = testString.getBytes();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
GZIPOutputStream gzos = new GZIPOutputStream(baos);
gzos.write(originalBytes);
gzos.close();
byte[] compressedBytes = baos.toByteArray();
ByteArrayInputStream bais = new ByteArrayInputStream(compressedBytes);
GZIPInputStream gzis = new GZIPInputStream(bais);
ByteArrayOutputStream dbaos = new ByteArrayOutputStream();
while(gzis.available() > 0) {
dbaos.write(gzis.read());
}
byte[] decompressedBytes = dbaos.toByteArray();
String decompressedString = new String(decompressedBytes);
System.out.println(">>" + decompressedString + "<<");
System.out.println("Size of bytes before: " + originalBytes.length);
System.out.println("Size of bytes after: " + decompressedBytes.length);
}
输出:
>>Sample String here�<<
Size of bytes before: 18
Size of bytes after: 19
有人能告诉我为什么有垃圾值字节?如何在不改变上面代码设置的情况下摆脱它?
答案 0 :(得分:4)
你在这里使用available()
,所以你得到一个额外的字节。您应该阅读流并检查小于0
的值。改变这个
ByteArrayOutputStream dbaos = new ByteArrayOutputStream();
while(gzis.available() > 0) {
dbaos.write(gzis.read());
}
类似
ByteArrayOutputStream dbaos = new ByteArrayOutputStream();
int b;
while ((b = gzis.read()) >= 0) {
dbaos.write(b);
}
我得到了
>>Sample String here<<
Size of bytes before: 18
Size of bytes after: 18