code: public static void transfer(InputStream in, byte[] bytes) throws IOException
      {
          // Read in the bytes
          int offset = 0;
          int numRead;
          while (offset < bytes.length && (numRead = in.read(bytes, offset, bytes.length - offset)) >= 0)
          {
              offset += numRead;
          }
  
          if (offset < bytes.length)
          {
              throw new IOException("Retry:  Not all bytes were transferred correctly.");
          }
      }
docs: Use this when you expect a byte[] length of bytes to be read from the InputStream