Analogous to the utility in to read at most a certain number of characters from a Reader into a String, we need an analogous method to read bytes from an InputStream. The complication is that reading the contents of a buffer may not fill the buffer even if the end of the stream is not yet reached. For example, the following code in XmlDom is incorrect!
That code assumes that if all the requested bytes were not returned, the end of the data was reached. In reality the InputStream.read() method doesn't guarantee it will fill the buffer; it may be that only some of the data was available at the time. So we need a method to keep reading repeatedly until either the buffer is full or the end of the stream is reached.