Java InputStream Read by Charset toString(InputStream in, Charset charset)

Here you can find the source of toString(InputStream in, Charset charset)

Description

to String

License

Apache License

Declaration

public static String toString(InputStream in, Charset charset) throws IOException 

Method Source Code


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

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

import java.io.Reader;

import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;

public class Main {
    public static String toString(InputStream in) throws IOException {
        return toString(in, StandardCharsets.UTF_8);
    }/*from   w  w w . j a  v  a  2  s .  co  m*/

    public static String toString(InputStream in, Charset charset) throws IOException {
        return toString(new InputStreamReader(in, charset));
    }

    public static String toString(Reader reader) throws IOException {
        try {
            char[] arr = new char[8 * 1024];
            StringBuilder buffer = new StringBuilder();
            int numCharsRead;
            while ((numCharsRead = reader.read(arr, 0, arr.length)) != -1) {
                buffer.append(arr, 0, numCharsRead);
            }
            return buffer.toString();
        } finally {
            reader.close();
        }
    }
}

Related

  1. stringFromStream(InputStream in, Charset cs)
  2. toInputStream(final String string, final Charset charset)
  3. toInputStream(String input, Charset charset)
  4. toInputStream(String input, Charset cs)
  5. toString(final InputStream input, final Charset encoding)
  6. toString(InputStream in, Charset charset)
  7. toString(InputStream in, Charset charset)
  8. toString(InputStream in, Charset charset)
  9. toString(InputStream in, Charset charset)