gpt4 book ai didi

java - 如何根据GZIP的压缩字节数组得到原数组的长度?

转载 作者:行者123 更新时间:2023-12-02 02:22:15 39 4
gpt4 key购买 nike

在解压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/

39 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com