Android InputStream to String Convert inputToString(InputStream inputStream, String encoding)

Here you can find the source of inputToString(InputStream inputStream, String encoding)

Description

input To String

Declaration

public static String inputToString(InputStream inputStream,
        String encoding) throws IOException 

Method Source Code

//package com.java2s;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;

import android.text.TextUtils;

public class Main {

    public static String inputToString(InputStream inputStream,
            String encoding) throws IOException {

        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        byte[] buffer = new byte[1024];
        int len = 0;
        while ((len = inputStream.read(buffer)) != -1) {
            bos.write(buffer, 0, len);//from  ww  w  .ja v a  2 s .com
        }
        inputStream.close();
        bos.close();
        if (TextUtils.isEmpty(encoding)) {
            encoding = "UTF-8";
        }
        return new String(bos.toByteArray(), encoding);
    }

    public static boolean isEmpty(String str) {
        if (str == null || "".equals(str))
            return true;

        for (int i = 0; i < str.length(); i++) {
            char c = str.charAt(i);
            if (c != ' ' && c != '\t' && c != '\r' && c != '\n') {
                return false;
            }
        }
        return true;
    }
}

Related

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