protected static byte[] inputStreamToByteArray(InputStream str) throws IOException {
  try (ByteArrayOutputStream baos=new ByteArrayOutputStream()){
    int len=0;
    byte[] buf=new byte[8192];
    while ((len=str.read(buf)) != -1) {
      baos.write(buf,0,len);
    }
    return baos.toByteArray();
  }
 }

protected static byte [ ] inputStreamToByteArray ( InputStream str ) throws IOException { try ( ByteArrayOutputStream baos = new ByteArrayOutputStream ( ) ) { int len = 0 ; byte [ ] buf = new byte [ 8192 ] ; while ( ( len = str . read ( buf ) ) != - 1 ) { baos . write ( buf , 0 , len ) ; } return baos . toByteArray ( ) ; } }
helper function to convert an inputstream to a byte [ ] array .
