// =========================================================================== // Input.java © 1996 Metrowerks Inc. All rights reserved. // =========================================================================== // // Created: 1/17/97 // Author: Clinton Popetz // // These two classes just implement a simple System.in stream using a // TextField package com.mw.io; import java.awt.*; import java.io.*; import java.lang.*; public interface SystemInConstants { static final char marker = '\uFFFF'; static final char newLineChar = 0x000A; static final byte newLine = 0x0A; static final byte cr = 0x0D; } public class SystemInput extends Frame implements SystemInConstants{ static SystemInput sFrame; //no GC SettableStringInputStream stream; TextField field; public static void main(String [] argv) { if (sFrame == null) new SystemInput(); else sFrame.toFront(); } public Insets insets() { return new Insets (5, 5, 20, 5); } public SystemInput() { super("System.in"); sFrame = this; //no GC stream = new SettableStringInputStream(this); System.in = stream; field = new TextField(30); add("North", new Label("System input should be entered here.")); add("Center",field); pack(); show(); } public boolean handleEvent(Event e) { switch (e.id) { case Event.KEY_PRESS: // If we get a CMD-D, set a marker character in the buffer. if ((e.key == 'd' && (e.modifiers & Event.META_MASK) == Event.META_MASK) || e.key == newLine || e.key == cr) { char theCharArray[] = new char[1]; String theText = field.getText(); // add the newline to the buffer if an enter or return was pressed if ( e.key != 'd') { theText += newLineChar; } else { // add the marker character to the buffer theText += marker; } stream.MoreData(theText); field.setText(""); } return super.handleEvent(e); default: return super.handleEvent(e); } } public void toFront() { super.toFront(); field.requestFocus(); } } public class SettableStringInputStream extends InputStream implements SystemInConstants{ protected String buffer = ""; protected int pos; protected SystemInput mFrame; public SettableStringInputStream (SystemInput inNewFrame) { mFrame = inNewFrame; } public synchronized void MoreData(String data) { buffer += data; notify(); } public synchronized int read() throws IOException { try { if (buffer.length() <= pos) { mFrame.toFront(); wait(); } char c = buffer.charAt(pos++); if (c == marker || pos == buffer.length()) //used up this string, reset it { pos = 0; buffer = ""; } // If we find the marker char in the buffer, return -1 for EOF. if (c == marker) return -1; else return c; } catch (InterruptedException e) { return -1; } } }