001: /*
002: * StringOutputStream.java
003: *
004: * Copyright (C) 2002-2004 Peter Graves
005: * $Id: StringOutputStream.java,v 1.14 2004/03/10 01:56:59 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.lisp;
023:
024: import java.io.StringWriter;
025:
026: public final class StringOutputStream extends Stream {
027: private final StringWriter stringWriter;
028:
029: public StringOutputStream() {
030: this (Symbol.CHARACTER);
031: }
032:
033: private StringOutputStream(LispObject elementType) {
034: this .elementType = elementType;
035: isInputStream = false;
036: isOutputStream = true;
037: isCharacterStream = true;
038: isBinaryStream = false;
039: setWriter(stringWriter = new StringWriter());
040: }
041:
042: public LispObject typeOf() {
043: return Symbol.STRING_OUTPUT_STREAM;
044: }
045:
046: public LispClass classOf() {
047: return BuiltInClass.STRING_OUTPUT_STREAM;
048: }
049:
050: public LispObject typep(LispObject type) throws ConditionThrowable {
051: if (type == Symbol.STRING_OUTPUT_STREAM)
052: return T;
053: if (type == Symbol.STRING_STREAM)
054: return T;
055: if (type == BuiltInClass.STRING_OUTPUT_STREAM)
056: return T;
057: if (type == BuiltInClass.STRING_STREAM)
058: return T;
059: return super .typep(type);
060: }
061:
062: public void _writeChar(char c) throws ConditionThrowable {
063: if (elementType == NIL)
064: writeError();
065: super ._writeChar(c);
066: }
067:
068: public void _writeChars(char[] chars, int start, int end)
069: throws ConditionThrowable {
070: if (elementType == NIL)
071: writeError();
072: super ._writeChars(chars, start, end);
073: }
074:
075: public void _writeString(String s) throws ConditionThrowable {
076: if (elementType == NIL)
077: writeError();
078: super ._writeString(s);
079: }
080:
081: public void _writeLine(String s) throws ConditionThrowable {
082: if (elementType == NIL)
083: writeError();
084: super ._writeLine(s);
085: }
086:
087: private void writeError() throws ConditionThrowable {
088: signal(new TypeError(
089: "Attempt to write to a string output stream of element type NIL."));
090: }
091:
092: protected long _getFilePosition() throws ConditionThrowable {
093: if (elementType == NIL)
094: return 0;
095: return stringWriter.toString().length();
096: }
097:
098: public LispObject getString() throws ConditionThrowable {
099: if (elementType == NIL)
100: return new NilVector(0);
101: StringBuffer sb = stringWriter.getBuffer();
102: SimpleString s = new SimpleString(sb);
103: sb.setLength(0);
104: return s;
105: }
106:
107: public String toString() {
108: return unreadableString("STRING-OUTPUT-STREAM");
109: }
110:
111: // ### %make-string-output-stream
112: // %make-string-output-stream element-type => string-stream
113: private static final Primitive1 MAKE_STRING_OUTPUT_STREAM = new Primitive1(
114: "%make-string-output-stream", PACKAGE_SYS, false,
115: "element-type") {
116: public LispObject execute(LispObject arg)
117: throws ConditionThrowable {
118: return new StringOutputStream(arg);
119: }
120: };
121:
122: // ### get-output-stream-string
123: // get-output-stream-string string-output-stream => string
124: private static final Primitive1 GET_OUTPUT_STREAM_STRING = new Primitive1(
125: "get-output-stream-string", "string-output-stream") {
126: public LispObject execute(LispObject arg)
127: throws ConditionThrowable {
128: try {
129: return ((StringOutputStream) arg).getString();
130: } catch (ClassCastException e) {
131: return signal(new TypeError(this,
132: Symbol.STRING_OUTPUT_STREAM));
133: }
134: }
135: };
136: }
|