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

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

Description

Utility method to fully read an InputStream into a String.

License

Open Source License

Parameter

Parameter Description
in - InputStream to read from.
charset - The charset to use when decoding bytes to characters.

Exception

Parameter Description
IOException if an I/O error occurs

Declaration

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

Method Source Code


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

import java.io.*;
import java.nio.charset.Charset;

public class Main {
    /**//  w  ww  .ja  v  a  2s.  c  om
     * Utility method to fully read an InputStream into a String. This can be useful
     * for loading text files to a string or reading a URL input stream into a String.
     * This is essentially just a clean code utility.
     *
     * @param in - InputStream to read from.
     * @param charset - The charset to use when decoding bytes to characters.
     * @throws IOException if an I/O error occurs
     */
    public static String toString(InputStream in, Charset charset) throws IOException {
        return toString(in, charset, false);
    }

    /**
     * Utility method to fully read an InputStream into a String. This can be useful
     * for loading text files to a string or reading a URL input stream into a String.
     * This is essentially just a clean code utility.
     *
     * @param in - InputStream to read from.
     * @param charset - The charset to use when decoding bytes to characters.
     * @param close - Close stream when finished.
     * @throws IOException if an I/O error occurs
     */
    public static String toString(InputStream in, Charset charset, boolean close) throws IOException {
        return toString(new InputStreamReader(in, charset), close);
    }

    /**
     * Utility method to fully read a Reader into a String. This can be useful for
     * loading text files to a string. This is essentially just a clean code utility.
     *
     * @param in - Reader to read from.
     * @throws IOException if an I/O error occurs
     */
    public static String toString(Reader in) throws IOException {
        return toString(in, false);
    }

    /**
     * Utility method to fully read a Reader into a String. This can be useful for
     * loading text files to a string. This is essentially just a clean code utility.
     *
     * @param in - Reader to read from.
     * @param close - Close reader when finished.
     * @throws IOException if an I/O error occurs
     */
    public static String toString(Reader in, boolean close) throws IOException {
        StringWriter sw = new StringWriter();
        int len;
        char[] buf = new char[2048];
        while ((len = in.read(buf)) != -1) {
            sw.write(buf, 0, len);
        }
        if (close)
            close(in);
        return sw.toString();
    }

    /**
     * Unconditionally close an <code>OutputStream</code>.
     * <p>
     * Equivalent to {@link java.io.OutputStream#close()}, except any exceptions will be
     * ignored.
     *
     * @param output - A (possibly null) OutputStream
     */
    public static boolean close(OutputStream output) {
        if (null == output)
            return false;
        try {
            output.close();
            return true;
        } catch (IOException ioe) {
            return false;
        }
    }

    /**
     * Unconditionally close an <code>InputStream</code>.
     * <p>
     * Equivalent to {@link java.io.InputStream#close()}, except any exceptions will be
     * ignored.
     *
     * @param input - A (possibly null) InputStream
     */
    public static boolean close(InputStream input) {
        if (null == input)
            return false;
        try {
            input.close();
            return true;
        } catch (IOException ioe) {
            return false;
        }
    }

    /**
     * Unconditionally close an <code>InputStream</code>/<code>OutputStream</code> pair.
     *
     * @param in - A (possibly null) InputStream
     * @param out - A (possibly null) OutputStream
     */
    public static void close(InputStream in, OutputStream out) {
        try {
            if (in != null)
                in.close();
        } catch (Exception ignored) {
        }
        try {
            if (out != null)
                out.close();
        } catch (Exception ignored) {
        }
    }

    /**
     * Unconditionally close an <code>Writer</code>.
     * <p>
     * Equivalent to {@link java.io.Writer#close()}, except any exceptions will be
     * ignored.
     *
     * @param output - A (possibly null) Writer
     */
    public static boolean close(Writer output) {
        if (null == output)
            return false;
        try {
            output.close();
            return true;
        } catch (IOException ioe) {
            return false;
        }
    }

    /**
     * Unconditionally close an <code>Reader</code>.
     * <p>
     * Equivalent to {@link java.io.Reader#close()}, except any exceptions will be
     * ignored.
     *
     * @param input - A (possibly null) Reader
     */
    public static boolean close(Reader input) {
        if (null == input)
            return false;
        try {
            input.close();
            return true;
        } catch (IOException ioe) {
            return false;
        }
    }

    /**
     * Unconditionally close a <code>Reader</code>/<code>Writer</code> pair.
     *
     * @param in - A (possibly null) Reader
     * @param out - A (possibly null) Writer
     */
    public static void close(Reader in, Writer out) {
        try {
            if (in != null)
                in.close();
        } catch (Exception ignored) {
        }
        try {
            if (out != null)
                out.close();
        } catch (Exception ignored) {
        }
    }
}

Related

  1. toString(final InputStream input, final Charset encoding)
  2. toString(InputStream in, Charset charset)
  3. toString(InputStream in, Charset charset)
  4. toString(InputStream in, Charset charset)
  5. toString(InputStream in, Charset charset)
  6. toString(InputStream input, Charset charset)
  7. toString(InputStream input, String charset)
  8. toString(InputStream inputStream, Charset charset)
  9. toString(InputStream is, Charset charset)