Java InputStream to String inputStreamToString(InputStream in)

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

Description

Reads an InputStream into a String

License

Open Source License

Parameter

Parameter Description
in The InputStream

Return

A String representation of the contents of the stream, or null if the stream is empty.

Declaration

public static String inputStreamToString(InputStream in) 

Method Source Code


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

import java.io.InputStream;
import java.util.Scanner;

public class Main {
    /**//  w  w  w. j  av a  2s . c om
     * Reads an InputStream into a String
     *
     * @param in    The InputStream
     * @return  A String representation of the contents of the stream, or
     *          null if the stream is empty.
     */
    public static String inputStreamToString(InputStream in) {
        return inputStreamToString(in, "UTF-8");
    }

    /**
     * Same as the above {@link #inputStreamToString(java.io.InputStream)},
     * except that one can specify the encoding.
     *
     * @param in    The InputStream
     * @param encoding  The encoding to use
     * @return  A String representation of the contents of the stream, or
     *          null if the stream is empty.
     */
    public static String inputStreamToString(InputStream in, String encoding) {
        Scanner s = new Scanner(in, encoding).useDelimiter("\\A");
        return s.hasNext() ? s.next() : "";
    }
}

Related

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