code: public static void read(InputStream stream, byte[] buffer) throws IOException {
          int read = 0;
          while(read < buffer.length) {
              int newlyRead = stream.read(buffer, read, buffer.length - read);
              if(newlyRead == -1)
                  throw new EOFException("Attempt to read " + buffer.length
                                         + " bytes failed due to EOF.");
              read += newlyRead;
          }
      }
docs: Read exactly buffer.length bytes from the stream into the buffer
  
  @param stream The stream to read from
  @param buffer The buffer to read into