Java InputStream to String inputStreamToString(InputStream input)

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

Description

Deprecated

License

Open Source License

Parameter

Parameter Description
input a parameter

Exception

Parameter Description
IOException an exception

Return

String form of input stream

Declaration

public static String inputStreamToString(InputStream input) throws IOException 

Method Source Code


//package com.java2s;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.StringWriter;

public class Main {
    /** Deprecated/* w w  w.  j a v  a  2  s.  c  o m*/
     * @param input
     * @return String form of input stream
     * @throws IOException
     */
    public static String inputStreamToString(InputStream input) throws IOException {
        if (input != null) {
            StringWriter writer = new StringWriter();
            char[] buffer = new char[1024];
            try {
                Reader reader = new BufferedReader(new InputStreamReader(input));
                int n;
                while ((n = reader.read(buffer)) != -1) {
                    writer.write(buffer, 0, n);
                }
            } finally {
                input.close();
            }
            return writer.toString().trim();
        } else {
            return "";
        }
    }
}

Related

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