20-CS-694 Advanced Programming Techniques Spring 2012
Streams

Interfaces, Exceptions, Graphics, Animation, Threads, Reflection, Networking, RMI, JDBC, JNI

All lectures
Stream Classes

This series shows how to use many of the classes associated with streams. Click on the class names in the following table to see class details.

DataInputStream InputStream OutputStream PrintWriter CharsetDecoder
BufferedReader InputStreamReader FilterInputStream KeyEvent CoderResult
KeyListener PipedReader PipedWriter Charset CharBuffer

1.   InputStreamTest.java
TextInputStream.java

  -   Class TextInputStream subclasses InputStream and implements read() as required. This basic class will be used in the next four applets when connecting to other Java-supplied classes to build a needed set of services for accessing an input stream. The TextInputStream class is simple: a JFrame is opened and a KeyListener is attached - when a key is pressed while the JFrame is in focus, the corresponding key code is sent into the stream. In this example codes are taken directly from the TextInputStream object and printed raw onto another "Output" JFrame.
 
2.   Reader.java
StreamReaderTest.java
TextInputStream.java
InputStreamReader.java

  -   The InputStreamReader is an intermediary between a raw InputStream and a class with useful services like a BufferedReader. It is shown here connected to the TextInputStream of the above applet.

CAUTION! Use the supplied InputStreamReader class if you are having difficulty!! The code below to the left assumes this. If you want to try the native InputStreamReader add import java.io.*; to the top of the StreamReaderTest.java file and remove the InputStreamReader class from the current directory.

 
3.   BufferedReaderTest.java
TextInputStream.java
InputStreamReader.java

  -   A BufferedReader is used to read lines of printable characters at a time from a TextInputStream.

CAUTION! see the caution above.

 
4.   DataInputStreamTest.java
TextInputStream.java
InputStreamReader.java

  -   Another example of how different stream reading services can be provided. Here, the DataInputStream class interprets raw bytes from a TextInputStream as doubles. Nothing happens until 8 bytes are typed in the Input Stream JFrame. Then a number appears in the Output JFrame.
 
5.   FilterStreamTest.java
OffByOneFilter.java
InputStreamReader.java
TextInputStream.java

  -   A filter can be inserted between an InputStream and a Reader such as a BufferedReader to manipulate the stream in some desired way. In this example the input stream is a TextInputStream and the manipulation is to add 1 to the ascii value of every byte except the newline character.

CAUTION! see the caution above

 
6.   Reader.java
ReaderTest.java
TextReader.java

  -   This example shows that stream readers can be customized too. A class called TextReader is created which places typed characters into a buffer supplied by a consumer of characters until either the buffer is filled or until a newline character is typed. Then the number of characters that were stored, including the newline, is returned.
 
7.   OutputStreamTest.java
TextOutputStream.java

  -   An OutputStream class called TextOutputStream accepts bytes from a stream and prints them in a JFrame. In this case, bytes with values ranging between 0-255 are printed.
 
8.   PrintWriterTest.java
TextOutputStream.java

  -   The "homemade" TextOutputStream class is compatible with existing Java-supplied stream classes such as PrintWriter.
 
9.   PipeDemo.java

  -   Up to now streams have been of the kind where a token is requested, the requestor waits for a producer to put the token into the stream, then finally grabs the token and continues. Similarly, a producer waits if tokens are not ready to be read by a requestor. Piped Readers and Writers do not require waiting on either end. Tokens are added to a stream regardless of the state of any requestors and requestors do not wait if there are no tokens in the stream. A useful application is a logging daemon which may have to log data from many streams.

In this example an application sends 10 "testing.." messages to a logger. Initially, a timer causes the logger not to check the piped connection for those messages. When it wakes up it grabs them and listens for more. The application hardly notices.