I/O Iterators for Java
Tuesday, 15 April 2003
While fleshing out the remaining parts of the primitives package in Commons Collections, a simple but potentially powerful feature became apparent. Using the byte-based primitive collections, it's not only easy but also efficient to bring together the I/O and Collections APIs via ByteIterator/InputStream adapters.
This allows one to treat any InputStream as a ByteIterator (and hence as an Iterator over Bytes) without resorting to a byte array buffer. This means that Iterator-based APIs like the functor algorithms can be applied to I/O streams. Similarly, this allows one to treat any ByteCollection (or Collection of Bytes) as a source for InputStreams. This means that stream-based APIs can be applied to ByteCollections and Collections of Bytes.
This brings Java one small step closer to the conceptual uniformity and resulting combinatorial expressiveness enjoyed by languages like Lisp. This is reason enough in my mind to extend the primitive collections to Char/char and to Reader-adapters as well.
(Also see this related post.)