Java Reference
In-Depth Information
{
super(out);
if (map == null)
throw new NullPointerException("map is null");
if (map.length != 256)
throw new IllegalArgumentException("map.length !=
256");
this.map = map;
}
@Override
public void write(int b) throws IOException
{
out.write(map[b]);
}
}
Listing 8-9 presents a ScrambledOutputStream class that performs trivial en-
cryptiononitsinputstreambyscramblingtheinputstream'sbytesviaaremappingop-
eration. Its constructor takes a pair of arguments:
out identifies the output stream on which to write the scrambled bytes.
map identifiesanarrayof256byteintegervaluestowhichinputstreambytes
map.
Theconstructorfirstpassesits out argumenttothe FilterOutputStream par-
entviaa super(out) call.Itthenverifiesits map argument'sintegrity( map mustbe
nonnullandhavealengthof256—abytestreamoffersexactly256bytestomap)before
saving map .
The write() method is trivial: it calls the underlying output stream's write()
method with the byte to which argument b maps. FilterOutputStream declares
out tobe protected (forperformance),whichiswhyIcandirectlyaccessthisfield.
Note It's only essential to override write(int) because FilterOut-
putStream 'sothertwo write() methodsareimplementedintermsofthismethod.
Listing8-10 presentsthesourcecodetoa Scramble applicationforexperimenting
with scrambling a source file's bytes via ScrambledOutputStream and writing
these scrambled bytes to a destination file.
Search WWH ::




Custom Search