Android InputStream to String Convert convertStreamToString(InputStream is)

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

Description

convert Stream To String

Declaration

public static String convertStreamToString(InputStream is) 

Method Source Code

//package com.java2s;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

public class Main {

    public static String convertStreamToString(InputStream is) {
        BufferedReader reader = new BufferedReader(
                new InputStreamReader(is));
        StringBuilder sb = new StringBuilder();
        String line = null;//from   w  w  w .  ja  v a 2s.co m
        try {
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }

            //??????\n??
            if (sb.indexOf("\n") != -1
                    && sb.lastIndexOf("\n") == sb.length() - 1) {
                sb.delete(sb.lastIndexOf("\n"), sb.lastIndexOf("\n") + 1);
            }

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return sb.toString();
    }
}

Related

  1. toStringBuffer(InputStream is)
  2. toStringBuffer(InputStream is)
  3. toStringBuffer(InputStream is)
  4. inputStreamToString(final InputStream is)
  5. convertStreamToString(InputStream is)
  6. convertStreamToString(InputStream is)
  7. convertStreamToString(InputStream is)
  8. convertStreamToText(InputStream is)
  9. convertStreamToText(InputStream is)