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