source: trunk/abcl/src/org/armedbear/lisp/EchoStream.java @ 11488

Last change on this file since 11488 was 11488, checked in by ehuelsmann, 16 years ago

Add @Override annotations.

Patch by: Douglas Miles

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 7.4 KB
Line 
1/*
2 * EchoStream.java
3 *
4 * Copyright (C) 2004-2005 Peter Graves
5 * $Id: EchoStream.java 11488 2008-12-27 10:50:33Z ehuelsmann $
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 final class EchoStream extends Stream
37{
38    private final Stream in;
39    private final Stream out;
40
41    private int unreadChar = -1;
42
43    public EchoStream(Stream in, Stream out)
44    {
45        this.in = in;
46        this.out = out;
47    }
48
49    public EchoStream(Stream in, Stream out, boolean interactive)
50    {
51        this.in = in;
52        this.out = out;
53        setInteractive(interactive);
54    }
55
56    @Override
57    public LispObject getElementType() throws ConditionThrowable
58    {
59        LispObject itype = in.getElementType();
60        LispObject otype = out.getElementType();
61        if (itype.equal(otype))
62            return itype;
63        return Symbol.NULL; // FIXME
64    }
65
66    public Stream getInputStream()
67    {
68        return in;
69    }
70
71    public Stream getOutputStream()
72    {
73        return out;
74    }
75
76    @Override
77    public LispObject typeOf()
78    {
79        return Symbol.ECHO_STREAM;
80    }
81
82    @Override
83    public LispObject classOf()
84    {
85        return BuiltInClass.ECHO_STREAM;
86    }
87
88    @Override
89    public LispObject typep(LispObject type) throws ConditionThrowable
90    {
91        if (type == Symbol.ECHO_STREAM)
92            return T;
93        if (type == BuiltInClass.ECHO_STREAM)
94            return T;
95        return super.typep(type);
96    }
97
98    @Override
99    public boolean isInputStream()
100    {
101        return true;
102    }
103
104    @Override
105    public boolean isOutputStream()
106    {
107        return true;
108    }
109
110    @Override
111    public boolean isCharacterInputStream() throws ConditionThrowable
112    {
113        return in.isCharacterInputStream();
114    }
115
116    @Override
117    public boolean isBinaryInputStream() throws ConditionThrowable
118    {
119        return in.isBinaryInputStream();
120    }
121
122    @Override
123    public boolean isCharacterOutputStream() throws ConditionThrowable
124    {
125        return out.isCharacterOutputStream();
126    }
127
128    @Override
129    public boolean isBinaryOutputStream() throws ConditionThrowable
130    {
131        return out.isBinaryOutputStream();
132    }
133
134    // Returns -1 at end of file.
135    @Override
136    protected int _readChar() throws ConditionThrowable
137    {
138        int n = in._readChar();
139        if (n >= 0) {
140            // Not at end of file.
141            if (unreadChar < 0)
142                out._writeChar((char)n);
143            else
144                unreadChar = -1;
145        }
146        return n;
147    }
148
149    @Override
150    protected void _unreadChar(int n) throws ConditionThrowable
151    {
152        in._unreadChar(n);
153        unreadChar = n;
154    }
155
156    @Override
157    protected boolean _charReady() throws ConditionThrowable
158    {
159        return in._charReady();
160    }
161
162    @Override
163    public void _writeChar(char c) throws ConditionThrowable
164    {
165        out._writeChar(c);
166    }
167
168    @Override
169    public void _writeChars(char[] chars, int start, int end)
170        throws ConditionThrowable
171    {
172        out._writeChars(chars, start, end);
173    }
174
175    @Override
176    public void _writeString(String s) throws ConditionThrowable
177    {
178        out._writeString(s);
179    }
180
181    @Override
182    public void _writeLine(String s) throws ConditionThrowable
183    {
184        out._writeLine(s);
185    }
186
187    // Reads an 8-bit byte.
188    @Override
189    public int _readByte() throws ConditionThrowable
190    {
191        int n = in._readByte();
192        if (n >= 0)
193            out._writeByte(n);
194        return n;
195    }
196
197    // Writes an 8-bit byte.
198    @Override
199    public void _writeByte(int n) throws ConditionThrowable
200    {
201        out._writeByte(n);
202    }
203
204    @Override
205    public void _finishOutput() throws ConditionThrowable
206    {
207        out._finishOutput();
208    }
209
210    @Override
211    public void _clearInput() throws ConditionThrowable
212    {
213        in._clearInput();
214    }
215
216    @Override
217    public LispObject close(LispObject abort) throws ConditionThrowable
218    {
219        // "The effect of CLOSE on a constructed stream is to close the
220        // argument stream only. There is no effect on the constituents of
221        // composite streams."
222        setOpen(false);
223        return T;
224    }
225
226    @Override
227    public LispObject listen() throws ConditionThrowable
228    {
229        return in.listen();
230    }
231
232    @Override
233    public LispObject freshLine() throws ConditionThrowable
234    {
235        return out.freshLine();
236    }
237
238    @Override
239    public String toString()
240    {
241        return unreadableString("ECHO-STREAM");
242    }
243
244    // ### make-echo-stream
245    // input-stream output-stream => echo-stream
246    private static final Primitive MAKE_ECHO_STREAM =
247        new Primitive("make-echo-stream", "input-stream output-stream")
248    {
249        @Override
250        public LispObject execute(LispObject first, LispObject second)
251            throws ConditionThrowable
252        {
253            if (!(first instanceof Stream))
254                return type_error(first, Symbol.STREAM);
255            if (!(second instanceof Stream))
256                return type_error(second, Symbol.STREAM);
257            return new EchoStream((Stream) first, (Stream) second);
258        }
259    };
260
261    // ### echo-stream-input-stream
262    // echo-stream => input-stream
263    private static final Primitive ECHO_STREAM_INPUT_STREAM =
264        new Primitive("echo-stream-input-stream", "echo-stream")
265    {
266        @Override
267        public LispObject execute(LispObject arg) throws ConditionThrowable
268        {
269            if (arg instanceof EchoStream)
270                return ((EchoStream)arg).getInputStream();
271            return type_error(arg, Symbol.ECHO_STREAM);
272        }
273    };
274
275    // ### echo-stream-output-stream
276    // echo-stream => output-stream
277    private static final Primitive ECHO_STREAM_OUTPUT_STREAM =
278        new Primitive("echo-stream-output-stream", "echo-stream")
279    {
280        @Override
281        public LispObject execute(LispObject arg) throws ConditionThrowable
282        {
283            if (arg instanceof EchoStream)
284                return ((EchoStream)arg).getOutputStream();
285            return type_error(arg, Symbol.ECHO_STREAM);
286        }
287    };
288}
Note: See TracBrowser for help on using the repository browser.