In java, InputStream
class has methods read(byte[], int, int)
and readNBytes(byte[], int, int)
. It seems that these two methods have exactly the same functionality, so I wonder what are the differences between them.
read() reads next byte of data from the Input Stream. The value byte is returned in the range 0 to 255. If no byte is available because the end of the stream has been reached, the value -1 is returned.
Depending on where the InputStream is coming from, you might not be able to reset it. You can check if mark() and reset() are supported using markSupported() . If it is, you can call reset() on the InputStream to return to the beginning. If not, you need to read the InputStream from the source again.
Returns. the total number of bytes read into the buffer, or -1 if there is no more data because the end of the stream has been reached.
It returns an int because when the stream can no longer be read, it returns -1. If it returned a byte, then -1 could not be returned to indicate a lack of input because -1 is a valid byte. In addition, you could not return value above 127 or below -128 because Java only handles signed bytes.
Edited for better visibility of discussion in the comments:
read()
says it attempts to read "up to len
bytes ... but a smaller number may be read. This method blocks until input data is available, end of file is detected, or an exception is thrown."
readNBytes()
says "blocks until len
bytes of input data have been read, end of stream is detected, or an exception is thrown."
Even though the JDK's implementation for InputStream
is likely to give you identical results for both methods, the documented differences mean than other classes inheriting from it may behave differently.
E.g. Given the stream '12345<end>'
, read(s,0,10)
is allowed to return '123'
, whereas readNbytes()
is more likely to keep going to look for an end-of-stream and give you the whole thing.
Original answer:
You're right that the javadocs are very similar. When in doubt, always drop down to the source. Most IDEs make it easy to attach the OpenJDK source and lets you drill down to them.
This is readNBytes
from InputStream.java:
public int readNBytes(byte[] b, int off, int len) throws IOException {
Objects.requireNonNull(b);
if (off < 0 || len < 0 || len > b.length - off)
throw new IndexOutOfBoundsException();
int n = 0;
while (n < len) {
int count = read(b, off + n, len - n);
if (count < 0)
break;
n += count;
}
return n;
}
As you can see, it actually performs a call to read(byte[],int,int)
. The difference in this case is that if the actual read bytes is less than your specified len
, it will attempt to read() again until it is confirmed that there is actually nothing left to be read.
Edit: Note that
InputStream
. Others may differ.InputStream
may also have their own overridden implementation. Consult the doc/source for the relevant class.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With