code: public static void readFully(final InputStream inputStream, final byte[] buffer, final int offset, final int length)
      throws IOException {
          if (buffer.length < length + offset) {
              throw new IllegalArgumentException("Buffer is too small");
          }
  
          int arrayOffset = offset;
          int bytesToRead = length;
          while (bytesToRead > 0) {
              int bytesRead = inputStream.read(buffer, arrayOffset, bytesToRead);
              if (bytesRead < 0) {
                  throw new EOFException();
              }
              bytesToRead -= bytesRead;
              arrayOffset += bytesRead;
          }
      }
docs: Reads bytes from the input stream and puts them into the given byte buffer.
  
  @param inputStream the input stream to read from
  @param buffer      the buffer into which the data is read.
  @param offset      the start offset in array {@code buffer} at which the data is written.
  @param length      the maximum number of bytes to read.
  @throws IOException if there's an error reading from the {@code inputStream}
  @see java.io.InputStream#read(byte[], int, int)