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

public class Main {
    final static int BUFFER_SIZE = 4096;

    public static String InputStreamTOString(InputStream in) {

        ByteArrayOutputStream outStream = new ByteArrayOutputStream();
        byte[] data = new byte[BUFFER_SIZE];
        String string = null;//from  w w  w. j  a  v  a 2 s .  c om
        int count = 0;
        try {
            while ((count = in.read(data, 0, BUFFER_SIZE)) != -1)
                outStream.write(data, 0, count);
        } catch (IOException e) {
            e.printStackTrace();
        }

        data = null;
        try {
            string = new String(outStream.toByteArray(), "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return string;
    }

    public static String InputStreamTOString(InputStream in, String encoding) {
        String string = null;
        ByteArrayOutputStream outStream = new ByteArrayOutputStream();
        byte[] data = new byte[BUFFER_SIZE];
        int count = -1;
        try {
            while ((count = in.read(data, 0, BUFFER_SIZE)) != -1)
                outStream.write(data, 0, count);
        } catch (IOException e) {
            e.printStackTrace();
        }

        data = null;
        try {
            string = new String(outStream.toByteArray(), encoding);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return string;
    }
}

Related

  1. inputStreamToString(final InputStream is, final int bufferSize)
  2. inputStreamToString(final InputStream stream)
  3. inputStreamToString(final InputStream stream, final String charset)
  4. inputStreamToString(InputStream in)
  5. inputStreamToString(InputStream in)
  6. inputStreamToString(InputStream in)
  7. inputStreamToString(InputStream in)
  8. inputStreamToString(InputStream in)
  9. InputStreamToString(InputStream in)