Java InputStream to String inputStreamToReaderToString(InputStream in)

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

Description

input Stream To Reader To String

License

Apache License

Declaration

public static String inputStreamToReaderToString(InputStream in) throws Exception 

Method Source Code

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

import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.StringWriter;

public class Main {
    public static String inputStreamToReaderToString(InputStream in) throws Exception {
        char[] buf = new char[4096];
        int len;//from   ww  w . j  a  va  2  s.c o m

        InputStreamReader reader = null;
        StringWriter writer = null;
        try {
            reader = new InputStreamReader(in, "UTF-8");
            writer = new StringWriter();

            while ((len = reader.read(buf)) >= 0) {
                writer.write(buf, 0, len);
            }
            writer.flush();

        } finally {
            if (reader != null) {
                reader.close();
            }
            if (writer != null) {
                writer.close();
            }
        }

        return (writer == null ? null : writer.toString());
    }
}

Related

  1. inputStream2String(InputStream is)
  2. inputStream2String(InputStream source)
  3. inputstream_to_string(InputStream in)
  4. inputStreamAsString(InputStream is)
  5. inputStreamReaderToStringBuilder(InputStreamReader reader, StringBuilder builder)
  6. inputStreamtoStream(InputStream in)
  7. inputStreamToString(final InputStream in)
  8. inputStreamToString(final InputStream inputStream, final String... optionalCharsetName)
  9. inputStreamToString(final InputStream is, final int bufferSize)