InputStream The base class for byte stream input operations
OutputStream The base class for byte stream output operations
InputStream and OutputStream are both abstract classes. As you are well aware by now, you cannot
create instances of an abstract class — these classes serve only as a base from which to derive classes with
more concrete input or output capabilities. However, both of the classes declare methods that define a ba-
sic set of operations for the streams they represent, so the fundamental characteristics of how a stream is
accessed are set by these classes. Generally, the InputStream and OutputStream subclasses represent byte
streams and provide the means of reading and writing binary data as a series of bytes.
Both classes implement the Closeable interface. This interface declares just one method, close() ,
which closes the stream and releases any resources that the stream object is holding. The Closeable inter-
face extends the AutoCloseable interface that also declares the close() method so classes that implement
Closeable also implement AutoCloseable . Classes that implement the AutoCloseable interface can have
their close() method called automatically when the class object is created within a special try block. I
discuss this in Chapter 9.
Basic Input Stream Operations
As you saw in the previous section, the InputStream class is abstract, so you cannot create objects of this
class type. Nonetheless, input stream objects are often accessible via a reference of this type, so the methods
identified in this class are what you get. The InputStream class includes three methods for reading data
This method is abstract, so it has to be defined in a subclass. The method returns the next byte available
from the stream as type int . If the end of the stream is reached, the method returns the value -1 . An ex-
ception of type IOException is thrown if an I/O error occurs.
This method reads bytes from the stream into successive elements of array . The maximum of ar-
ray.length bytes is read. The method does not return until the input data is read or the end of the
stream is detected. The method returns the number of bytes read or -1 if no bytes were read because the
end of the stream was reached. If an I/O error occurs, an exception of type IOException is thrown. If the
argument to the method is null then a NullPointerException is thrown. An input/output method that
does not return until the operation is completed is referred to as a blocking method, and you say that the
methods blocks until the operation is complete.
This works in essentially the same way as the previous method, except that up to length bytes are read
into array starting with the element array[offset] . The method returns the number of bytes read or -1
if no bytes were read because the end of the stream was reached.
These methods read data from the stream simply as bytes. No conversion is applied. If any conversion is
required — for a stream containing bytes in the local character encoding, for example — you must provide
a way to handle this. You 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.