Filter Streams
InputStream
and OutputStream are fairly raw classes. They
allow you to read and write bytes, either singly or in groups, but
that’s all. Deciding what those bytes mean—whether
they’re integers or IEEE 754 floating point numbers or Unicode
text—is completely up to the programmer and the code. However,
there are certain data formats that are extremely common and can
benefit from a solid implementation in the class library. For
example, many integers passed as parts of network protocols are
32-bit big-endian integers. Much text sent over the Web is either
7-bit ASCII or 8-bit Latin-1. Many files transferred by
ftp are stored in the zip format. Java provides
a number of filter classes you can attach to raw streams to translate
the raw bytes to and from these and other formats.
The filters come in two versions: the filter streams and the readers
and writers. The filter streams still work primarily with raw data as
bytes, for instance, by compressing the data or interpreting it as
binary numbers. The readers and writers handle the special case of
text in a variety of encodings such as UTF-8 and ISO 8859-1. Filter
streams are placed on top of raw streams such as a
TelnetInputStream or a
FileOutputStream or other filter streams. Readers and writers can be layered on top of raw streams, filter streams, or other readers and writers. However, filter streams cannot be placed on top of a reader or a writer, so we’ll start here with filter streams and address readers and writers ...