source: branches/0.17.x/abcl/src/org/armedbear/lisp/ConcatenatedStream.java

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

Remove 'throws ConditionThrowable?' method annotations:

it's an unchecked exception now, so no need to declare it thrown.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 7.6 KB
Line 
1/*
2 * ConcatenatedStream.java
3 *
4 * Copyright (C) 2004-2005 Peter Graves
5 * $Id: ConcatenatedStream.java 12254 2009-11-06 20:07:54Z 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 ConcatenatedStream extends Stream
37{
38    private LispObject streams;
39
40    private ConcatenatedStream(LispObject streams)
41    {
42        this.streams = streams;
43        isInputStream = true;
44    }
45
46    @Override
47    public boolean isCharacterInputStream()
48    {
49        if (streams == NIL)
50            return true;
51        return ((Stream)streams.car()).isCharacterInputStream();
52    }
53
54    @Override
55    public boolean isBinaryInputStream()
56    {
57        if (streams == NIL)
58            return true;
59        return ((Stream)streams.car()).isBinaryInputStream();
60    }
61
62    @Override
63    public boolean isCharacterOutputStream()
64    {
65        return false;
66    }
67
68    @Override
69    public boolean isBinaryOutputStream()
70    {
71        return false;
72    }
73
74    @Override
75    public LispObject typeOf()
76    {
77        return Symbol.CONCATENATED_STREAM;
78    }
79
80    @Override
81    public LispObject classOf()
82    {
83        return BuiltInClass.CONCATENATED_STREAM;
84    }
85
86    @Override
87    public LispObject typep(LispObject typeSpecifier)
88    {
89        if (typeSpecifier == Symbol.CONCATENATED_STREAM)
90            return T;
91        if (typeSpecifier == BuiltInClass.CONCATENATED_STREAM)
92            return T;
93        return super.typep(typeSpecifier);
94    }
95
96    @Override
97    public LispObject getElementType()
98    {
99        if (streams == NIL)
100            return NIL;
101        return ((Stream)streams.car()).getElementType();
102    }
103
104    @Override
105    public LispObject readCharNoHang(boolean eofError, LispObject eofValue)
106
107    {
108        if (streams == NIL) {
109            if (eofError)
110                return error(new EndOfFile(this));
111            else
112                return eofValue;
113        }
114  try 
115    {
116      return _charReady() ? readChar(eofError, eofValue) : NIL;
117    }
118  catch (java.io.IOException e)
119    {
120      return error(new StreamError(this, e));
121    }
122    }
123
124    @Override
125    public LispObject listen()
126    {
127        if (unreadChar >= 0)
128            return T;
129        if (streams == NIL)
130            return NIL;
131        LispObject obj = readCharNoHang(false, this);
132        if (obj == this)
133            return NIL;
134        unreadChar = ((LispCharacter)obj).getValue();
135        return T;
136    }
137
138    private int unreadChar = -1;
139
140    // Returns -1 at end of file.
141    @Override
142    protected int _readChar() throws java.io.IOException
143    {
144        int n;
145        if (unreadChar >= 0) {
146            n = unreadChar;
147            unreadChar = -1;
148            return n;
149        }
150        if (streams == NIL)
151            return -1;
152        Stream stream = (Stream) streams.car();
153        n = stream._readChar();
154        if (n >= 0)
155            return n;
156        streams = streams.cdr();
157        return _readChar();
158    }
159
160    @Override
161    protected void _unreadChar(int n)
162    {
163        if (unreadChar >= 0)
164            error(new StreamError(this, "UNREAD-CHAR was invoked twice consecutively without an intervening call to READ-CHAR."));
165        unreadChar = n;
166    }
167
168    @Override
169    protected boolean _charReady() throws java.io.IOException
170    {
171        if (unreadChar >= 0)
172            return true;
173        if (streams == NIL)
174            return false;
175        Stream stream = (Stream) streams.car();
176        if (stream._charReady())
177            return true;
178        LispObject remainingStreams = streams.cdr();
179        while (remainingStreams != NIL) {
180            stream = (Stream) remainingStreams.car();
181            if (stream._charReady())
182                return true;
183            remainingStreams = remainingStreams.cdr();
184        }
185        return false;
186    }
187
188    @Override
189    public void _writeChar(char c)
190    {
191        outputStreamError();
192    }
193
194    @Override
195    public void _writeChars(char[] chars, int start, int end)
196
197    {
198        outputStreamError();
199    }
200
201    @Override
202    public void _writeString(String s)
203    {
204        outputStreamError();
205    }
206
207    @Override
208    public void _writeLine(String s)
209    {
210        outputStreamError();
211    }
212
213    // Reads an 8-bit byte.
214    @Override
215    public int _readByte()
216    {
217        if (streams == NIL)
218            return -1;
219        Stream stream = (Stream) streams.car();
220        int n = stream._readByte();
221        if (n >= 0)
222            return n;
223        streams = streams.cdr();
224        return _readByte();
225    }
226
227    // Writes an 8-bit byte.
228    @Override
229    public void _writeByte(int n)
230    {
231        outputStreamError();
232    }
233
234    @Override
235    public void _finishOutput()
236    {
237        outputStreamError();
238    }
239
240    @Override
241    public void _clearInput()
242    {
243        // FIXME
244    }
245
246    private void outputStreamError()
247    {
248        error(new StreamError(this,
249                               String.valueOf(this) + " is not an output stream."));
250    }
251
252    // ### make-concatenated-stream &rest streams => concatenated-stream
253    private static final Primitive MAKE_CONCATENATED_STREAM =
254        new Primitive("make-concatenated-stream", "&rest streams")
255    {
256        @Override
257        public LispObject execute(LispObject[] args)
258        {
259            LispObject streams = NIL;
260            for (int i = 0; i < args.length; i++) {
261                if (args[i] instanceof Stream) {
262                    Stream stream = (Stream) args[i];
263                    if (stream.isInputStream()) {
264                        //                         streams[i] = (Stream) args[i];
265                        streams = new Cons(stream, streams);
266                        continue;
267                    }
268                }
269                error(new TypeError(String.valueOf(args[i]) +
270                                     " is not an input stream."));
271            }
272            return new ConcatenatedStream(streams.nreverse());
273        }
274    };
275
276    // ### concatenated-stream-streams concatenated-stream => streams
277    private static final Primitive CONCATENATED_STREAM_STREAMS =
278        new Primitive("concatenated-stream-streams", "concatenated-stream")
279    {
280        @Override
281        public LispObject execute(LispObject arg)
282        {
283            if (arg instanceof ConcatenatedStream) 
284                return ((ConcatenatedStream)arg).streams;
285            return error(new TypeError(arg, Symbol.CONCATENATED_STREAM));
286        }
287    };
288}
Note: See TracBrowser for help on using the repository browser.