public static byte[] readInputStream(InputStream in) throws IOException {
  try {
class Part {
      byte[] partData;
      int len;
    }
    LinkedList<Part> parts=new LinkedList<>();
    int len=1;
    while (len > 0) {
      byte[] data=new byte[1024];
      len=in.read(data);
      if (len > 0) {
        Part part=new Part();
        part.partData=data;
        part.len=len;
        parts.add(part);
      }
    }
    int length=0;
    for (    Part part : parts) {
      length+=part.len;
    }
    byte[] result=new byte[length];
    int pos=0;
    for (    Part part : parts) {
      System.arraycopy(part.partData,0,result,pos,part.len);
      pos+=part.len;
    }
    return result;
  }
  finally {
    if (in != null) {
      try {
        in.close();
      }
 catch (      IOException e) {
      }
    }
  }
}

public static byte [ ] readInputStream ( InputStream in ) throws IOException { try { class Part { byte [ ] partData ; int len ; } LinkedList < Part > parts = new LinkedList < > ( ) ; int len = 1 ; while ( len > 0 ) { byte [ ] data = new byte [ 1024 ] ; len = in . read ( data ) ; if ( len > 0 ) { Part part = new Part ( ) ; part . partData = data ; part . len = len ; parts . add ( part ) ; } } int length = 0 ; for ( Part part : parts ) { length += part . len ; } byte [ ] result = new byte [ length ] ; int pos = 0 ; for ( Part part : parts ) { System . arraycopy ( part . partData , 0 , result , pos , part . len ) ; pos += part . len ; } return result ; } finally { if ( in != null ) { try { in . close ( ) ; } catch ( IOException e ) { } } } }
this method will return a byte array containing the raw data from the given input stream . the stream will be closed afterwards in any case .
