001: /*
002: * TwoWayStream.java
003: *
004: * Copyright (C) 2003 Peter Graves
005: * $Id: TwoWayStream.java,v 1.6 2003/11/15 11:03:34 beedlem Exp $
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License
009: * as published by the Free Software Foundation; either version 2
010: * of the License, or (at your option) any later version.
011: *
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
015: * GNU General Public License for more details.
016: *
017: * You should have received a copy of the GNU General Public License
018: * along with this program; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
020: */
021:
022: package org.armedbear.lisp;
023:
024: public final class TwoWayStream extends LispStream {
025: private final LispInputStream in;
026: private final LispOutputStream out;
027:
028: public TwoWayStream(LispInputStream in, LispOutputStream out) {
029: this .in = in;
030: this .out = out;
031: }
032:
033: public LispInputStream getInputStream() {
034: return in;
035: }
036:
037: public LispOutputStream getOutputStream() {
038: return out;
039: }
040:
041: public LispObject typeOf() {
042: return Symbol.TWO_WAY_STREAM;
043: }
044:
045: public LispClass classOf() {
046: return BuiltInClass.TWO_WAY_STREAM;
047: }
048:
049: public LispObject typep(LispObject type) throws ConditionThrowable {
050: if (type == Symbol.TWO_WAY_STREAM)
051: return T;
052: if (type == BuiltInClass.TWO_WAY_STREAM)
053: return T;
054: return super .typep(type);
055: }
056:
057: public LispObject close(LispObject abort) throws ConditionThrowable {
058: in.close(abort);
059: out.close(abort);
060: return T;
061: }
062:
063: // ### make-two-way-stream
064: // input-stream output-stream => two-way-stream
065: private static final Primitive2 MAKE_TWO_WAY_STREAM = new Primitive2(
066: "make-two-way-stream") {
067: public LispObject execute(LispObject first, LispObject second)
068: throws ConditionThrowable {
069: if (!(first instanceof LispInputStream))
070: throw new ConditionThrowable(new TypeError(first,
071: "input stream"));
072: if (!(second instanceof LispOutputStream))
073: throw new ConditionThrowable(new TypeError(second,
074: "output stream"));
075: return new TwoWayStream((LispInputStream) first,
076: (LispOutputStream) second);
077: }
078: };
079:
080: // ### two-way-stream-input-stream
081: // two-way-stream => input-stream
082: private static final Primitive1 TWO_WAY_STREAM_INPUT_STREAM = new Primitive1(
083: "two-way-stream-input-stream") {
084: public LispObject execute(LispObject arg)
085: throws ConditionThrowable {
086: if (arg instanceof TwoWayStream)
087: return ((TwoWayStream) arg).getInputStream();
088: throw new ConditionThrowable(new TypeError(arg,
089: "two-way-stream"));
090: }
091: };
092:
093: // ### two-way-stream-output-stream
094: // two-way-stream => output-stream
095: private static final Primitive1 TWO_WAY_STREAM_OUTPUT_STREAM = new Primitive1(
096: "two-way-stream-output-stream") {
097: public LispObject execute(LispObject arg)
098: throws ConditionThrowable {
099: if (arg instanceof TwoWayStream)
100: return ((TwoWayStream) arg).getOutputStream();
101: throw new ConditionThrowable(new TypeError(arg,
102: "two-way-stream"));
103: }
104: };
105: }
|