Possible Duplicate:
Read/convert an InputStream to a String
I’m trying to append data from a InputStream object to a string as follows:
byte[] buffer = new byte[8192];
InputStream stdout;
String s = "";
while(IsMoreInputDataAvailable()) {
int length = this.stdout.read(buffer);
s += new String(this.buffer, 0, length, "ASCII");
Is there an easier way of doing this without using Apache or such libraries?
The usual way to read character data from an
InputStreamis to wrap it in aReader. InputStreams are for binary data, Readers are for character data. The appropriateReaderto read from anInputStreamis the aptly namedInputStreamReader:Note that appending to a
Stringrepeatedly inside a loop is usually a bad idea (because each time you’ll need to copy the whole string), so I’m using aStringBuilderhere instead.