1 7 8 package java.io; 9 10 11 32 33 public abstract class Writer implements Appendable , Closeable , Flushable { 34 35 38 private char[] writeBuffer; 39 40 43 private final int writeBufferSize = 1024; 44 45 52 protected Object lock; 53 54 58 protected Writer() { 59 this.lock = this; 60 } 61 62 68 protected Writer(Object lock) { 69 if (lock == null) { 70 throw new NullPointerException (); 71 } 72 this.lock = lock; 73 } 74 75 86 public void write(int c) throws IOException { 87 synchronized (lock) { 88 if (writeBuffer == null){ 89 writeBuffer = new char[writeBufferSize]; 90 } 91 writeBuffer[0] = (char) c; 92 write(writeBuffer, 0, 1); 93 } 94 } 95 96 103 public void write(char cbuf[]) throws IOException { 104 write(cbuf, 0, cbuf.length); 105 } 106 107 116 abstract public void write(char cbuf[], int off, int len) throws IOException ; 117 118 125 public void write(String str) throws IOException { 126 write(str, 0, str.length()); 127 } 128 129 138 public void write(String str, int off, int len) throws IOException { 139 synchronized (lock) { 140 char cbuf[]; 141 if (len <= writeBufferSize) { 142 if (writeBuffer == null) { 143 writeBuffer = new char[writeBufferSize]; 144 } 145 cbuf = writeBuffer; 146 } else { cbuf = new char[len]; 148 } 149 str.getChars(off, (off + len), cbuf, 0); 150 write(cbuf, 0, len); 151 } 152 } 153 154 181 public Writer append(CharSequence csq) throws IOException { 182 if (csq == null) 183 write("null"); 184 else 185 write(csq.toString()); 186 return this; 187 } 188 189 225 public Writer append(CharSequence csq, int start, int end) throws IOException { 226 CharSequence cs = (csq == null ? "null" : csq); 227 write(cs.subSequence(start, end).toString()); 228 return this; 229 } 230 231 250 public Writer append(char c) throws IOException { 251 write(c); 252 return this; 253 } 254 255 270 abstract public void flush() throws IOException ; 271 272 279 abstract public void close() throws IOException ; 280 281 } 282 | Popular Tags |