Java Utililty Methods InputStream to String

List of utility methods to do InputStream to String

Description

The list of methods to do InputStream to String are organized into topic(s).

Method

StringgetStreamAsString(InputStream is, String encoding)
Copies the contents of the given stream to a string.
Reader r = null;
if (encoding == null) {
    r = new InputStreamReader(is);
} else {
    r = new InputStreamReader(is, encoding);
try {
    StringWriter w = new StringWriter();
...
StringgetStreamAsString(InputStream stream)
get Stream As String
String output = "";
byte[] buffer = new byte[4096];
while (stream.read(buffer) > 0) {
    output = output + new String(buffer);
return output;
StringgetStreamAsString(InputStream stream, String charset)
get Stream As String
try {
    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();
StringgetStreamContent(InputStream is)
get Stream Content
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
String c = "", line;
while ((line = reader.readLine()) != null)
    c += "\n" + line;
return c.substring(1);
byte[]getStreamContent(InputStream stream, int bufferSize)
get Stream Content
ByteArrayOutputStream output = new ByteArrayOutputStream();
try {
    byte[] buffer = new byte[bufferSize];
    int len = stream.read(buffer, 0, bufferSize);
    if (len > 0) {
        output.write(buffer, 0, len);
} finally {
...
StringgetStreamContentAsString(InputStream is)
Gets the stream content as string.
try {
    byte buf[] = new byte[is.available()];
    is.read(buf);
    return new String(buf, "UTF-8");
} catch (Exception e) {
    throw new RuntimeException(e);
StringgetStreamContentAsString(InputStream is, String encoding)
get Stream Content As String
return new String(getStreamContentAsBytes(is), encoding);
StringgetStreamContents(final InputStream is)
get Stream Contents
return getStreamContents(is, DEFAULT_ENCODING);
StringinputStream2String(InputStream in)
input Stream String
BufferedReader reader = new BufferedReader(new InputStreamReader(in, "utf-8"));
StringBuffer sb = new StringBuffer();
String line = null;
try {
    while ((line = reader.readLine()) != null) {
        sb.append(line + "\n");
} catch (IOException e) {
...
StringinputStream2String(InputStream in)
input Stream String
StringBuffer out = new StringBuffer();
byte[] b = new byte[4096];
for (int n; (n = in.read(b)) != -1;) {
    out.append(new String(b, 0, n));
return out.toString();