KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * EchoStream.java
3  *
4  * Copyright (C) 2004 Peter Graves
5  * $Id: EchoStream.java,v 1.10 2004/06/22 23:07:46 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 public final class EchoStream extends Stream
25 {
26     private final Stream in;
27     private final Stream out;
28
29     private int unreadChar = -1;
30
31     public EchoStream(Stream in, Stream out)
32     {
33         this.in = in;
34         this.out = out;
35     }
36
37     public EchoStream(Stream in, Stream out, boolean interactive)
38     {
39         this.in = in;
40         this.out = out;
41         setInteractive(interactive);
42     }
43
44     public LispObject getElementType() throws ConditionThrowable
45     {
46         LispObject itype = in.getElementType();
47         LispObject otype = out.getElementType();
48         if (itype.equal(otype))
49             return itype;
50         return Symbol.NULL; // FIXME
51
}
52
53     public Stream getInputStream()
54     {
55         return in;
56     }
57
58     public Stream getOutputStream()
59     {
60         return out;
61     }
62
63     public LispObject typeOf()
64     {
65         return Symbol.ECHO_STREAM;
66     }
67
68     public LispClass classOf()
69     {
70         return BuiltInClass.ECHO_STREAM;
71     }
72
73     public LispObject typep(LispObject type) throws ConditionThrowable
74     {
75         if (type == Symbol.ECHO_STREAM)
76             return T;
77         if (type == BuiltInClass.ECHO_STREAM)
78             return T;
79         return super.typep(type);
80     }
81
82     public boolean isInputStream()
83     {
84         return true;
85     }
86
87     public boolean isOutputStream()
88     {
89         return true;
90     }
91
92     public boolean isCharacterInputStream() throws ConditionThrowable
93     {
94         return in.isCharacterInputStream();
95     }
96
97     public boolean isBinaryInputStream() throws ConditionThrowable
98     {
99         return in.isBinaryInputStream();
100     }
101
102     public boolean isCharacterOutputStream() throws ConditionThrowable
103     {
104         return out.isCharacterOutputStream();
105     }
106
107     public boolean isBinaryOutputStream() throws ConditionThrowable
108     {
109         return out.isBinaryOutputStream();
110     }
111
112     // Returns -1 at end of file.
113
protected int _readChar() throws ConditionThrowable
114     {
115         int n = in._readChar();
116         if (n >= 0) {
117             // Not at end of file.
118
if (unreadChar < 0)
119                 out._writeChar((char)n);
120             else
121                 unreadChar = -1;
122         }
123         return n;
124     }
125
126     protected void _unreadChar(int n) throws ConditionThrowable
127     {
128         in._unreadChar(n);
129         unreadChar = n;
130     }
131
132     protected boolean _charReady() throws ConditionThrowable
133     {
134         return in._charReady();
135     }
136
137     public void _writeChar(char c) throws ConditionThrowable
138     {
139         out._writeChar(c);
140     }
141
142     public void _writeChars(char[] chars, int start, int end)
143         throws ConditionThrowable
144     {
145         out._writeChars(chars, start, end);
146     }
147
148     public void _writeString(String JavaDoc s) throws ConditionThrowable
149     {
150         out._writeString(s);
151     }
152
153     public void _writeLine(String JavaDoc s) throws ConditionThrowable
154     {
155         out._writeLine(s);
156     }
157
158     // Reads an 8-bit byte.
159
public int _readByte() throws ConditionThrowable
160     {
161         int n = in._readByte();
162         if (n >= 0)
163             out._writeByte(n);
164         return n;
165     }
166
167     // Writes an 8-bit byte.
168
public void _writeByte(int n) throws ConditionThrowable
169     {
170         out._writeByte(n);
171     }
172
173     public void _finishOutput() throws ConditionThrowable
174     {
175         out._finishOutput();
176     }
177
178     public void _clearInput() throws ConditionThrowable
179     {
180         in._clearInput();
181     }
182
183     public LispObject close(LispObject abort) throws ConditionThrowable
184     {
185         // "The effect of CLOSE on a constructed stream is to close the
186
// argument stream only. There is no effect on the constituents of
187
// composite streams."
188
setOpen(false);
189         return T;
190     }
191
192     public LispObject listen() throws ConditionThrowable
193     {
194         return in.listen();
195     }
196
197     public LispObject freshLine() throws ConditionThrowable
198     {
199         return out.freshLine();
200     }
201
202     public String JavaDoc toString()
203     {
204         return unreadableString("ECHO-STREAM");
205     }
206
207     // ### make-echo-stream
208
// input-stream output-stream => echo-stream
209
private static final Primitive2 MAKE_ECHO_STREAM =
210         new Primitive2("make-echo-stream","input-stream output-stream") {
211         public LispObject execute(LispObject first, LispObject second)
212             throws ConditionThrowable
213         {
214             if (!(first instanceof Stream))
215                 return signal(new TypeError(first, Symbol.STREAM));
216             if (!(second instanceof Stream))
217                 return signal(new TypeError(second, Symbol.STREAM));
218             return new EchoStream((Stream) first, (Stream) second);
219         }
220     };
221
222     // ### echo-stream-input-stream
223
// echo-stream => input-stream
224
private static final Primitive1 ECHO_STREAM_INPUT_STREAM =
225         new Primitive1("echo-stream-input-stream","echo-stream") {
226         public LispObject execute(LispObject arg) throws ConditionThrowable
227         {
228             if (arg instanceof EchoStream)
229                 return ((EchoStream)arg).getInputStream();
230             return signal(new TypeError(arg, Symbol.ECHO_STREAM));
231         }
232     };
233
234     // ### echo-stream-output-stream
235
// echo-stream => output-stream
236
private static final Primitive1 ECHO_STREAM_OUTPUT_STREAM =
237         new Primitive1("echo-stream-output-stream","echo-stream") {
238         public LispObject execute(LispObject arg) throws ConditionThrowable
239         {
240             if (arg instanceof EchoStream)
241                 return ((EchoStream)arg).getOutputStream();
242             return signal(new TypeError(arg, Symbol.ECHO_STREAM));
243         }
244     };
245 }
246
Popular Tags