Created
December 26, 2011 18:15
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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