Java InputStream to String getStreamAsString(InputStream stream, String charset)

Here you can find the source of getStreamAsString(InputStream stream, String charset)

Description

get Stream As String

License

Open Source License

Declaration

private static String getStreamAsString(InputStream stream, String charset) throws IOException 

Method Source Code


//package com.java2s;
import java.io.*;

public class Main {
    private static String getStreamAsString(InputStream stream, String charset) throws IOException {
        try {/*from w w  w.  ja  va  2s.  c o  m*/
            BufferedReader reader = new BufferedReader(new InputStreamReader(stream, charset));
            StringWriter writer = new StringWriter();

            char[] chars = new char[256];
            int count = 0;
            while ((count = reader.read(chars)) > 0) {
                writer.write(chars, 0, count);
            }

            return writer.toString();
        } finally {
            if (stream != null) {
                stream.close();
            }
        }
    }
}

Related

  1. getInputStreamContents(InputStream inputStream)
  2. getStreamAsString(final InputStream stream)
  3. getStreamAsString(InputStream input, String charset)
  4. getStreamAsString(InputStream is, String encoding)
  5. getStreamAsString(InputStream stream)
  6. getStreamContent(InputStream is)
  7. getStreamContent(InputStream stream, int bufferSize)
  8. getStreamContentAsString(InputStream is)
  9. getStreamContentAsString(InputStream is, String encoding)