First of all, I'm sorry if my terminology is a bit amateur, try to bear with me ;)
I am attempting to convert the gzipped body of a HTTP response to plaintext. I've taken the byte array of this response and converted it to a ByteArrayInputStream. I've then converted this to a GZIPInputStream. I now want to read the GZIPInputStream and store the final decompressed HTTP response body as a plaintext String.
This code will store the final decompressed contents in an OutputStream, but I want to store the contents as a String:
public static int sChunk = 8192;
ByteArrayInputStream bais = new ByteArrayInputStream(responseBytes);
GZIPInputStream gzis = new GZIPInputStream(bais);
byte[] buffer = new byte[sChunk];
int length;
while ((length = gzis.read(buffer, 0, sChunk)) != -1) {
out.write(buffer, 0, length);
}
To decode bytes from an InputStream, you can use an InputStreamReader. Then, a BufferedReader will allow you to read your stream line by line.
Your code will look like:
You can use the StringWriter to write to String
you can also do
AutoClosable is a good thing https://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html
Use the try-with-resources idiom (which automatically closes any resources opened in try(...) on exit from the block) to make code cleaner.
Use Apache IOUtils to convert inputStream to String using default CharSet.
You should rather have obtained the response as an
InputStream
instead of asbyte[]
. Then you can ungzip it usingGZIPInputStream
and read it as character data usingInputStreamReader
and finally write it as character data into aString
usingStringWriter
.See also:
If your final intent is to parse the response as HTML, then I strongly recommend to just use a HTML parser for this like Jsoup. It's then as easy as:
Use Apache Commons to convert GzipInputStream to byteArray.
To convert byte array uncompressed content to String, do something like this :