Start of Tutorial > Start of Trail > Start of Lesson |
Search
Feedback Form |
Let's rewrite the example from How to Write Your Own Filter Streams so that it works onRandomAccessFiles
. BecauseRandomAccessFile
implements theDataInput
andDataOutput
interfaces, a side benefit is that the filtered stream will also work with otherDataInput
andDataOutput
streams including some sequential access streams such asDataInputStream
andDataOutputStream
.The example
CheckedIODemo
from How to Write Your Own Filter Streams implements two filter streams that compute a checksum as data is read from or written to the stream. Those streams areCheckedInputStream
andCheckedOutputStream
.In the new example,
CheckedDataOutput
is a rewrite ofCheckedOutputStream
--it computes a checksum for data writen to the stream. However, it operates onDataOutput
objects instead of onOutputStream
objects. Similarly,CheckedDataInput
modifiesCheckedInputStream
so that it now works onDataInput
objects instead ofInputStream
objects.
CheckedDataOutput
versus CheckedOutputStream
Let's look at howCheckedDataOutput
differs fromCheckedOutputStream
.The first difference in these two classes is that
CheckedDataOutput
does not extendFilterOutputStream
. Instead, it implements theDataOutput
interface.public class CheckedDataOutput implements DataOutput
Note: To keep the example code simple, theCheckedDataOutput
class actually provided in this lesson is not declared to implementDataOutput
, because theDataOutput
interface specifies so many methods. However, theCheckedDataOutput
class as provided in the example does implement several ofDataOutput
's methods to illustrate how it should work.Next,
CheckedDataOutput
declares a private variable to hold aDataOutput
object.This is the object to which data will be written.private DataOutput out;The constructor for
CheckedDataOutput
differs fromCheckedOutputStream
's constructor in thatCheckedDataOutput
is created on aDataOutput
object rather than on anOutputStream
.This constructor does not callpublic CheckedDataOutput(DataOutput out, Checksum cksum) { this.cksum = cksum; this.out = out; }super(out)
like theCheckedOutputStream
constructor did, becauseCheckedDataOutput
extends fromObject
rather than from a stream class.Those are the only modifications made to
CheckedOutputStream
to create a filter that works onDataOutput
objects.
CheckedDataInput
versus CheckedInputStream
CheckedDataInput
requires the same changes asCheckedDataOuput
, as follows:In addition to these changes, the
CheckedDataInput
does not derive fromFilterInputStream
. Instead, it implements theDataInput
interface instead.CheckedDataInput
declares a private variable to hold aDataInput
object, which it wraps.- The constructor for
CheckedDataInput
requires aDataInput
object rather than anInputStream
.read
methods are changed.CheckedInputStream
from the original example implements tworead
methods, one for reading a single byte and one for reading a byte array. TheDataInput
interface has methods that implement the same functionality, but they have different names and different method signatures. Thus, theread
methods in theCheckedDataInput
class have new names and method signatures:Also, thepublic byte readByte() throws IOException { byte b = in.readByte(); cksum.update(b); return b; } public void readFully(byte[] b) throws IOException { in.readFully(b, 0, b.length); cksum.update(b, 0, b.length); } public void readFully(byte[] b, int off, int len) throws IOException { in.readFully(b, off, len); cksum.update(b, off, len); }DataInput
interface declares many other methods that we don’t implement for this example.
Finally, this example has two main programs to test the new filters:These two main programs differ only in the type of object they open the checksum filters on.
CheckedDIDemo
, which runs the filters on sequential access files (DataInputStream
andDataOutputStream
objects)CheckedRAFDemo
, which runs the filters on random access files (RandomAccessFiles
objects)CheckedDIDemo
creates aDataInputStream
and aDataOutputStream
and uses the checksum filter on them, as in the following code:in = new CheckedDataInput(new DataInputStream( new FileInputStream("farrago.txt")), inChecker); out = new CheckedDataOutput(new DataOutputStream( new FileOutputStream("outagain.txt")), outChecker);CheckedRAFDemo
creates twoRandomAccessFile
objects: one for reading and one for writing. It uses the checksum filter on them as follows:in = new CheckedDataInput( new RandomAccessFile("farrago.txt", "r"), inChecker); out = new CheckedDataOutput( new RandomAccessFile("outagain.txt", "rw"), outChecker);When you run either of these programs you should see the following output:
Input stream check sum: 736868089 Output stream check sum: 736868089
Start of Tutorial > Start of Trail > Start of Lesson |
Search
Feedback Form |
Copyright 1995-2002 Sun Microsystems, Inc. All rights reserved.