Java InputStream to String getInputStreamContent(InputStream i, String encoding)

Here you can find the source of getInputStreamContent(InputStream i, String encoding)

Description

get Input Stream Content

License

Open Source License

Declaration

public static String getInputStreamContent(InputStream i,
            String encoding) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

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

public class Main {
    public static String getInputStreamContent(InputStream i) {
        return getInputStreamContent(i, "UTF-8");
    }/*from w w  w .j av  a  2  s.c  o m*/

    public static String getInputStreamContent(InputStream i,
            String encoding) {
        if (i != null) {
            BufferedReader pBr;
            try {
                pBr = new BufferedReader(new InputStreamReader(i, encoding));
            } catch (UnsupportedEncodingException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
                return null;
            }
            StringBuffer pSb = new StringBuffer();
            int a = -1;
            try {
                while ((a = pBr.read()) != -1) {
                    pSb.append((char) a);
                }
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return pSb.toString();
        }
        return null;
    }
}

Related

  1. asArrayString(InputStream response)
  2. getInputStreamAsString(final InputStream is, final String encoding)
  3. getInputStreamAsString(InputStream inStream)
  4. getInputStreamAsString(InputStream stream)
  5. getInputStreamContent(InputStream inputStream)
  6. getInputStreamContents(InputStream inputStream)
  7. getInputStreamContents(InputStream inputStream)
  8. getStreamAsString(final InputStream stream)