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.*;

public class Main {

    public static String convertToString(InputStream is, String encoding)
            throws IOException {
        if (null == is) {
            return null;
        }//  www  . j a va 2  s .  c o  m
        BufferedReader reader = new BufferedReader(new InputStreamReader(
                is, encoding));
        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. convertStreamToString(InputStream is)
  3. convertStreamToText(InputStream is)
  4. convertStreamToText(InputStream is)
  5. convertToString(InputStream is, String encoding)
  6. convertToString(InputStream is, String encoding)
  7. convert(InputStream is)
  8. readInputStream(InputStream inStream)
  9. toString(InputStream is)