KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > armedbear > lisp > StringOutputStream


1 /*
2  * StringOutputStream.java
3  *
4  * Copyright (C) 2002-2004 Peter Graves
5  * $Id: StringOutputStream.java,v 1.14 2004/03/10 01:56:59 piso Exp $
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20  */

21
22 package org.armedbear.lisp;
23
24 import java.io.StringWriter JavaDoc;
25
26 public final class StringOutputStream extends Stream
27 {
28     private final StringWriter JavaDoc stringWriter;
29
30     public StringOutputStream()
31     {
32         this(Symbol.CHARACTER);
33     }
34
35     private StringOutputStream(LispObject elementType)
36     {
37         this.elementType = elementType;
38         isInputStream = false;
39         isOutputStream = true;
40         isCharacterStream = true;
41         isBinaryStream = false;
42         setWriter(stringWriter = new StringWriter JavaDoc());
43     }
44
45     public LispObject typeOf()
46     {
47         return Symbol.STRING_OUTPUT_STREAM;
48     }
49
50     public LispClass classOf()
51     {
52         return BuiltInClass.STRING_OUTPUT_STREAM;
53     }
54
55     public LispObject typep(LispObject type) throws ConditionThrowable
56     {
57         if (type == Symbol.STRING_OUTPUT_STREAM)
58             return T;
59         if (type == Symbol.STRING_STREAM)
60             return T;
61         if (type == BuiltInClass.STRING_OUTPUT_STREAM)
62             return T;
63         if (type == BuiltInClass.STRING_STREAM)
64             return T;
65         return super.typep(type);
66     }
67
68     public void _writeChar(char c) throws ConditionThrowable
69     {
70         if (elementType == NIL)
71             writeError();
72         super._writeChar(c);
73     }
74
75     public void _writeChars(char[] chars, int start, int end)
76         throws ConditionThrowable
77     {
78         if (elementType == NIL)
79             writeError();
80         super._writeChars(chars, start, end);
81     }
82
83     public void _writeString(String JavaDoc s) throws ConditionThrowable
84     {
85         if (elementType == NIL)
86             writeError();
87         super._writeString(s);
88     }
89
90     public void _writeLine(String JavaDoc s) throws ConditionThrowable
91     {
92         if (elementType == NIL)
93             writeError();
94         super._writeLine(s);
95     }
96
97     private void writeError() throws ConditionThrowable
98     {
99         signal(new TypeError("Attempt to write to a string output stream of element type NIL."));
100     }
101
102     protected long _getFilePosition() throws ConditionThrowable
103     {
104         if (elementType == NIL)
105             return 0;
106         return stringWriter.toString().length();
107     }
108
109     public LispObject getString() throws ConditionThrowable
110     {
111         if (elementType == NIL)
112             return new NilVector(0);
113         StringBuffer JavaDoc sb = stringWriter.getBuffer();
114         SimpleString s = new SimpleString(sb);
115         sb.setLength(0);
116         return s;
117     }
118
119     public String JavaDoc toString()
120     {
121         return unreadableString("STRING-OUTPUT-STREAM");
122     }
123
124     // ### %make-string-output-stream
125
// %make-string-output-stream element-type => string-stream
126
private static final Primitive1 MAKE_STRING_OUTPUT_STREAM =
127         new Primitive1("%make-string-output-stream", PACKAGE_SYS, false,
128                        "element-type")
129     {
130         public LispObject execute(LispObject arg) throws ConditionThrowable
131         {
132             return new StringOutputStream(arg);
133         }
134     };
135
136     // ### get-output-stream-string
137
// get-output-stream-string string-output-stream => string
138
private static final Primitive1 GET_OUTPUT_STREAM_STRING =
139         new Primitive1("get-output-stream-string", "string-output-stream")
140     {
141         public LispObject execute(LispObject arg) throws ConditionThrowable
142         {
143             try {
144                 return ((StringOutputStream)arg).getString();
145             }
146             catch (ClassCastException JavaDoc e) {
147                 return signal(new TypeError(this, Symbol.STRING_OUTPUT_STREAM));
148             }
149         }
150     };
151 }
152
Popular Tags