Java Reference
In-Depth Information
The data is read from the stream by these methods simply as bytes. No conversion is applied to the
bytes read. If any conversion is required, for a stream containing bytes in the local character encoding
for instance, you must provide a way to handle this. We will see how this might be done in a moment.
You can skip over bytes in an
InputStream
by calling its
skip()
method. You specify the number of
bytes to be skipped as an argument of type
long
, and the actual number of bytes skipped is returned,
also a value of type
long
. This method can throw an
IOException
if an error occurs.
You can close an
InputStream
by calling its
close()
method. Once you have closed an input stream,
subsequent attempts to access or read from the stream will cause an
IOException
to be thrown.
The
InputStream
class has seven direct subclasses as shown in the diagram below.
InputStream
AudioInputStream
SequenceInputStream
For Reading Audio data
For Reading from a
Sequence of Streams
FileInputStream
ByteArrayInputStream
For Reading from a File
For Reading from a byte Array
ObjectInputStream
PipedInputStream
For Reading Objects from a Stream
For Reading from a Piped Stream
FilterInputStream
For Filtering Input From an
Existing Stream
We will be using the
FileInputStream
class in Chapter 10
Writing Files
, and the
ObjectInputStream
class in Chapter 11
Reading Files
.
The
FilterInputStream
class has a further nine direct subclasses that provide more specialized ways of
filtering or transforming data from an input stream. We will only be using the
BufferedInputStream
class, but here's the complete set with an indication of what each of them does:
BufferedInputStream
Buffers input from another stream in memory to make the read
operations more efficient.
DataInputStream
Reads data of primitive types from a binary stream.
CheckedInputStream
Reads an input stream and maintains a checksum for the data
that is read to verify its integrity.