Java Reference
In-Depth Information
close()
methodbecause
FilterOutputStream
automaticallycallstheunderly-
ing output stream's
close()
method.
The
makeMap()
method is responsible for creating the map array that's passed to
ScrambledOutputStream
'sconstructor.Theideaistopopulatethearraywithall
256 byte integer values, storing them in random order.
Note
I pass 0 as the seed argument when creating the
java.util.Random
object inordertoreturnapredictable sequence ofrandomnumbers.Ineedtousethe
samesequenceofrandomnumberswhencreatingthecomplementarymaparrayinthe
Unscramble
application, which I will present shortly. Unscrambling will not work
without the same sequence.
Supposeyouhaveasimple15-bytefilenamed
hello.txt
thatcontains“
Hello,
World!
” (followed by a carriage return and a line feed). When you execute
java
Scramble hello.txt hello.out
on an XP platform, you'll observe
Figure
Figure 8-5.
Different fonts yield different-looking scrambled output.
A
filter input stream
takesthedataobtainedfromitsunderlyinginputstream,which
mightbeanotherfilterinputstreamorasourceinputstreamsuchasafileinputstream,
filters it, and makes this data available via its
read()
methods (the output stream).
Filter input streams are created from subclasses of the concrete
FilterIn-
putStream
class, an
InputStream
subclass.
FilterInputStream
declares a
single
FilterInputStream(InputStream in)
constructorthatcreatesafilter
input stream built on top of
in
, the underlying input stream.
declare a constructor that passes its
InputStream
argument to
FilterIn-
putStream
'sconstructorandoverride
FilterInputStream
's
int read()
and
int read(byte[] b, int off, int len)
methods.
Listing 8-11.
Unscrambling a stream of bytes