The I/O unit communicates with the outside world using streams. Intuitively
streams are unbounded sequences of values. The java.io
package
supports streams extensively.
Underlying any Java stream is a stream of bytes. Such byte streams called
InputStream
and OutputStream
are low-level and rarely used directly
in applications. For our purposes, we would like to view the input stream as carrying
a sequence of numbers and we would also like to write arbitrary values to the output
stream. We can achieve this with a bit of work.
First, we move from byte streams to character streams using the classes
InputStreamReader
and OutputStreamWriter
. The read and
write operations on these streams handle only one character at a time (which may
correspond to more than one byte on the underlying byte stream). For efficiency,
these streams are usually wrapped using a BufferedReader
and
BufferedWriter
.
Next, we can automatically convert a stream of characters to a stream of "tokens"
where tokens represent entities such as strings, numbers, etc. This is achieved
using the rather flexible class StreamTokenizer
. The behavior of this
class can be parameterized by a number of flags so you will need to consult the API
before using it. Symmetrically, we can write arbitrary Java objects to a character
stream by first wrapping it in a PrintWriter
stream.
The I/O unit of the machine has a simple interface. It communicates with the outside
worlds using two streams. The methods getWriter
and getReader
return the high-level streams used to communicate with the outside world. The unit
can readData
from the input stream and printData
to the output
stream:
import java.io.*; public interface ioUnitI { InputStream DEFAULT_ISTREAM = System.in; OutputStream DEFAULT_OSTREAM = System.out; PrintWriter getWriter (); StreamTokenizer getReader (); Word readData () throws ioE; void writeData (Word w); }
All inputs to our machine are numbers that are converted to integers and stored
in the memory. A call to the method readData
may throw an exception if
the input cannot be parsed as a number. More importantly, the call will block waiting
for input if there are no characters to read from the input stream. This is consistent
with the intuitive behavior of programs: if a program is requesting an input value, then
it will wait (block) until that value is supplied.
Write the class ioUnit
that implements the interface ioUnitI.
You will need to read the documentation of the java.io
package (especially
the API for StreamTokenizer
) carefully using the guidelines above.
You should provide two constructors for the I/O unit. The first should take no arguments
and use the default streams. The second constructor should take an InputStream
and an OutputStream
as arguments.
The help files are at the URL:
http://www.cs.uoregon.edu/~sabry/duckMachine/ioUnit/
and include:
ioE.java
which is the exception thrown by readData()
,
ioUnitI.java
which contains the interface,
TestIO.java
which contains a small program to test your code.
Turn in the code for ioUnit.java
during class on Monday,
April 26th.
Last modified: 04/13/99 13:58:25
yreimer@cs.uoregon.edu