1 21 22 package org.armedbear.lisp; 23 24 public class TwoWayStream extends Stream 25 { 26 private final Stream in; 27 private final Stream out; 28 29 public TwoWayStream(Stream in, Stream out) 30 { 31 this.in = in; 32 this.out = out; 33 isInputStream = true; 34 isOutputStream = true; 35 } 36 37 public TwoWayStream(Stream in, Stream out, boolean interactive) 38 { 39 this(in, out); 40 setInteractive(interactive); 41 } 42 43 public LispObject getElementType() throws ConditionThrowable 44 { 45 LispObject itype = in.getElementType(); 46 LispObject otype = out.getElementType(); 47 if (itype.equal(otype)) 48 return itype; 49 return list3(Symbol.AND, itype, otype); 50 } 51 52 public Stream getInputStream() 53 { 54 return in; 55 } 56 57 public Stream getOutputStream() 58 { 59 return out; 60 } 61 62 public boolean isCharacterInputStream() throws ConditionThrowable 63 { 64 return in.isCharacterInputStream(); 65 } 66 67 public boolean isBinaryInputStream() throws ConditionThrowable 68 { 69 return in.isBinaryInputStream(); 70 } 71 72 public boolean isCharacterOutputStream() throws ConditionThrowable 73 { 74 return out.isCharacterOutputStream(); 75 } 76 77 public boolean isBinaryOutputStream() throws ConditionThrowable 78 { 79 return out.isBinaryOutputStream(); 80 } 81 82 public LispObject typeOf() 83 { 84 return Symbol.TWO_WAY_STREAM; 85 } 86 87 public LispClass classOf() 88 { 89 return BuiltInClass.TWO_WAY_STREAM; 90 } 91 92 public LispObject typep(LispObject type) throws ConditionThrowable 93 { 94 if (type == Symbol.TWO_WAY_STREAM) 95 return T; 96 if (type == BuiltInClass.TWO_WAY_STREAM) 97 return T; 98 return super.typep(type); 99 } 100 101 protected int _readChar() throws ConditionThrowable 103 { 104 return in._readChar(); 105 } 106 107 protected void _unreadChar(int n) throws ConditionThrowable 108 { 109 in._unreadChar(n); 110 } 111 112 protected boolean _charReady() throws ConditionThrowable 113 { 114 return in._charReady(); 115 } 116 117 public void _writeChar(char c) throws ConditionThrowable 118 { 119 out._writeChar(c); 120 } 121 122 public void _writeChars(char[] chars, int start, int end) 123 throws ConditionThrowable 124 { 125 out._writeChars(chars, start, end); 126 } 127 128 public void _writeString(String s) throws ConditionThrowable 129 { 130 out._writeString(s); 131 } 132 133 public void _writeLine(String s) throws ConditionThrowable 134 { 135 out._writeLine(s); 136 } 137 138 public int _readByte() throws ConditionThrowable 140 { 141 return in._readByte(); 142 } 143 144 public void _writeByte(int n) throws ConditionThrowable 146 { 147 out._writeByte(n); 148 } 149 150 public void _finishOutput() throws ConditionThrowable 151 { 152 out._finishOutput(); 153 } 154 155 public void _clearInput() throws ConditionThrowable 156 { 157 in._clearInput(); 158 } 159 160 public LispObject listen() throws ConditionThrowable 161 { 162 return in.listen(); 163 } 164 165 public LispObject freshLine() throws ConditionThrowable 166 { 167 return out.freshLine(); 168 } 169 170 public LispObject close(LispObject abort) throws ConditionThrowable 171 { 172 setOpen(false); 176 return T; 177 } 178 179 public String writeToString() 180 { 181 return unreadableString("TWO-WAY-STREAM"); 182 } 183 184 private static final Primitive2 MAKE_TWO_WAY_STREAM = 187 new Primitive2("make-two-way-stream", "input-stream output-stream") 188 { 189 public LispObject execute(LispObject first, LispObject second) 190 throws ConditionThrowable 191 { 192 if (!(first instanceof Stream)) 193 return signal(new TypeError(first, Symbol.STREAM)); 194 if (!(second instanceof Stream)) 195 return signal(new TypeError(second, Symbol.STREAM)); 196 Stream in = (Stream) first; 197 if (!in.isInputStream()) 198 return signal(new TypeError(in, "input stream")); 199 Stream out = (Stream) second; 200 if (!out.isOutputStream()) 201 return signal(new TypeError(out, "output stream")); 202 return new TwoWayStream(in, out); 203 } 204 }; 205 206 private static final Primitive1 TWO_WAY_STREAM_INPUT_STREAM = 209 new Primitive1("two-way-stream-input-stream", "two-way-stream") 210 { 211 public LispObject execute(LispObject arg) throws ConditionThrowable 212 { 213 if (arg instanceof TwoWayStream) 214 return ((TwoWayStream)arg).getInputStream(); 215 return signal(new TypeError(arg, Symbol.TWO_WAY_STREAM)); 216 } 217 }; 218 219 private static final Primitive1 TWO_WAY_STREAM_OUTPUT_STREAM = 222 new Primitive1("two-way-stream-output-stream", "two-way-stream") 223 { 224 public LispObject execute(LispObject arg) throws ConditionThrowable 225 { 226 if (arg instanceof TwoWayStream) 227 return ((TwoWayStream)arg).getOutputStream(); 228 return signal(new TypeError(arg, Symbol.TWO_WAY_STREAM)); 229 } 230 }; 231 } 232 | Popular Tags |