Java InputStream to String loadText(InputStream in)

Here you can find the source of loadText(InputStream in)

Description

Loads the entire stream into memory as a String and returns it.

License

Apache License

Declaration

public static String loadText(InputStream in) throws IOException 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

public class Main {
    /**/*from w w  w.  jav a 2  s . c  o  m*/
     * Loads the entire stream into memory as a String and returns it.
     * <p/>
     * <b>Notice:</b> This implementation appends a <tt>\n</tt> as line
     * terminator at the of the text.
     */
    public static String loadText(InputStream in) throws IOException {
        StringBuilder builder = new StringBuilder();
        try (InputStreamReader isr = new InputStreamReader(in)) {
            BufferedReader reader = new BufferedReader(isr);
            while (true) {
                String line = reader.readLine();
                if (line != null) {
                    builder.append(line);
                    builder.append("\n");
                } else {
                    break;
                }
            }
        }
        return builder.toString();
    }
}

Related

  1. InputStreamToStringArray(InputStream input_stream)
  2. inputStreamToStringBuffer(InputStream stream)
  3. inputStreamToStringBuilder(final InputStream inputStream, final String charset)
  4. inputStreamToStringBuilder(InputStream in, int minimumCapacity)
  5. inputStreamToText(InputStream inputStream)
  6. loadTextFile(InputStream stream, String encoding, int maxSize, boolean finish)
  7. valueOf(InputStream is)