Java Reference
In-Depth Information
Before trying to use marking and resetting, check whether the markSupported() meth‐
od returns true. If it does, the stream supports marking and resetting. Otherwise, mark()
will do nothing and reset() will throw an IOException .
In my opinion, this demonstrates very poor design. In practice, more
streams don't support marking and resetting than do . Attaching func‐
tionality to an abstract superclass that is not available to many, prob‐
ably most, subclasses is a very poor idea. It would be better to place
these three methods in a separate interface that could be implement‐
ed by those classes that provided this functionality. The disadvantage
of this approach is that you couldn't then invoke these methods on an
arbitrary input stream of unknown type; but in practice, you can't do
that anyway because not all streams support marking and resetting.
Providing a method such as markSupported() to check for function‐
ality at runtime is a more traditional, non-object-oriented solution to
the problem. An object-oriented approach would embed this in the
type system through interfaces and classes so that it could all be
checked at compile time.
The only input stream classes in java.io that always support marking are BufferedIn
putStream and ByteArrayInputStream . However, other input streams such as Telne
tInputStream may support marking if they're chained to a buffered input stream first.
Filter Streams
InputStream and OutputStream are fairly raw classes. They read and write bytes 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 pro‐
grammer and the code. However, there are certain extremely common data formats that
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 of the text
sent over the Web is either 7-bit ASCII, 8-bit Latin-1, or multibyte UTF-8. 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.
Filters are organized in a chain, as shown in Figure 2-2 . Each link in the chain receives
data from the previous filter or stream and passes the data along to the next link in the
Search WWH ::




Custom Search