code: static int readAllBytes(InputStream in, byte[] buffer) throws IOException {
      int index = 0;
  
      while (index < buffer.length) {
        int read = in.read(buffer, index, buffer.length - index);
        if (read == -1) {
          return index;
        }
        index += read;
      }
  
      return index;
    }
docs: Attempts to fill the buffer by reading as many bytes as available. The
  returned number indicates how many bytes were read, which may be smaller
  than the buffer size if EOF was reached.
  
  @param in
  input stream
  @param buffer
  buffer to fill
  @return the number of bytes read
  @throws IOException