1 19 20 package org.netbeans.modules.schema2beansdev.gen; 21 22 import java.util.*; 23 import java.io.*; 24 25 public class GenBuffer extends Writer { 26 protected int INITIAL_BUFFER_CAPACITY = 512; 27 protected int curOut; 28 protected StringBuffer listOut[]; 29 protected int bufferCount; 30 protected Stack selectStack = new Stack(); 31 private boolean first = false; 32 private String separator = null; 33 34 37 public GenBuffer(int bufferCount) { 38 this.bufferCount = bufferCount; 39 listOut = new StringBuffer [bufferCount]; 40 privateInit(); 41 } 42 47 public GenBuffer(GenBuffer source) { 48 bufferCount = source.bufferCount; 49 listOut = new StringBuffer [bufferCount]; 50 INITIAL_BUFFER_CAPACITY = source.INITIAL_BUFFER_CAPACITY; 51 curOut = source.curOut; 52 first = source.first; 53 separator = source.separator; 54 privateInit(); 55 } 56 57 60 public void reset() { 61 privateInit(); 62 } 63 64 private void privateInit() { 65 for (int i = 0; i < bufferCount; i++) { 66 listOut[i] = new StringBuffer (); 67 listOut[i].ensureCapacity(INITIAL_BUFFER_CAPACITY); 68 } 69 } 70 71 76 public void insertAdditionalBuffers(int offset, int count) { 77 StringBuffer [] newListOut = new StringBuffer [bufferCount + count]; 78 System.arraycopy(listOut, 0, newListOut, 0, offset+1); 80 System.arraycopy(listOut, offset+1, newListOut, offset + 1 + count, bufferCount - offset - 1); 82 for (int i = 0; i < count; ++i) { 84 newListOut[offset + 1 + i] = new StringBuffer (); 85 newListOut[offset + 1 + i].ensureCapacity(INITIAL_BUFFER_CAPACITY); 86 } 87 bufferCount += count; 88 listOut = newListOut; 89 } 90 91 94 public void close() { 95 } 96 97 100 public void flush() { 101 } 102 103 107 public void select(int bufferNum) { 108 if (bufferNum >= bufferCount || bufferNum < 0) 109 throw new IllegalArgumentException ("Invalid bufferNum "+bufferNum+" out of "+bufferCount); 110 curOut = bufferNum; 111 } 112 113 public void pushSelect(int bufferNum) { 114 int prevOut = curOut; 115 select(bufferNum); 117 selectStack.push(new Integer (prevOut)); 118 } 119 120 public void popSelect() { 121 curOut = ((Integer ) selectStack.pop()).intValue(); 122 } 123 124 127 protected void beforeWriteHook() {} 128 129 132 public void write(boolean b) throws IOException { 133 beforeWriteHook(); 134 listOut[curOut].append(b); 135 } 136 137 140 public void write(char c) throws IOException { 141 beforeWriteHook(); 142 listOut[curOut].append(c); 143 } 144 145 148 public void write(char[] str) throws IOException { 149 beforeWriteHook(); 150 listOut[curOut].append(str); 151 } 152 153 156 public void write(char[] cbuf, int off, int len) throws IOException { 157 beforeWriteHook(); 158 listOut[curOut].append(cbuf, off, len); 159 } 160 161 164 public void write(double d) throws IOException { 165 beforeWriteHook(); 166 listOut[curOut].append(d); 167 } 168 169 172 public void write(float f) throws IOException { 173 beforeWriteHook(); 174 listOut[curOut].append(f); 175 } 176 177 private CharArrayWriter caw = null; 178 181 public void write(int i) throws IOException { 182 if (caw == null) 185 caw = new CharArrayWriter(2); 186 caw.write(i); 187 beforeWriteHook(); 188 listOut[curOut].append(caw.toString()); 189 caw.reset(); 190 } 191 192 195 public void write(long l) throws IOException { 196 write((int)l); 197 } 198 199 203 public void write(Object obj) throws IOException { 204 beforeWriteHook(); 205 listOut[curOut].append(obj); 206 } 207 208 211 public void write(String s) throws IOException { 212 beforeWriteHook(); 213 listOut[curOut].append(s); 214 } 215 216 219 public void write(StringBuffer s) throws IOException { 220 beforeWriteHook(); 221 listOut[curOut].append(s); 222 } 223 224 228 public void write(String s1, String s2) throws IOException { 229 beforeWriteHook(); 230 listOut[curOut].append(s1); 231 listOut[curOut].append(s2); 232 } 233 234 238 public void write(String s1, String s2, String s3) throws IOException { 239 beforeWriteHook(); 240 listOut[curOut].append(s1); 241 listOut[curOut].append(s2); 242 listOut[curOut].append(s3); 243 } 244 245 249 public void write(String s1, String s2, String s3, String s4) throws IOException { 250 beforeWriteHook(); 251 listOut[curOut].append(s1); 252 listOut[curOut].append(s2); 253 listOut[curOut].append(s3); 254 listOut[curOut].append(s4); 255 } 256 257 public void write(String str, int bufferNum) throws IOException { 258 if (bufferNum >= bufferCount || bufferNum < 0) 259 throw new IllegalArgumentException ("Invalid bufferNum "+bufferNum+" out of "+bufferCount); 260 beforeWriteHook(); 261 listOut[bufferNum].append(str); 262 } 263 264 274 public void setFirst(String separator) { 275 first = true; 276 this.separator = separator; 277 } 278 279 282 public void writeNext(String msg) throws IOException { 283 writeNext(); 284 write(msg); 285 } 286 287 290 public void writeNext(String msg1, String msg2) throws IOException { 291 writeNext(); 292 write(msg1); 293 write(msg2); 294 } 295 296 299 public void writeNext(String msg1, String msg2, String msg3) throws IOException { 300 writeNext(); 301 write(msg1); 302 write(msg2); 303 write(msg3); 304 } 305 306 310 public void writeNext() throws IOException { 311 if (first) 312 first = false; 313 else 314 write(separator); 315 } 316 317 320 public void writeTo(Writer out) throws IOException { 321 for (int i = 0; i < bufferCount; i++) 322 out.write(listOut[i].toString()); 323 } 324 325 328 public void writeTo(OutputStream out) throws IOException { 329 for (int i = 0; i < bufferCount; i++) 330 out.write(listOut[i].toString().getBytes()); 331 } 332 333 public void writeTo(StringBuffer out) { 334 for (int i = 0; i < bufferCount; i++) 335 out.append(listOut[i]); 336 } 337 338 public void writeTo(GenBuffer out) { 339 int minInCommonBufferCount = bufferCount; 340 if (out.bufferCount < bufferCount) 341 minInCommonBufferCount = out.bufferCount; 342 for (int i = 0; i < minInCommonBufferCount; i++) 343 out.listOut[i].append(listOut[i]); 344 if (out.bufferCount < bufferCount) { 345 for (int i = minInCommonBufferCount; i < bufferCount; i++) 348 out.listOut[minInCommonBufferCount-1].append(listOut[i]); 349 } else { 350 out.curOut = curOut; 351 } 352 out.first = first; 353 out.separator = separator; 354 } 355 356 359 public boolean anyContent() { 360 for (int i = 0; i < bufferCount; i++) 361 if (listOut[i].length() > 0) 362 return true; 363 return false; 364 } 365 366 public int getCurrentPosition() { 367 return listOut[curOut].length(); 368 } 369 370 public void truncateAtPosition(int pos) { 371 listOut[curOut].setLength(pos); 372 } 373 374 377 public StringBuffer getBuffer() { 378 return listOut[curOut]; 379 } 380 381 384 public void ensureCapacity(int minimumCapacity) { 385 for (int i = 0; i < bufferCount; i++) 386 listOut[i].ensureCapacity(minimumCapacity); 387 } 388 } 389 | Popular Tags |