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