1 | /* |
---|
2 | * ConcatenatedStream.java |
---|
3 | * |
---|
4 | * Copyright (C) 2004-2005 Peter Graves |
---|
5 | * $Id: ConcatenatedStream.java 14209 2012-10-21 16:09:53Z 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 (streams == NIL) |
---|
131 | return NIL; |
---|
132 | Stream stream = (Stream)streams.car(); |
---|
133 | return stream.listen(); |
---|
134 | } |
---|
135 | |
---|
136 | // Returns -1 at end of file. |
---|
137 | @Override |
---|
138 | protected int _readChar() throws java.io.IOException |
---|
139 | { |
---|
140 | int n; |
---|
141 | if (streams == NIL) |
---|
142 | return -1; |
---|
143 | Stream stream = (Stream) streams.car(); |
---|
144 | n = stream._readChar(); |
---|
145 | if (n >= 0) |
---|
146 | return n; |
---|
147 | streams = streams.cdr(); |
---|
148 | return _readChar(); |
---|
149 | } |
---|
150 | |
---|
151 | @Override |
---|
152 | protected void _unreadChar(int n) throws java.io.IOException |
---|
153 | { |
---|
154 | if (streams == NIL) |
---|
155 | error(new StreamError(this, "UNREAD-CHAR was invoked without a stream to unread into.")); |
---|
156 | Stream stream = (Stream)streams.car(); |
---|
157 | |
---|
158 | stream._unreadChar(n); |
---|
159 | } |
---|
160 | |
---|
161 | @Override |
---|
162 | protected boolean _charReady() throws java.io.IOException |
---|
163 | { |
---|
164 | if (streams == NIL) |
---|
165 | return false; |
---|
166 | Stream stream = (Stream) streams.car(); |
---|
167 | if (stream._charReady()) |
---|
168 | return true; |
---|
169 | LispObject remainingStreams = streams.cdr(); |
---|
170 | while (remainingStreams != NIL) { |
---|
171 | stream = (Stream) remainingStreams.car(); |
---|
172 | if (stream._charReady()) |
---|
173 | return true; |
---|
174 | remainingStreams = remainingStreams.cdr(); |
---|
175 | } |
---|
176 | return false; |
---|
177 | } |
---|
178 | |
---|
179 | @Override |
---|
180 | public void _writeChar(char c) |
---|
181 | { |
---|
182 | outputStreamError(); |
---|
183 | } |
---|
184 | |
---|
185 | @Override |
---|
186 | public void _writeChars(char[] chars, int start, int end) |
---|
187 | |
---|
188 | { |
---|
189 | outputStreamError(); |
---|
190 | } |
---|
191 | |
---|
192 | @Override |
---|
193 | public void _writeString(String s) |
---|
194 | { |
---|
195 | outputStreamError(); |
---|
196 | } |
---|
197 | |
---|
198 | @Override |
---|
199 | public void _writeLine(String s) |
---|
200 | { |
---|
201 | outputStreamError(); |
---|
202 | } |
---|
203 | |
---|
204 | // Reads an 8-bit byte. |
---|
205 | @Override |
---|
206 | public int _readByte() |
---|
207 | { |
---|
208 | if (streams == NIL) |
---|
209 | return -1; |
---|
210 | Stream stream = (Stream) streams.car(); |
---|
211 | int n = stream._readByte(); |
---|
212 | if (n >= 0) |
---|
213 | return n; |
---|
214 | streams = streams.cdr(); |
---|
215 | return _readByte(); |
---|
216 | } |
---|
217 | |
---|
218 | // Writes an 8-bit byte. |
---|
219 | @Override |
---|
220 | public void _writeByte(int n) |
---|
221 | { |
---|
222 | outputStreamError(); |
---|
223 | } |
---|
224 | |
---|
225 | @Override |
---|
226 | public void _finishOutput() |
---|
227 | { |
---|
228 | outputStreamError(); |
---|
229 | } |
---|
230 | |
---|
231 | @Override |
---|
232 | public void _clearInput() |
---|
233 | { |
---|
234 | // FIXME |
---|
235 | } |
---|
236 | |
---|
237 | private void outputStreamError() |
---|
238 | { |
---|
239 | error(new StreamError(this, |
---|
240 | String.valueOf(this) + " is not an output stream.")); |
---|
241 | } |
---|
242 | |
---|
243 | // ### make-concatenated-stream &rest streams => concatenated-stream |
---|
244 | private static final Primitive MAKE_CONCATENATED_STREAM = |
---|
245 | new Primitive("make-concatenated-stream", "&rest streams") |
---|
246 | { |
---|
247 | @Override |
---|
248 | public LispObject execute(LispObject[] args) |
---|
249 | { |
---|
250 | LispObject streams = NIL; |
---|
251 | for (int i = 0; i < args.length; i++) { |
---|
252 | if (args[i] instanceof Stream) { |
---|
253 | Stream stream = (Stream) args[i]; |
---|
254 | if (stream.isInputStream()) { |
---|
255 | // streams[i] = (Stream) args[i]; |
---|
256 | streams = new Cons(stream, streams); |
---|
257 | continue; |
---|
258 | } |
---|
259 | } |
---|
260 | error(new TypeError(String.valueOf(args[i]) + |
---|
261 | " is not an input stream.")); |
---|
262 | } |
---|
263 | return new ConcatenatedStream(streams.nreverse()); |
---|
264 | } |
---|
265 | }; |
---|
266 | |
---|
267 | // ### concatenated-stream-streams concatenated-stream => streams |
---|
268 | private static final Primitive CONCATENATED_STREAM_STREAMS = |
---|
269 | new Primitive("concatenated-stream-streams", "concatenated-stream") |
---|
270 | { |
---|
271 | @Override |
---|
272 | public LispObject execute(LispObject arg) |
---|
273 | { |
---|
274 | if (arg instanceof ConcatenatedStream) |
---|
275 | return ((ConcatenatedStream)arg).streams; |
---|
276 | return error(new TypeError(arg, Symbol.CONCATENATED_STREAM)); |
---|
277 | } |
---|
278 | }; |
---|
279 | } |
---|