InputStream
Previous
Next
InputStreamTest.java
TextInputStream.java
InputStream Class
// Create class TextInputStream which subclasses InputStream and implements
// read() as required. This basic class will be used in the next four demos
// 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 we take the codes directly from the
// TextInputStream object and print them raw onto another "Output" JFrame.
//
// The following is in file TextInputStream.java
import java.io.InputStream;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class TextFrame extends Frame implements KeyListener {
JTextArea text;
int chr;
boolean char_ready;
int i=1;
TextFrame () {
setLayout(new BorderLayout());
add ("North", new Label("Input Stream"));
add ("Center", new JScrollPane(text = new JTextArea(20,30)));
text.addKeyListener(this);
setSize(300,300);
setVisible(true);
}
public void keyReleased (KeyEvent e) { }
public void keyPressed (KeyEvent e) { }
// The thread that will own this method is the AWT-EventQueue
// This method is invoked when a key is typed while the TextFrame
// object is in focus.
synchronized public void keyTyped (KeyEvent e) {
if (char_ready) return;
if (i++ == 30) { text.append ("\n"); i = 0; }
chr = (int)e.getKeyChar();
char_ready = true;
notify();
}
// The thread that will own this is the Applet (InputStreamTest object)
synchronized int read () {
if (!char_ready) try { wait (); } catch (Exception e) { }
char_ready = false;
return chr;
}
}
// Required to implement the read() method. We also implement read(byte[] chr)
// in advance of its being needed for the BufferedReader test that will be
// made later.
class TextInputStream extends InputStream {
TextFrame tf;
TextInputStream () { tf = new TextFrame(); }
public int read () { return tf.read(); }
public int read(byte[] chr) { chr[0] = (byte)tf.read(); return 1; }
}
// The following is in file InputStreamTest.java. This is where the above
// class is used
import java.awt.*;
import javax.swing.*;
import java.applet.*;
class InputStreamFrame extends JFrame implements Runnable {
JTextArea out;
Thread runner;
public InputStreamFrame () {
setLayout(new BorderLayout());
add ("North", new JLabel("Output", JLabel.CENTER));
add ("Center", new JScrollPane(out = new JTextArea(20,25)));
setVisible(true);
}
public void start () {
runner = new Thread(this);
runner.start();
}
public void run () {
TextInputStream is = new TextInputStream();
try {
for (int i=1 ; ; i++) {
int in = is.read();
out.append(in+" ");
if (i == 7) { out.append ("\n"); i = 0; }
}
}
catch (Exception e) { out.append ("\n"+e); }
}
public void finalize () throws Throwable { super.finalize(); }
}
public class InputStreamTest extends Applet {
public void init () {
InputStreamFrame inp = new InputStreamFrame();
inp.setSize(300,300);
inp.setVisible(true);
inp.start();
}
}