Java InputStream to String inputStreamToString(InputStream is)

Here you can find the source of inputStreamToString(InputStream is)

Description

input Stream To String

License

Apache License

Declaration

public static String inputStreamToString(InputStream is) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.io.InputStream;
import java.io.IOException;
import java.io.ByteArrayOutputStream;
import java.io.InputStreamReader;
import java.io.CharArrayWriter;

public class Main {
    private static final int BUFFERSIZE = 8192;

    public static String inputStreamToString(InputStream is) {
        return new String(exhaustInputStreamUnchecked(is));
    }//from  ww  w  . ja v a2s .co  m

    public static byte[] exhaustInputStreamUnchecked(InputStream in) {
        try {
            return exhaustInputStream(in);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public static char[] exhaustInputStreamUnchecked(InputStream in, String encoding) {
        try {
            return exhaustInputStream(in, encoding);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public static byte[] exhaustInputStream(InputStream in) throws IOException {
        assert in != null;

        ByteArrayOutputStream out = new ByteArrayOutputStream();
        byte[] buffer = new byte[16384];

        int c;
        while ((c = in.read(buffer)) >= 0) {
            out.write(buffer, 0, c);
        }

        return out.toByteArray();
    }

    public static char[] exhaustInputStream(InputStream in, String encoding) throws IOException {
        assert in != null;
        assert encoding != null;

        InputStreamReader charin = new InputStreamReader(in, encoding);
        CharArrayWriter out = new CharArrayWriter();
        char[] buffer = new char[BUFFERSIZE];

        int c;
        while ((c = charin.read(buffer)) >= 0) {
            out.write(buffer, 0, c);
        }

        return out.toCharArray();
    }
}

Related

  1. inputStreamToString(InputStream inputStream)
  2. inputStreamToString(InputStream inputStream, String charsetName)
  3. inputStreamToString(InputStream is)
  4. inputStreamToString(InputStream is)
  5. inputStreamToString(InputStream is)
  6. inputStreamToString(InputStream is)
  7. inputStreamToString(InputStream is)
  8. inputStreamToString(InputStream s)
  9. inputStreamToString(InputStream stream)