Java Reference
In-Depth Information
Listing 8-11 presents a ScrambledInputStream class that performs trivial de-
cryption onits underlying input stream byunscrambling the underlying input stream's
scrambled bytes via a remapping operation.
The read() methodfirstreadsthescrambledbytefromitsunderlyinginputstream.
Ifthereturnedvalueis-1(endoffile),thisvalueisreturnedtoitscaller.Otherwise,the
byte is mapped to its unscrambled value, which is returned.
The read(byte[], int, int) methodissimilarto read() ,butstoresbytes
readfromtheunderlyinginputstreaminabytearray,takinganoffsetintothisarrayand
a length (number of bytes to read) into account.
Onceagain,-1mightbereturnedfromtheunderlying read() methodcall.Ifso,this
valuemustbereturned.Otherwise,eachbyteinthearrayismappedtoitsunscrambled
value, and the number of bytes read is returned.
Note It'sonlyessentialtooverride read() and read(byte[], int, int)
because FilterInputStream 's int read(byte[] b) methodisimplemented
via the latter method.
Listing 8-12 presents the source code to an UnScramble application for experi-
menting with ScrambledInputStream by unscrambling a source file's bytes and
writing these unscrambled bytes to a destination file.
Listing 8-12. Unscrambling a file's bytes
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Random;
class Unscramble
{
public static void main(String[] args)
{
if (args.length != 2)
{
System.err.println("usage: java unscramble sr-
cpath destpath");
return;
 
Search WWH ::




Custom Search