source: branches/0.15.x/abcl/src/org/armedbear/lisp/TwoWayStream.java

Last change on this file was 11754, checked in by vvoutilainen, 16 years ago

Convert using ClassCastException? to checking instanceof.
Performance tests show this approach to be faster.
Patch by Douglas R. Miles. I modified the patch to
remove tabs, so indentation may be slightly off in places.
That's something that we need to handle separately, abcl
doesn't have a clear indentation policy.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 7.3 KB
Line 
1/*
2 * TwoWayStream.java
3 *
4 * Copyright (C) 2003-2005 Peter Graves
5 * $Id: TwoWayStream.java 11754 2009-04-12 10:53:39Z vvoutilainen $
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 * As a special exception, the copyright holders of this library give you
22 * permission to link this library with independent modules to produce an
23 * executable, regardless of the license terms of these independent
24 * modules, and to copy and distribute the resulting executable under
25 * terms of your choice, provided that you also meet, for each linked
26 * independent module, the terms and conditions of the license of that
27 * module.  An independent module is a module which is not derived from
28 * or based on this library.  If you modify this library, you may extend
29 * this exception to your version of the library, but you are not
30 * obligated to do so.  If you do not wish to do so, delete this
31 * exception statement from your version.
32 */
33
34package org.armedbear.lisp;
35
36public class TwoWayStream extends Stream
37{
38    public final Stream in;
39    public final Stream out;
40
41    public TwoWayStream(Stream in, Stream out)
42    {
43        this.in = in;
44        this.out = out;
45        isInputStream = true;
46        isOutputStream = true;
47    }
48
49    public TwoWayStream(Stream in, Stream out, boolean interactive)
50    {
51        this(in, out);
52        setInteractive(interactive);
53    }
54
55    @Override
56    public LispObject getElementType() throws ConditionThrowable
57    {
58        LispObject itype = in.getElementType();
59        LispObject otype = out.getElementType();
60        if (itype.equal(otype))
61            return itype;
62        return list(Symbol.AND, itype, otype);
63    }
64
65    public Stream getInputStream()
66    {
67        return in;
68    }
69
70    public Stream getOutputStream()
71    {
72        return out;
73    }
74
75    @Override
76    public boolean isCharacterInputStream() throws ConditionThrowable
77    {
78        return in.isCharacterInputStream();
79    }
80
81    @Override
82    public boolean isBinaryInputStream() throws ConditionThrowable
83    {
84        return in.isBinaryInputStream();
85    }
86
87    @Override
88    public boolean isCharacterOutputStream() throws ConditionThrowable
89    {
90        return out.isCharacterOutputStream();
91    }
92
93    @Override
94    public boolean isBinaryOutputStream() throws ConditionThrowable
95    {
96        return out.isBinaryOutputStream();
97    }
98
99    @Override
100    public LispObject typeOf()
101    {
102        return Symbol.TWO_WAY_STREAM;
103    }
104
105    @Override
106    public LispObject classOf()
107    {
108        return BuiltInClass.TWO_WAY_STREAM;
109    }
110
111    @Override
112    public LispObject typep(LispObject type) throws ConditionThrowable
113    {
114        if (type == Symbol.TWO_WAY_STREAM)
115            return T;
116        if (type == BuiltInClass.TWO_WAY_STREAM)
117            return T;
118        return super.typep(type);
119    }
120
121    // Returns -1 at end of file.
122    @Override
123    protected int _readChar() throws ConditionThrowable
124    {
125        return in._readChar();
126    }
127
128    @Override
129    protected void _unreadChar(int n) throws ConditionThrowable
130    {
131        in._unreadChar(n);
132    }
133
134    @Override
135    protected boolean _charReady() throws ConditionThrowable
136    {
137        return in._charReady();
138    }
139
140    @Override
141    public void _writeChar(char c) throws ConditionThrowable
142    {
143        out._writeChar(c);
144    }
145
146    @Override
147    public void _writeChars(char[] chars, int start, int end)
148        throws ConditionThrowable
149    {
150        out._writeChars(chars, start, end);
151    }
152
153    @Override
154    public void _writeString(String s) throws ConditionThrowable
155    {
156        out._writeString(s);
157    }
158
159    @Override
160    public void _writeLine(String s) throws ConditionThrowable
161    {
162        out._writeLine(s);
163    }
164
165    // Reads an 8-bit byte.
166    @Override
167    public int _readByte() throws ConditionThrowable
168    {
169        return in._readByte();
170    }
171
172    // Writes an 8-bit byte.
173    @Override
174    public void _writeByte(int n) throws ConditionThrowable
175    {
176        out._writeByte(n);
177    }
178
179    @Override
180    public void _finishOutput() throws ConditionThrowable
181    {
182        out._finishOutput();
183    }
184
185    @Override
186    public void _clearInput() throws ConditionThrowable
187    {
188        in._clearInput();
189    }
190
191    @Override
192    public LispObject listen() throws ConditionThrowable
193    {
194        return in.listen();
195    }
196
197    @Override
198    public LispObject freshLine() throws ConditionThrowable
199    {
200        return out.freshLine();
201    }
202
203    @Override
204    public LispObject close(LispObject abort) throws ConditionThrowable
205    {
206        // "The effect of CLOSE on a constructed stream is to close the
207        // argument stream only. There is no effect on the constituents of
208        // composite streams."
209        setOpen(false);
210        return T;
211    }
212
213    @Override
214    public String writeToString() throws ConditionThrowable
215    {
216        return unreadableString(Symbol.TWO_WAY_STREAM);
217    }
218
219    // ### make-two-way-stream input-stream output-stream => two-way-stream
220    private static final Primitive MAKE_TWO_WAY_STREAM =
221        new Primitive(Symbol.MAKE_TWO_WAY_STREAM, "input-stream output-stream")
222    {
223        @Override
224        public LispObject execute(LispObject first, LispObject second)
225            throws ConditionThrowable
226        {
227            final Stream in = checkStream(first);
228            final Stream out = checkStream(second);
229            if (!in.isInputStream())
230                return type_error(in, list(Symbol.SATISFIES,
231                                                 Symbol.INPUT_STREAM_P));
232            if (!out.isOutputStream())
233                return type_error(out, list(Symbol.SATISFIES,
234                                                  Symbol.OUTPUT_STREAM_P));
235            return new TwoWayStream(in, out);
236        }
237    };
238
239    // ### two-way-stream-input-stream two-way-stream => input-stream
240    private static final Primitive TWO_WAY_STREAM_INPUT_STREAM =
241        new Primitive(Symbol.TWO_WAY_STREAM_INPUT_STREAM, "two-way-stream")
242    {
243        @Override
244        public LispObject execute(LispObject arg) throws ConditionThrowable
245        {
246           if (arg instanceof TwoWayStream) 
247               return ((TwoWayStream)arg).in;               
248           return type_error(arg, Symbol.TWO_WAY_STREAM);
249        }
250    };
251
252    // ### two-way-stream-output-stream two-way-stream => output-stream
253    private static final Primitive TWO_WAY_STREAM_OUTPUT_STREAM =
254        new Primitive(Symbol.TWO_WAY_STREAM_OUTPUT_STREAM, "two-way-stream")
255    {
256        @Override
257        public LispObject execute(LispObject arg) throws ConditionThrowable
258        {
259           if (arg instanceof TwoWayStream) 
260               return ((TwoWayStream)arg).out;               
261           return type_error(arg, Symbol.TWO_WAY_STREAM);
262        }
263    };
264}
Note: See TracBrowser for help on using the repository browser.