|
|
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. BecauseRandomAccessFileimplements theDataInputand
DataOutputinterfaces, a side benefit is that the filtered stream will also work with other
DataInputandDataOutputstreams including some sequential access streams such asDataInputStreamandDataOutputStream.The example
CheckedIODemofrom 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 are
CheckedInputStreamand
CheckedOutputStream.
In the new example,
CheckedDataOutputis a rewrite of
CheckedOutputStream--it computes a checksum for data writen to the stream. However, it operates onDataOutputobjects instead of onOutputStreamobjects. Similarly,CheckedDataInputmodifies
CheckedInputStreamso that it now works onDataInputobjects instead ofInputStreamobjects.
CheckedDataOutput versus CheckedOutputStreamLet's look at howCheckedDataOutputdiffers fromCheckedOutputStream.The first difference in these two classes is that
CheckedDataOutputdoes not extendFilterOutputStream. Instead, it implements theDataOutputinterface.public class CheckedDataOutput implements DataOutput
Note: To keep the example code simple, theCheckedDataOutputclass actually provided in this lesson is not declared to implementDataOutput, because theDataOutputinterface specifies so many methods. However, theCheckedDataOutputclass as provided in the example does implement several ofDataOutput's methods to illustrate how it should work.Next,
CheckedDataOutputdeclares a private variable to hold aDataOutputobject.This is the object to which data will be written.private DataOutput out;The constructor for
CheckedDataOutputdiffers fromCheckedOutputStream's constructor in thatCheckedDataOutputis created on aDataOutputobject rather than on anOutputStream.This constructor does not callpublic CheckedDataOutput(DataOutput out, Checksum cksum) { this.cksum = cksum; this.out = out; }super(out)like theCheckedOutputStreamconstructor did, becauseCheckedDataOutputextends fromObjectrather than from a stream class.Those are the only modifications made to
CheckedOutputStreamto create a filter that works onDataOutputobjects.
CheckedDataInput versus CheckedInputStreamCheckedDataInputrequires the same changes asCheckedDataOuput, as follows:In addition to these changes, the
CheckedDataInputdoes not derive fromFilterInputStream. Instead, it implements theDataInputinterface instead.CheckedDataInputdeclares a private variable to hold aDataInputobject, which it wraps.- The constructor for
CheckedDataInputrequires aDataInputobject rather than anInputStream.readmethods are changed.CheckedInputStreamfrom the original example implements tworeadmethods, one for reading a single byte and one for reading a byte array. TheDataInputinterface has methods that implement the same functionality, but they have different names and different method signatures. Thus, thereadmethods in theCheckedDataInputclass 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); }DataInputinterface 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 (
DataInputStreamandDataOutputStreamobjects)CheckedRAFDemo, which runs the filters on random access files (
RandomAccessFilesobjects)CheckedDIDemocreates aDataInputStreamand aDataOutputStreamand 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);CheckedRAFDemocreates twoRandomAccessFileobjects: 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-2004 Sun Microsystems, Inc. All rights reserved.