| InputStream is an abstract class that defines the fundamental
ways in which a destination (consumer) reads a stream of bytes from some source. |
| read() |
| |
| The most important method to the consumer of an
input stream is the one that reads bytes from the source. |
| |
| Here's the first form of read(): |
InputStream s
= getAnInputStreamFromSomewhere();
byte[] buffer = new byte[1024];
// any size will do
if (s.read(buffer) != buffer.length)
System.out.println("I got less than I expected."); |
| |
| This form of read() attempts to fill the entire buffer given.
If it cannot (usually due to reaching the end of the input stream), it
returns the actual number of bytes that were read into the buffer.
After that, any further calls to read() return -1, indicating
that you are at the end of the stream. Note that the if statement
still works even in this case, because -1 != 1024 (this corresponds
to an input stream with no bytes in it at all). |
| skip() |
| |
| What if you want to skip over some of the bytes in a
stream, or start reading a stream from other than its
beginning? A method similar to read() does the trick: |
if (s.skip(1024) != 1024)
System.out.println("I skipped less than I expected."); |
| |
| This example skips over the next 1024 bytes in the
input stream. However, the implementation of skip() in
InputStream may skip fewer bytes than the given argument, and
so it returns a long integer representing the number of bytes
it actually skipped. In this example, therefore, a message
is printed if the actual number of bytes skipped is less than 1024. |
| available() |
| |
| If for some reason we would like to know how many
bytes are in the stream right now, we can ask the following: |
if (s.available() < 1024)
System.out.println("Too little is available right now."); |
| |
| This tells us the number of bytes that we can read without blocking. |
mark() and reset()
InputStream s = getAnInputStreamFromSomewhere();
if (s.markSupported()) { // does s support the notion?
. . . // read the stream for a while
s.mark(1024);
. . . // read less than 1024 more bytes
s.reset();
. . . // we can now re-read those bytes
} else {
. . . // no, perform some alternative
} |
| |
| When marking a stream, we specify the maximum number
of bytes you intend to allow to pass before resetting it.
This allows the stream to limit the size of its byte "memory." If
this number of bytes goes by and you have not yet used reset(), the
mark becomes invalid, and attempting to use reset() will throw an exception. |