Skip to content

Instantly share code, notes, and snippets.

@greghelton
Created December 26, 2011 18:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save greghelton/1521796 to your computer and use it in GitHub Desktop.
Save greghelton/1521796 to your computer and use it in GitHub Desktop.
Java: Readers, Writers, Buffers and Streams - seems like a lot to deal with
package us.greg;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.StringWriter;
import java.io.Writer;
public class StreamReaderHelper {
/*
* this is my helper function
*/
public static String readStream(InputStream is) throws IOException {
int n = is.available();
char[] buffer = new char[n];
Reader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
Writer writer = new StringWriter();
while ((n = reader.read(buffer, 0, n)) != -1) {
writer.write(buffer, 0, n);
}
return writer.toString();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment