tags:

views:

148

answers:

3

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);
}
A: 

You can use the StringWriter to write to String

Gopi
+1  A: 

You should rather have obtained the response as an InputStream isntead of as byte[]. Then you can ungzip it using GZIPInputStream and read it as character data using InputStreamReader and finally write it as character data into a String using StringWriter.

Reader reader = null;
StringWriter writer = null;
String charset = "UTF-8"; // You should determine it based on response header.

try {
    InputStream gzippedResponse = response.getInputStream();
    InputStream ungzippedResponse = new GZIPInputStream(gzippedResponse);
    reader = new InputStreamReader(ungzippedResponse, charset);
    writer = new StringWriter();

    byte[] buffer = new byte[10240];
    for (int length = 0; (length = reader.read(buffer)) > 0;) {
        writer.write(buffer, 0, length);
    }
} finally {
    close(writer);
    close(reader);
}

String body = writer.toString();

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:

String html = Jsoup.parse(new URL("http://google.com"), 3000).html();
BalusC
+2  A: 

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:

ByteArrayInputStream bais = new ByteArrayInputStream(responseBytes);
GZIPInputStream gzis = new GZIPInputStream(bais);
InputStreamReader reader = new InputStreamReader(gzis);
BufferedReader in = new BufferedReader(reader);

String readed;
while ((readed = in.readLine()) != null) {
    System.out.println(readed);
}
Vivien Barousse
And don't forget to specify the encoding in the ISR constructor for interpreting the bytes correctly! :)
helios