In the last several chapters, you’ve learned how to use streams to move data between a running Java program and external sources and data stores. Streams can also be used to move data from one part of a Java program to another. This chapter explores three such classes. Sequence input streams chain several input streams together so that they appear as a single stream. Byte array streams allow output to be stored in byte arrays and input to be read from byte arrays. Finally, piped input and output streams allow output from one thread to become input for another thread.
class connects multiple input streams together in a particular order. A
SequenceInputStream first reads all the bytes from the first stream in the sequence, then all the bytes from the second stream in the sequence, then all the bytes from the third stream, and so on. When the end of one stream is reached, that stream is closed; the next data comes from the next stream. This class has two constructors:
public SequenceInputStream(Enumeration e) public SequenceInputStream(InputStream in1, InputStream in2)
The first constructor creates a sequence out of all the elements of the
Enumeration e. This assumes all objects in the enumeration are input streams. If this isn’t the case, a
ClassCastException will be thrown the first time a read is attempted from an object that is not an
InputStream. The use of an
Enumeration instead of an
Iterator is ...