Java InputStream to String InputStreamToString(InputStream in)

Here you can find the source of InputStreamToString(InputStream in)

Description

Input Stream To String

License

Open Source License

Declaration

public static String InputStreamToString(InputStream in) 

Method Source Code


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

public class Main {
    final static int BUFFER_SIZE = 4096;

    public static String InputStreamToString(InputStream in) {
        String result = null;/*from  w  ww.  j a va 2 s  . c o  m*/
        ByteArrayOutputStream outStream = null;
        if (in != null) {
            try {
                outStream = new ByteArrayOutputStream();
                byte[] data = new byte[BUFFER_SIZE];
                int count = -1;
                while ((count = in.read(data, 0, BUFFER_SIZE)) != -1)
                    outStream.write(data, 0, count);
                data = null;
                result = new String(outStream.toByteArray(), "utf-8");
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                try {
                    outStream.close();
                } catch (IOException e) {
                }
            }
        }
        return result;
    }
}

Related

  1. inputStreamToString(InputStream in)
  2. InputStreamTOString(InputStream in)
  3. inputStreamToString(InputStream in)
  4. inputStreamToString(InputStream in)
  5. inputStreamToString(InputStream in)
  6. InputStreamTOString(InputStream in)
  7. InputStreamToString(InputStream in, String charset)
  8. InputStreamTOString(InputStream in, String encoding)
  9. inputStreamToString(InputStream input)