source: branches/0.16.x/abcl/src/org/armedbear/lisp/BroadcastStream.java

Last change on this file was 11754, checked in by vvoutilainen, 16 years ago

Convert using ClassCastException? to checking instanceof.
Performance tests show this approach to be faster.
Patch by Douglas R. Miles. I modified the patch to
remove tabs, so indentation may be slightly off in places.
That's something that we need to handle separately, abcl
doesn't have a clear indentation policy.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 7.8 KB
Line 
1/*
2 * BroadcastStream.java
3 *
4 * Copyright (C) 2004-2005 Peter Graves
5 * $Id: BroadcastStream.java 11754 2009-04-12 10:53:39Z vvoutilainen $
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 BroadcastStream extends Stream
37{
38    private final Stream[] streams;
39
40    private BroadcastStream(Stream[] streams) throws ConditionThrowable
41    {
42        this.streams = streams;
43        isOutputStream = true;
44        if (streams.length == 0) {
45            elementType = T;
46            isBinaryStream = true;
47            isCharacterStream = true;
48        } else {
49            elementType = streams[streams.length-1].getElementType();
50            if (elementType == Symbol.CHARACTER || elementType == Symbol.BASE_CHAR)
51                isCharacterStream = true;
52            else
53                isBinaryStream = true;
54        }
55    }
56
57    public Stream[] getStreams()
58    {
59        return streams;
60    }
61
62    @Override
63    public LispObject typeOf()
64    {
65        return Symbol.BROADCAST_STREAM;
66    }
67
68    @Override
69    public LispObject classOf()
70    {
71        return BuiltInClass.BROADCAST_STREAM;
72    }
73
74    @Override
75    public LispObject typep(LispObject typeSpecifier) throws ConditionThrowable
76    {
77        if (typeSpecifier == Symbol.BROADCAST_STREAM)
78            return T;
79        if (typeSpecifier == BuiltInClass.BROADCAST_STREAM)
80            return T;
81        return super.typep(typeSpecifier);
82    }
83
84    @Override
85    public LispObject listen() throws ConditionThrowable
86    {
87        notSupported();
88        // Not reached.
89        return NIL;
90    }
91
92    @Override
93    public LispObject fileLength() throws ConditionThrowable
94    {
95        if (streams.length > 0)
96            return streams[streams.length - 1].fileLength();
97        else
98            return Fixnum.ZERO;
99    }
100
101    @Override
102    public LispObject fileStringLength(LispObject arg) throws ConditionThrowable
103    {
104        if (streams.length > 0)
105            return streams[streams.length - 1].fileStringLength(arg);
106        else
107            return Fixnum.ONE;
108    }
109
110    // Returns -1 at end of file.
111    @Override
112    protected int _readChar() throws ConditionThrowable
113    {
114        notSupported();
115        // Not reached.
116        return -1;
117    }
118
119    @Override
120    protected void _unreadChar(int n) throws ConditionThrowable
121    {
122        notSupported();
123    }
124
125    @Override
126    protected boolean _charReady() throws ConditionThrowable
127    {
128        notSupported();
129        // Not reached.
130        return false;
131    }
132
133    @Override
134    public void _writeChar(char c) throws ConditionThrowable
135    {
136        for (int i = 0; i < streams.length; i++)
137            streams[i]._writeChar(c);
138    }
139
140    @Override
141    public void _writeChars(char[] chars, int start, int end)
142        throws ConditionThrowable
143    {
144        for (int i = 0; i < streams.length; i++)
145            streams[i]._writeChars(chars, start, end);
146    }
147
148    @Override
149    public void _writeString(String s) throws ConditionThrowable
150    {
151        for (int i = 0; i < streams.length; i++)
152            streams[i]._writeString(s);
153    }
154
155    @Override
156    public void _writeLine(String s) throws ConditionThrowable
157    {
158        for (int i = 0; i < streams.length; i++)
159            streams[i]._writeLine(s);
160    }
161
162    // Reads an 8-bit byte.
163    @Override
164    public int _readByte() throws ConditionThrowable
165    {
166        notSupported();
167        // Not reached.
168        return -1;
169    }
170
171    // Writes an 8-bit byte.
172    @Override
173    public void _writeByte(int n) throws ConditionThrowable
174    {
175        for (int i = 0; i < streams.length; i++)
176            streams[i]._writeByte(n);
177    }
178
179    @Override
180    public void _finishOutput() throws ConditionThrowable
181    {
182        for (int i = 0; i < streams.length; i++)
183            streams[i]._finishOutput();
184    }
185
186    @Override
187    public void _clearInput() throws ConditionThrowable
188    {
189        notSupported();
190    }
191
192    @Override
193    protected long _getFilePosition() throws ConditionThrowable
194    {
195        if (streams.length == 0)
196            return 0;
197        else
198            return streams[streams.length-1]._getFilePosition();
199    }
200
201    @Override
202    protected boolean _setFilePosition(LispObject arg) throws ConditionThrowable
203    {
204        return false;
205    }
206
207    @Override
208    public void _close() throws ConditionThrowable
209    {
210        setOpen(false);
211    }
212
213    private void notSupported() throws ConditionThrowable
214    {
215        error(new TypeError("Operation is not supported for streams of type BROADCAST-STREAM."));
216    }
217
218    @Override
219    public String writeToString()
220    {
221        return unreadableString("BROADCAST-STREAM");
222    }
223
224    // ### make-broadcast-stream &rest streams => broadcast-stream
225    private static final Primitive MAKE_BROADCAST_STREAM =
226        new Primitive("make-broadcast-stream", "&rest streams")
227    {
228        @Override
229        public LispObject execute() throws ConditionThrowable
230        {
231            return new BroadcastStream(new Stream[0]);
232        }
233        @Override
234        public LispObject execute(LispObject[] args) throws ConditionThrowable
235        {
236            Stream[] streams = new Stream[args.length];
237            for (int i = 0; i < args.length; i++) {
238                if (args[i] instanceof Stream) {
239                    if (((Stream)args[i]).isOutputStream()) {
240                        streams[i] = (Stream) args[i];
241                        continue;
242                    } else
243                        return error(new TypeError(args[i], list(Symbol.SATISFIES,
244                                                                   Symbol.OUTPUT_STREAM_P)));
245                } else
246                    return error(new TypeError(args[i], Symbol.STREAM));
247            }
248            // All is well.
249            return new BroadcastStream(streams);
250        }
251    };
252
253    // ### broadcast-stream-streams broadcast-stream => streams
254    private static final Primitive BROADCAST_STREAM_STREAMS =
255        new Primitive("broadcast-stream-streams", "broadcast-stream")
256    {
257        @Override
258        public LispObject execute(LispObject arg) throws ConditionThrowable
259        {
260            if (arg instanceof BroadcastStream) {
261                BroadcastStream stream = (BroadcastStream) arg;
262                Stream[] streams = stream.streams;
263                LispObject result = NIL;
264                for (int i = streams.length; i-- > 0;)
265                    result = new Cons(streams[i], result);
266                return result;
267            }
268            return error(new TypeError(arg, Symbol.BROADCAST_STREAM));
269        }
270    };
271}
Note: See TracBrowser for help on using the repository browser.