Skip to content

Instantly share code, notes, and snippets.

@fkirc
Last active July 3, 2019 11:15
Show Gist options
  • Save fkirc/a231c817d582e114e791b77bb33e30e9 to your computer and use it in GitHub Desktop.
Save fkirc/a231c817d582e114e791b77bb33e30e9 to your computer and use it in GitHub Desktop.
Java (JDK only): Efficiently convert InputStream to String
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