1 7 8 package java.io; 9 10 11 23 24 public class StringWriter extends Writer { 25 26 private StringBuffer buf; 27 28 32 public StringWriter() { 33 buf = new StringBuffer (); 34 lock = buf; 35 } 36 37 43 public StringWriter(int initialSize) { 44 if (initialSize < 0) { 45 throw new IllegalArgumentException ("Negative buffer size"); 46 } 47 buf = new StringBuffer (initialSize); 48 lock = buf; 49 } 50 51 54 public void write(int c) { 55 buf.append((char) c); 56 } 57 58 65 public void write(char cbuf[], int off, int len) { 66 if ((off < 0) || (off > cbuf.length) || (len < 0) || 67 ((off + len) > cbuf.length) || ((off + len) < 0)) { 68 throw new IndexOutOfBoundsException (); 69 } else if (len == 0) { 70 return; 71 } 72 buf.append(cbuf, off, len); 73 } 74 75 78 public void write(String str) { 79 buf.append(str); 80 } 81 82 89 public void write(String str, int off, int len) { 90 buf.append(str.substring(off, off + len)); 91 } 92 93 117 public StringWriter append(CharSequence csq) { 118 if (csq == null) 119 write("null"); 120 else 121 write(csq.toString()); 122 return this; 123 } 124 125 157 public StringWriter append(CharSequence csq, int start, int end) { 158 CharSequence cs = (csq == null ? "null" : csq); 159 write(cs.subSequence(start, end).toString()); 160 return this; 161 } 162 163 179 public StringWriter append(char c) { 180 write(c); 181 return this; 182 } 183 184 187 public String toString() { 188 return buf.toString(); 189 } 190 191 196 public StringBuffer getBuffer() { 197 return buf; 198 } 199 200 203 public void flush() { 204 } 205 206 211 public void close() throws IOException { 212 } 213 214 } 215 | Popular Tags |