Before decompressing the array compressed by GZIP, I would like to know the length of the original array so as to apply for a suitable buffer array.Like "1024" in the following code.Thanks!
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
will take care of growing the internal byte[]
array. As per the class 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.
You most likely want to copy the stream using InputStream.transferTo()
to make the code more readable:
ByteArrayOutputStream out = new ByteArrayOutputStream(); // default 32 size
GZIPInputStream in = new GZIPInputStream(
new ByteArrayInputStream(localByteBuf)));
in.transferTo(out);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.