001: /*
002: * @(#)Console.java 1.4 05/05/16
003: *
004: * Copyright (c) 1997-2005 Sun Microsystems, Inc. All Rights Reserved.
005: *
006: * See the file "LICENSE.txt" for information on usage and redistribution
007: * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
008: */
009: package pnuts.tools;
010:
011: import java.io.IOException;
012: import java.io.PipedReader;
013: import java.io.PipedWriter;
014: import java.io.Reader;
015: import java.io.Writer;
016:
017: /**
018: * General purpose Console
019: */
020: public class Console {
021: private final static int DEFAULT_LINE_BUFFER_SIZE = 40;
022: private ConsoleBuffer out;
023: private PipedReader in;
024: private PipedWriter pipe;
025: protected ConsoleUI ui;
026:
027: /**
028: * Constructor
029: */
030: public Console() {
031: out = new ConsoleBuffer(DEFAULT_LINE_BUFFER_SIZE);
032: pipe = new PipedWriter();
033: in = new PipedReader();
034: try {
035: pipe.connect(in);
036: } catch (IOException exc) {
037: exc.printStackTrace();
038: }
039: }
040:
041: /**
042: * Sets the UI object of this console
043: */
044: public void setConsoleUI(ConsoleUI ui) {
045: this .ui = ui;
046: }
047:
048: /**
049: * Gets the UI object of this console
050: */
051: public ConsoleUI getConsoleUI() {
052: return ui;
053: }
054:
055: /**
056: * Gets the Reader from this console
057: */
058: public Reader getReader() {
059: return in;
060: }
061:
062: /**
063: * Gets the OutputStream from this console
064: */
065: public Writer getWriter() {
066: return out;
067: }
068:
069: /**
070: * Sends the specified string to the scripting engine
071: */
072: public void enter(String str) throws IOException {
073: char[] carray = str.toCharArray();
074: enter(carray, 0, carray.length);
075: }
076:
077: /**
078: * Sends the specified characters to the scripting engine
079: */
080: public void enter(char[] cbuf, int offset, int size)
081: throws IOException {
082: pipe.write(cbuf, offset, size);
083: pipe.write("\n");
084: pipe.flush();
085: }
086:
087: class ConsoleBuffer extends Writer {
088: private StringBuffer buf = new StringBuffer();
089: private int lineBufferSize;
090: private int pendingLines = 0;
091:
092: ConsoleBuffer() {
093: this (0);
094: }
095:
096: ConsoleBuffer(int lineBufferSize) {
097: this .lineBufferSize = lineBufferSize;
098: }
099:
100: public synchronized void write(int ch) {
101: buf.append((char) ch);
102: if (lineBufferSize > 0 && ch == '\n') {
103: if (++pendingLines > lineBufferSize) {
104: flushBuffer();
105: pendingLines = 0;
106: }
107: }
108: }
109:
110: public synchronized void write(char[] data, int off, int len) {
111: for (int i = off; i < len; i++) {
112: buf.append(data[i]);
113: if (lineBufferSize > 0 && data[i] == '\n') {
114: if (++pendingLines > lineBufferSize) {
115: flushBuffer();
116: pendingLines = 0;
117: }
118: }
119: }
120: }
121:
122: public synchronized void flush() throws IOException {
123: if (buf.length() > 0) {
124: flushBuffer();
125: }
126: }
127:
128: public void close() throws IOException {
129: flush();
130: }
131:
132: public int size() {
133: return buf.length();
134: }
135:
136: private void flushBuffer() {
137: final String str = buf.toString();
138: buf.setLength(0);
139: if (ui != null) {
140: ui.append(str);
141: }
142: }
143: }
144: }
|