Create BufferedReader from InputStream - Android java.io

Android examples for java.io:InputStream

Description

Create BufferedReader from InputStream

Demo Code

import android.util.Log;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

public class Main{

    /**//from w w  w. j  a v a 2 s . c o m
     * Read the whole stream into a string. The connection is not closed by this method.
     * @param stream
     * @return
     */
    public static String fromStream(InputStream stream) {
        if (null == stream) {
            return null;
        }

        final StringBuilder sb = new StringBuilder();
        final BufferedReader br = new BufferedReader(new InputStreamReader(
                stream), 256);
        String line;

        while (true) {
            try {
                line = br.readLine();
            } catch (IOException e) {
                Log.e("StringUtil", "Error reading stream in HTTP get", e);
                break;
            }

            if (null == line) {
                break;
            }

            sb.append(line);
        }

        return sb.toString();
    }

}

Related Tutorials