Java Reference
In-Depth Information
for (int i = 0; i < map.length; i++)
map[i] = i;
// Shuffle map.
Random r = new Random(0);
for (int i = 0; i < map.length; i++)
{
int n = r.nextInt(map.length);
int temp = map[i];
map[i] = map[n];
map[n] = temp;
}
return map;
}
}
Scramble
's
main()
methodfirstverifiesthenumberofcommand-linearguments:
the first argument identifies the source path of the file with unscrambled content; the
secondargumentidentifiesthedestinationpathofthefilethatstoresscrambledcontent.
Assumingthattwocommand-lineargumentshavebeenspecified,
main()
instanti-
ates
FileInputStream
,creatingafileinputstreamthat'sconnectedtothefileiden-
tified by
args[0]
.
Continuing,
main()
instantiates
FileOutputStream
, creating a file output
stream that's connected to the file identified by
args[1]
. It then instantiates
ScrambledOutputStream
, passing the
FileOutputStream
instance to
ScrambledOutputStream
's constructor.
Note
Whenastreaminstanceispassedtoanotherstreamclass'sconstructor,thetwo
streamsare
chained together
.Forexample,thescrambledoutputstreamischainedto
the file output stream.
main()
now enters a loop, reading bytes from the file input stream and writing
themtothescrambledoutputstreambycalling
ScrambledOutputStream
's
void
write(int b)
method. This loop continues until
FileInputStream
's
int
read()
method returns
-1
(end of file).
The try-with-resources statement closes the file input stream and scrambled output
stream by calling their
close()
methods. It doesn't call the file output stream's