Last active
July 3, 2019 11:15
-
-
Save fkirc/a231c817d582e114e791b77bb33e30e9 to your computer and use it in GitHub Desktop.
Java (JDK only): Efficiently convert InputStream to String
This file contains hidden or 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
private static String inputStreamToString(final InputStream is) throws IOException { | |
final InputStreamReader ir = new InputStreamReader(is, "UTF-8"); | |
final StringBuilder sb = new StringBuilder(); | |
final char[] buf = new char[1024]; | |
int n; | |
while ((n = ir.read(buf)) != -1) { | |
sb.append(buf, 0, n); | |
} | |
return sb.toString(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment