|
The Java 2 Platform provides several classes for reading and
writing streams of data. While most of these classes can be found
in the This article discusses the concept of writing specialized stream classes that can process (filter) data in a special fashion. Note, this is an advanced topic on I/O streams and assumes the reader has some knowledge of how I/O streams work. For the basics of I/O stream programming, the difference between byte and character streams, and the concept of stream chaining, please refer to Programming with Java I/O Streams.
The Java platform provides several specialized stream classes, the
majority of which exists in the Java Stream ClassesBefore you decide to write your own specialized stream classes, it might be worth your while inspecting the stream classes provided with the Java 2 platform. Because, one, there might already be a stream class available for your purpose, and two, you can use the source and documentation as examples for writing your own.
To give you a general idea of the various streaming classes available
in the Java platform, the following is a complete list (excluding
deprecated classes) of stream classes available in the
Here are some additional stream classes available in the
For more information on the above classes, please refer to the
Java 2
platform documentation. Additionally, you can download
Java 2 software
and unbundle the Why Write Your Own Stream Classes?With such a variety of I/O stream classes available in the Java platform, you might wonder why anyone would ever need to write their own stream classes. Well, there could be several reasons why, but mainly because your application might require processing a data stream in a special way for which the Java 2 platform does not have sufficient classes. The following are candidates for specialized stream classes.
How to Write Specialized Stream Classes
Writing your own stream classes is relatively simple. For reading
character streams, you basically extend the Note, to understand the difference between byte and character streams, please refer to the article, Programming with Java I/O Streams. Filter Versus Top-Level Stream Classes
When developing specialized stream classes, you can choose to either
directly subclass the top-level classes (
The filter stream classes seem a bit superfluous at times. However, they
were originally designed for convenience, because they provide default
implementations for all the methods found in the top-level classes. This
enables you to override only those methods you need to. For example, if
you extend Developing Specialized Byte Stream Classes
To develop specialized character stream classes, you either subclass the
To develop a
Developing Specialized Byte Stream Classes
To develop specialized character stream classes, you either subclass the
int read()
To develop an void write(int b) Specialized Stream Classes Outside the Java PlatformNow that you have a general idea of the variety of situations stream classes can be written for, it would help to look at some concrete examples of specialized non-Java stream classes. Examples
The following two examples, TeeOutputStream
The following class,
The code should be fairly easy to follow. Basically, this class overrides
the
CountReader
The following class,
Like the
A Real World Example: Divya's 100% Pure Java Backup ApplicationDivya's 100% Pure Java applet and application, BackOnline, is a Internet-based backup, briefcase, and archival application that backs up a user's data files from the client machine to a server, as portrayed in the following illustration.
BackOnline makes extensive use of streams: everything from File Input/Output streams, to Socket streams, to its own specialized Protocol and Encryption/Decryption streams. When a user backs up a file, or files, in BackOnline, the file is processed via a compression stream, then an encryption stream, and then sent over the wire on a socket stream. The server receives this data using socket streams, and stores it in a file using file streams. For restoring files, this process simply works in reverse. The streams in use during the backup and restore processing are shown in the following figure.
Although, most of the stream classes used in BackOnline are ones available in the Java core APIs (such as socket, file, GZIP compression), there are two pairs of specialized stream classes written by Divya: one for the protocol used by the client and server to communicate with each other; the other for encrypting and decrypting the stream using 56-bit DES encryption. Protocol Stream Classes
The application uses a protocol almost identical to HTTP version 1.1.
Each client request (such as file backup) contains a header followed by
a blank line, followed by the data for that request. The stream classes
automatically handle the separation of the header and data portions in
the stream, and provide the appropriate methods to obtain the header
values and
Here is the Java source code
for the Encryption Stream ClassesWhen a user logs into BackOnline, in addition to providing the userid and password for authentication, they provide an optional encryption key, like a second password, which is used to encrypt and decrypt the stream. In other words, the application uses a key/password-based encryption/decryption scheme. Because this key is always entered by the user and never physically stored anywhere, it provides an extremely secure mechanism for transmitting and storing the user's data.
Here is the Java source code for
the
After looking at the source for the Summary
By now, you probably have an in-depth understanding of how the Java
I/O stream classes work. However, it will still be worth your while
to take a look at the
Java 2 platform
source code, and
documentation
for the various stream classes, especially the classes in the
Remember, while I/O streaming is perhaps not the most glamorous subject compared to other Java technologies, it is in a sense the "central nervous system" of the Java platform, because it is used in so many APIs. Therefore, a thorough understanding of I/O stream classes is essential.
Anil Hemrajani is a senior consultant at Divya Incorporated, a consulting firm specializing in Java/Internet software solutions. Anil provides Java/Internet-based architecture, design and development solutions to Fortune 500 companies, and occasionally writes articles and speaks at conferences. He can be reached at anil@divya.com.
| |||||||||||||
Oracle is reviewing the Sun product roadmap and will provide guidance to customers in accordance with Oracle's standard product communication policies. Any resulting features and timing of release of such features as determined by Oracle's review of roadmaps, are at the sole discretion of Oracle. All product roadmap information, whether communicated by Sun Microsystems or by Oracle, does not represent a commitment to deliver any material, code, or functionality, and should not be relied upon in making purchasing decisions. It is intended for information purposes only, and may not be incorporated into any contract.
|
| ||||||||||||