Java InputStream to String InputStreamToString(InputStream in, String charset)

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

Description

Input Stream To String

License

Open Source License

Declaration

public static String InputStreamToString(InputStream in, String charset) throws IOException 

Method Source Code

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

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 inputStream) throws IOException {

        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        byte[] data = new byte[BUFFER_SIZE];
        String string = null;//from   w  w w . j av a2  s.  com
        int count = 0;
        while ((count = inputStream.read(data, 0, BUFFER_SIZE)) != -1) {

            byteArrayOutputStream.write(data, 0, count);

        }

        string = new String(byteArrayOutputStream.toByteArray(), "UTF-8");
        return string;
    }

    public static String InputStreamToString(InputStream in, String charset) throws IOException {

        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        byte[] data = new byte[BUFFER_SIZE];
        String string = null;
        int count = 0;
        while ((count = in.read(data, 0, BUFFER_SIZE)) != -1) {

            byteArrayOutputStream.write(data, 0, count);

        }

        string = new String(byteArrayOutputStream.toByteArray(), charset);
        return string;
    }
}

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, String encoding)
  7. inputStreamToString(InputStream input)
  8. InputStreamToString(InputStream input_stream)
  9. inputStreamToString(InputStream inputStream)