KickJava   Java API By Example, From Geeks To Geeks.

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


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