Skip to Main Content
Java I/O, 2nd Edition
book

Java I/O, 2nd Edition

by Elliotte Rusty Harold
May 2006
Beginner to intermediate content levelBeginner to intermediate
726 pages
19h 57m
English
O'Reilly Media, Inc.
Content preview from Java I/O, 2nd Edition

Chapter 9. Streams in Memory

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.

Sequence Input Streams

The java.io.SequenceInputStream 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 ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Java I/O

Java I/O

Elliotte Rusty Harold
Java NIO

Java NIO

Ron Hitchens

Publisher Resources

ISBN: 0596527500Errata PageSupplemental Content