JavaPad in a nutshell Chapter 7, PadStreams


The streams PadInputStream and PadOutputStream.

There are two streams that comes with javaPad, one for input and one for output.
They are not ment for standalone use, but may be used in other applications,
and can be used anywhere a normal Input/OutputStream is expected(fileIO, networking).
This chapter will explain their working and examine the test application wich
follows.

To better understand the streams I've created a picture of each stream
fully described with text.
PadInputStream
PadOutputStream

I've created a toy example that shows how to use the streams in an application.
SecureTalkServer will listen on port 12250 on the loopback interface(127.0.0.1)
while SecureTalkClient will get a string from the user, encrypt it and send it to SecureTalkServer
wich decrypts it and prints the plaintext message. Both programs will use the HISTORY file as key


SecureTalkServer
The code for this program is in the file SecureTalkServer.java and you should read it while i 
explain briefly about it.
If you are not familiar with networking in java, a ServerSocket object listens on a port
and when another program connects it returns a Socket object.

First the keyfile is found, using a RandomAccessFile (line 24-13)
Then a PadInputStream is created but is pointing to null.(line 33)
The ServerSocket is then created with port 12250 as agrument (line 36)
the ServerSocket.accept() will wait until a connection is detected, 
and returns a socket object (line 39.
Now the socket object returned from ServerScoket.accpet() is given to PadInputStream
together with the keyfile.
The while loop starting at line 42, will put the decrypted data from PadInputStream.read
into a temporary variable called temp, wich is then tested to see if it's value is higher than zero.
If it is higher that zero, the data will be printed, if the temp variable is negative it
means that there is no data from the string, and the program will exit.


SecureTalkClient
The code for this program is in the file SecureTalkClient.java and i will also briefly explain
the workings of this program.

First it reads a plain string using the SavitchIn.readLine() (line 25)
(by Walter Savitch, in his excelent java book).
Then a keyfile is found as a RandomAccessFile (line 27-34).
Then the program tries to connect to the SecureTalkServer who is presumably running. (line 38)
a PadOutputStream is created, and given the OutputStream to the network socket and keyfile as agument,
the PadOutputStream will now be able to send data to the socket (line 44).
A simple for loop sends all chars in the string gotten from the user on line 25, to the write() method
of PadOutputStream, wich encrypts is with the keyfile, and sends the result to the socket.
Note: the write() needs an int argument, so a cast is needed from char.


That's it, feel free to use PadStreams in your applications, and if you know of any improvements
or find any bugs, please contact me at erlend@klogd.net


-End of chapter 7
<- Last Next ->