在解压GZIP压缩的数组之前,我想知道原始数组的长度,以便申请合适的缓冲区数组。例如下面代码中的“1024”。谢谢!
byte[] buffer = new byte[1024];
ByteArrayOutputStream out = new ByteArrayOutputStream(1024);
try (ByteArrayInputStream bin = new ByteArrayInputStream(localByteBuf);
GZIPInputStream gis = new GZIPInputStream(bin)) {
int len;
while ((len = gis.read(buffer)) > 0) {
out.write(buffer, 0, len);
}
} catch (IOException e) {
e.printStackTrace();
}
最佳答案
ByteArrayOutputStream
将负责内部 byte[]
数组的增长。根据类 javadoc:
This class implements an output stream in which the data is written into a byte array. The buffer automatically grows as data is written to it.
您很可能希望使用 InputStream.transferTo()
复制流使代码更具可读性:
ByteArrayOutputStream out = new ByteArrayOutputStream(); // default 32 size
GZIPInputStream in = new GZIPInputStream(
new ByteArrayInputStream(localByteBuf)));
in.transferTo(out);
关于java - 如何根据GZIP的压缩字节数组得到原数组的长度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57271389/