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