001: /*
002: * BufferStream.java
003: *
004: * Copyright (C) 2004 Peter Graves
005: * $Id: BufferStream.java,v 1.1 2004/08/30 18:04:43 piso Exp $
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License
009: * as published by the Free Software Foundation; either version 2
010: * of the License, or (at your option) any later version.
011: *
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
015: * GNU General Public License for more details.
016: *
017: * You should have received a copy of the GNU General Public License
018: * along with this program; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
020: */
021:
022: package org.armedbear.j;
023:
024: import org.armedbear.lisp.ConditionThrowable;
025: import org.armedbear.lisp.LispObject;
026: import org.armedbear.lisp.Stream;
027: import org.armedbear.lisp.Symbol;
028:
029: public final class BufferStream extends Stream {
030: private final Buffer buffer;
031:
032: public BufferStream(Buffer buf) {
033: buffer = buf;
034: elementType = Symbol.CHARACTER;
035: isCharacterStream = true;
036: isOutputStream = true;
037: }
038:
039: public Buffer getBuffer() {
040: return buffer;
041: }
042:
043: public LispObject typeOf() {
044: return LispAPI.BUFFER_STREAM;
045: }
046:
047: // // FIXME
048: // public LispClass classOf()
049: // {
050: // return BuiltInClass.STREAM;
051: // }
052:
053: // FIXME
054: public LispObject typep(LispObject typeSpecifier)
055: throws ConditionThrowable {
056: if (typeSpecifier == LispAPI.BUFFER_STREAM)
057: return T;
058: return super .typep(typeSpecifier);
059: }
060:
061: public void _writeChar(char c) throws ConditionThrowable {
062: try {
063: buffer.lockWrite();
064: } catch (InterruptedException e) {
065: Log.error(e);
066: return;
067: }
068: try {
069: switch (c) {
070: case '\r':
071: break;
072: case '\n': {
073: buffer.appendLine("");
074: buffer.modified();
075: buffer.needsRenumbering(true);
076: break;
077: }
078: default: {
079: Line line = buffer.getLastLine();
080: int offset = line.length();
081: FastStringBuffer sb = new FastStringBuffer(line
082: .getText());
083: sb.append(c);
084: line.setText(sb.toString());
085: buffer.modified();
086: }
087: }
088: } finally {
089: buffer.unlockWrite();
090: }
091: }
092:
093: public void _writeChars(char[] chars, int start, int end)
094: throws ConditionThrowable {
095: _writeString(new String(chars, start, end - start));
096: }
097:
098: public void _writeString(String s) throws ConditionThrowable {
099: try {
100: buffer.lockWrite();
101: } catch (InterruptedException e) {
102: Log.error(e);
103: return;
104: }
105: try {
106: buffer.append(s);
107: buffer.modified();
108: if (s.indexOf('\n') >= 0)
109: buffer.needsRenumbering(true);
110: } finally {
111: buffer.unlockWrite();
112: }
113: }
114:
115: public void _writeLine(String s) throws ConditionThrowable {
116: try {
117: buffer.lockWrite();
118: } catch (InterruptedException e) {
119: Log.error(e);
120: return;
121: }
122: try {
123: buffer.append(s);
124: buffer.appendLine("");
125: buffer.modified();
126: buffer.needsRenumbering(true);
127: } finally {
128: buffer.unlockWrite();
129: }
130: }
131:
132: public void _finishOutput() {
133: if (buffer.needsRenumbering())
134: buffer.renumber();
135: buffer.repaint();
136: }
137:
138: public void _close() {
139: _finishOutput();
140: setOpen(false);
141: }
142:
143: public String toString() {
144: return unreadableString("BUFFER-STREAM");
145: }
146: }
|