1 21 22 package org.armedbear.j; 23 24 import org.armedbear.lisp.ConditionThrowable; 25 import org.armedbear.lisp.LispObject; 26 import org.armedbear.lisp.Stream; 27 import org.armedbear.lisp.Symbol; 28 29 public final class BufferStream extends Stream 30 { 31 private final Buffer buffer; 32 33 public BufferStream(Buffer buf) 34 { 35 buffer = buf; 36 elementType = Symbol.CHARACTER; 37 isCharacterStream = true; 38 isOutputStream = true; 39 } 40 41 public Buffer getBuffer() 42 { 43 return buffer; 44 } 45 46 public LispObject typeOf() 47 { 48 return LispAPI.BUFFER_STREAM; 49 } 50 51 57 public LispObject typep(LispObject typeSpecifier) throws ConditionThrowable 59 { 60 if (typeSpecifier == LispAPI.BUFFER_STREAM) 61 return T; 62 return super.typep(typeSpecifier); 63 } 64 65 public void _writeChar(char c) throws ConditionThrowable 66 { 67 try { 68 buffer.lockWrite(); 69 } 70 catch (InterruptedException e) { 71 Log.error(e); 72 return; 73 } 74 try { 75 switch (c) { 76 case '\r': 77 break; 78 case '\n': { 79 buffer.appendLine(""); 80 buffer.modified(); 81 buffer.needsRenumbering(true); 82 break; 83 } 84 default: { 85 Line line = buffer.getLastLine(); 86 int offset = line.length(); 87 FastStringBuffer sb = 88 new FastStringBuffer(line.getText()); 89 sb.append(c); 90 line.setText(sb.toString()); 91 buffer.modified(); 92 } 93 } 94 } 95 finally { 96 buffer.unlockWrite(); 97 } 98 } 99 100 public void _writeChars(char[] chars, int start, int end) 101 throws ConditionThrowable 102 { 103 _writeString(new String (chars, start, end - start)); 104 } 105 106 public void _writeString(String s) throws ConditionThrowable 107 { 108 try { 109 buffer.lockWrite(); 110 } 111 catch (InterruptedException e) { 112 Log.error(e); 113 return; 114 } 115 try { 116 buffer.append(s); 117 buffer.modified(); 118 if (s.indexOf('\n') >= 0) 119 buffer.needsRenumbering(true); 120 } 121 finally { 122 buffer.unlockWrite(); 123 } 124 } 125 126 public void _writeLine(String s) throws ConditionThrowable 127 { 128 try { 129 buffer.lockWrite(); 130 } 131 catch (InterruptedException e) { 132 Log.error(e); 133 return; 134 } 135 try { 136 buffer.append(s); 137 buffer.appendLine(""); 138 buffer.modified(); 139 buffer.needsRenumbering(true); 140 } 141 finally { 142 buffer.unlockWrite(); 143 } 144 } 145 146 public void _finishOutput() 147 { 148 if (buffer.needsRenumbering()) 149 buffer.renumber(); 150 buffer.repaint(); 151 } 152 153 public void _close() 154 { 155 _finishOutput(); 156 setOpen(false); 157 } 158 159 public String toString() 160 { 161 return unreadableString("BUFFER-STREAM"); 162 } 163 } 164 | Popular Tags |