Java Utililty Methods InputStream to String

List of utility methods to do InputStream to String

Description

The list of methods to do InputStream to String are organized into topic(s).

Method

Stringinputstream_to_string(InputStream in)
Description of the Method
Reader reader = new InputStreamReader(in);
StringWriter writer = new StringWriter();
char[] buf = new char[1000];
while (true) {
    int n = reader.read(buf, 0, 1000);
    if (n == -1) {
        break;
    writer.write(buf, 0, n);
return writer.toString();
StringinputStreamAsString(InputStream is)
input Stream As String
try (BufferedReader buffer = new BufferedReader(new InputStreamReader(is))) {
    return buffer.lines().collect(Collectors.joining("\n"));
} catch (IOException e) {
    throw new RuntimeException("Could not convert InputStream to String ", e);
voidinputStreamReaderToStringBuilder(InputStreamReader reader, StringBuilder builder)
input Stream Reader To String Builder
char[] b = new char[4096];
try {
    for (int n; (n = reader.read(b)) != -1;) {
        builder.append(new String(b, 0, n));
} catch (IOException e) {
    e.printStackTrace();
StringinputStreamToReaderToString(InputStream in)
input Stream To Reader To String
char[] buf = new char[4096];
int len;
InputStreamReader reader = null;
StringWriter writer = null;
try {
    reader = new InputStreamReader(in, "UTF-8");
    writer = new StringWriter();
    while ((len = reader.read(buf)) >= 0) {
...
StringinputStreamtoStream(InputStream in)
input Streamto Stream
StringBuilder sb = new StringBuilder();
InputStreamReader is = new InputStreamReader(in, "UTF-8");
BufferedReader br = new BufferedReader(is);
String read = br.readLine();
while (read != null) {
    sb.append(read);
    read = br.readLine();
return sb.toString();
StringinputStreamToString(final InputStream in)
input Stream To String
final BufferedReader br = new BufferedReader(new InputStreamReader(in));
final StringBuffer sb = new StringBuffer();
String line;
while ((line = br.readLine()) != null) {
    sb.append(line).append("\n");
return sb.toString();
StringinputStreamToString(final InputStream inputStream, final String... optionalCharsetName)
input Stream To String
Scanner scanner = (optionalCharsetName == null) || (optionalCharsetName.length == 0)
        || (optionalCharsetName[0] == null) ? new Scanner(inputStream)
                : new Scanner(inputStream, optionalCharsetName[0]);
String content = scanner.useDelimiter("\\A").hasNext() ? scanner.next() : "";
return content;
StringinputStreamToString(final InputStream is, final int bufferSize)
input Stream To String
final char[] buffer = new char[bufferSize];
final StringBuilder out = new StringBuilder();
try (Reader in = new InputStreamReader(is, "UTF-8")) {
    for (;;) {
        int rsz = in.read(buffer, 0, buffer.length);
        if (rsz < 0)
            break;
        out.append(buffer, 0, rsz);
...
StringinputStreamToString(final InputStream stream)
Derived from the Jrubyrack project - IOHelpers
if (stream == null)
    return null;
final StringBuilder str = new StringBuilder(128);
String coding = "UTF-8";
int c = stream.read();
if (c == '#') { 
    str.append((char) c);
    while ((c = stream.read()) != -1 && c != 10) {
...
StringinputStreamToString(final InputStream stream, final String charset)
Reads an entire input stream into a string, one line at a time.
BufferedReader reader = new BufferedReader(new InputStreamReader(stream, charset));
StringBuilder text = new StringBuilder();
for (String line = reader.readLine(); line != null; line = reader.readLine()) {
    text.append(line);
return text.toString();