Java - Write code to read string From InputStream

Requirements

Write code to read string From InputStream

Demo

//package com.book2s;
import java.io.IOException;
import java.io.InputStream;

public class Main {
    public static String stringFromStream(InputStream is)
            throws IOException {
        StringBuffer sb = new StringBuffer();
        byte[] b = new byte[4096];
        int n;/*from  w w  w. ja  v  a2 s.  c  o m*/
        while ((n = is.read(b)) != -1) {
            sb.append(new String(b, 0, n));
        }
        return sb.toString();
    }
}