Java Reference
In-Depth Information
it is interrupted is problematic. For example, suppose you use a particu-
lar stream to read
HTTP
requests across the network. If a thread reading
the next request is interrupted after reading two bytes of the header
field in the request packet, the next thread reading from that stream
will get invalid data unless the stream takes steps to prevent this. Given
the effort involved in writing classes that can deal effectively with these
sorts of situations, most implementations
do not
allow a thread to be in-
terrupted until the main I/O operation has completed, so you cannot rely
on blocking I/O being interruptible. The interruptible channels provided
in the
java.nio
package support interruption by closing the stream when
any thread using the stream is interruptedthis ensures that there are no
issues about what would next be read.
Even when interruption cannot be responded to during an I/O operation
many systems will check for interruption at the start and/or end of the
operation and throw the
InterruptedIOException
then. Also, if a thread is
blocked on a stream when the stream is closed by another thread, most
implementations will unblock the blocked thread and throw an
IOExcep-
tion
.
20.5.2.
Filter
Streams
Filter
streams
FilterInputStream
,
FilterOutputStream
,
FilterReader
, and
FilterWriter
help you chain streams to produce composite streams of
greater utility. Each filter stream is bound to another stream to which
it delegates the actual input or output actions.
Filter
streams get their
power from the ability to filterprocesswhat they read or write, trans-
forming the data in some way.
Filter
byte streams add new constructors that accept a stream of the
appropriate type (input or output) to which to connect.
Filter
character
streams similarly add a new constructor that accepts a character stream
of the appropriate type (reader or writer). However, many character
streams already have constructors that take another character stream,
so those
Reader
and
Writer
classes can act as filters even if they do not
extend
FilterReader
or
FilterWriter
.