public static String stream2String(InputStream is,int maxLength) throws IOException {
  byte[] buffer=new byte[4096];
  StringWriter sw=new StringWriter();
  int totalRead=0;
  int read=0;
  do {
    sw.write(new String(buffer,0,read));
    totalRead+=read;
    read=is.read(buffer,0,buffer.length);
  }
 while (((-1 == maxLength) || (totalRead < maxLength)) && (read != -1));
  return sw.toString();
}

public static String stream2String ( InputStream is , int maxLength ) throws IOException { byte [ ] buffer = new byte [ 4096 ] ; StringWriter sw = new StringWriter ( ) ; int totalRead = 0 ; int read = 0 ; do { sw . write ( new String ( buffer , 0 , read ) ) ; totalRead += read ; read = is . read ( buffer , 0 , buffer . length ) ; } while ( ( ( - 1 == maxLength ) || ( totalRead < maxLength ) ) && ( read != - 1 ) ) ; return sw . toString ( ) ; }
read a string of up to maxlength bytes from an inputstream
