1 21 22 package org.armedbear.lisp; 23 24 import java.io.StringWriter ; 25 26 public final class StringOutputStream extends Stream 27 { 28 private final StringWriter stringWriter; 29 30 public StringOutputStream() 31 { 32 this(Symbol.CHARACTER); 33 } 34 35 private StringOutputStream(LispObject elementType) 36 { 37 this.elementType = elementType; 38 isInputStream = false; 39 isOutputStream = true; 40 isCharacterStream = true; 41 isBinaryStream = false; 42 setWriter(stringWriter = new StringWriter ()); 43 } 44 45 public LispObject typeOf() 46 { 47 return Symbol.STRING_OUTPUT_STREAM; 48 } 49 50 public LispClass classOf() 51 { 52 return BuiltInClass.STRING_OUTPUT_STREAM; 53 } 54 55 public LispObject typep(LispObject type) throws ConditionThrowable 56 { 57 if (type == Symbol.STRING_OUTPUT_STREAM) 58 return T; 59 if (type == Symbol.STRING_STREAM) 60 return T; 61 if (type == BuiltInClass.STRING_OUTPUT_STREAM) 62 return T; 63 if (type == BuiltInClass.STRING_STREAM) 64 return T; 65 return super.typep(type); 66 } 67 68 public void _writeChar(char c) throws ConditionThrowable 69 { 70 if (elementType == NIL) 71 writeError(); 72 super._writeChar(c); 73 } 74 75 public void _writeChars(char[] chars, int start, int end) 76 throws ConditionThrowable 77 { 78 if (elementType == NIL) 79 writeError(); 80 super._writeChars(chars, start, end); 81 } 82 83 public void _writeString(String s) throws ConditionThrowable 84 { 85 if (elementType == NIL) 86 writeError(); 87 super._writeString(s); 88 } 89 90 public void _writeLine(String s) throws ConditionThrowable 91 { 92 if (elementType == NIL) 93 writeError(); 94 super._writeLine(s); 95 } 96 97 private void writeError() throws ConditionThrowable 98 { 99 signal(new TypeError("Attempt to write to a string output stream of element type NIL.")); 100 } 101 102 protected long _getFilePosition() throws ConditionThrowable 103 { 104 if (elementType == NIL) 105 return 0; 106 return stringWriter.toString().length(); 107 } 108 109 public LispObject getString() throws ConditionThrowable 110 { 111 if (elementType == NIL) 112 return new NilVector(0); 113 StringBuffer sb = stringWriter.getBuffer(); 114 SimpleString s = new SimpleString(sb); 115 sb.setLength(0); 116 return s; 117 } 118 119 public String toString() 120 { 121 return unreadableString("STRING-OUTPUT-STREAM"); 122 } 123 124 private static final Primitive1 MAKE_STRING_OUTPUT_STREAM = 127 new Primitive1("%make-string-output-stream", PACKAGE_SYS, false, 128 "element-type") 129 { 130 public LispObject execute(LispObject arg) throws ConditionThrowable 131 { 132 return new StringOutputStream(arg); 133 } 134 }; 135 136 private static final Primitive1 GET_OUTPUT_STREAM_STRING = 139 new Primitive1("get-output-stream-string", "string-output-stream") 140 { 141 public LispObject execute(LispObject arg) throws ConditionThrowable 142 { 143 try { 144 return ((StringOutputStream)arg).getString(); 145 } 146 catch (ClassCastException e) { 147 return signal(new TypeError(this, Symbol.STRING_OUTPUT_STREAM)); 148 } 149 } 150 }; 151 } 152 | Popular Tags |