Android InputStream to String Convert streamToString(InputStream is)

Here you can find the source of streamToString(InputStream is)

Description

Reads a InputStream into a String

Parameter

Parameter Description
is InputStream opened stream from which to read the characters

Exception

Parameter Description
IOException an exception

Return

the read data

Declaration

public static String streamToString(InputStream is) throws IOException 

Method Source Code

//package com.java2s;

import java.io.InputStream;

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

public class Main {
    /**//  www .j av a2 s. c om
     * Reads a {@link InputStream} into a {@link String}
     * @param is {@link InputStream} opened stream from which to read the characters
     * @return {@link String} the read data
     * @throws IOException
     */
    public static String streamToString(InputStream is) throws IOException {
        if (is != null) {
            StringBuilder sb = new StringBuilder();
            String line;
            try {
                InputStreamReader inputStreamReader = new InputStreamReader(
                        is, "utf-8");
                BufferedReader reader = new BufferedReader(
                        inputStreamReader);
                while ((line = reader.readLine()) != null) {
                    sb.append(line).append("\n");
                }
                inputStreamReader = null;
                reader = null;
            } finally {
                is.close();
            }
            String s = sb.toString();
            sb = null;
            return s;
        } else {
            return "";
        }
    }
}

Related

  1. streamToString(InputStream in, String charset)
  2. InputStreamToString(InputStream in)
  3. inputToString(InputStream inputStream, String encoding)
  4. converStreamToString(InputStream is)
  5. inputStreamToString(InputStream inputStream)
  6. slurp(InputStream in)
  7. convertStreamToString(InputStream is)
  8. inStream2Str(InputStream inputStream, String encode)
  9. readIt(InputStream stream)