1 25 26 package org.snipsnap.serialization; 27 28 import java.io.IOException ; 29 import java.io.Writer ; 30 31 38 39 public class StringBufferWriter extends Writer { 40 41 private StringBuffer buf; 42 43 46 private boolean isClosed = false; 47 48 49 private void ensureOpen() { 50 54 } 55 56 public StringBufferWriter(StringBuffer buffer) { 57 buf = buffer; 58 lock = buf; 59 } 60 61 65 public StringBufferWriter() { 66 buf = new StringBuffer (); 67 lock = buf; 68 } 69 70 76 public StringBufferWriter(int initialSize) { 77 if (initialSize < 0) { 78 throw new IllegalArgumentException ("Negative buffer size"); 79 } 80 buf = new StringBuffer (initialSize); 81 lock = buf; 82 } 83 84 87 public void write(int c) { 88 ensureOpen(); 89 buf.append((char) c); 90 } 91 92 99 public void write(char cbuf[], int off, int len) { 100 ensureOpen(); 101 if ((off < 0) || (off > cbuf.length) || (len < 0) || 102 ((off + len) > cbuf.length) || ((off + len) < 0)) { 103 throw new IndexOutOfBoundsException (); 104 } else if (len == 0) { 105 return; 106 } 107 buf.append(cbuf, off, len); 108 } 109 110 113 public void write(String str) { 114 ensureOpen(); 115 buf.append(str); 116 } 117 118 125 public void write(String str, int off, int len) { 126 ensureOpen(); 127 buf.append(str.substring(off, off + len)); 128 } 129 130 133 public String toString() { 134 return buf.toString(); 135 } 136 137 142 public StringBuffer getBuffer() { 143 return buf; 144 } 145 146 149 public void flush() { 150 ensureOpen(); 151 } 152 153 157 public void close() throws IOException { 158 isClosed = true; 159 } 160 161 } 162 | Popular Tags |