Android InputStream to String Convert convertToString(InputStream is, String encoding)

Here you can find the source of convertToString(InputStream is, String encoding)

Description

convert To String

Declaration

public static String convertToString(InputStream is, String encoding)
        throws IOException 

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 convertToString(InputStream is, String encoding)
            throws IOException {
        if (null == is)
            return null;
        BufferedReader reader = new BufferedReader(new InputStreamReader(
                is, encoding));//from  w ww  . j  a va 2 s.c  o  m
        char cache[] = new char[2 * 512];
        int cacheSize = -1;
        StringBuilder buffer = new StringBuilder();
        while ((cacheSize = reader.read(cache)) != -1) {
            buffer.append(new String(cache, 0, cacheSize));
            cacheSize = reader.read(cache);
        }
        is.close();
        return buffer.toString();
    }

    public static String toString(InputStream is) throws IOException {
        if (null == is)
            return null;
        return toStringBuffer(is).toString();
    }

    public static StringBuilder toStringBuffer(InputStream is)
            throws IOException {
        if (null == is)
            return null;
        BufferedReader in = new BufferedReader(new InputStreamReader(is));
        StringBuilder buffer = new StringBuilder();
        String line = null;
        while ((line = in.readLine()) != null) {
            buffer.append(line).append("\n");
        }
        is.close();
        return buffer;
    }
}

Related

  1. convertStreamToString(InputStream is)
  2. convertStreamToText(InputStream is)
  3. convertStreamToText(InputStream is)
  4. convertToString(InputStream is, String encoding)
  5. convertToString(InputStream is, String encoding)
  6. convert(InputStream is)
  7. readInputStream(InputStream inStream)
  8. toString(InputStream is)
  9. toStringBuffer(InputStream is)