1 14 package org.wings.io; 15 16 import java.io.ByteArrayOutputStream ; 17 import java.io.IOException ; 18 import java.io.OutputStream ; 19 import java.io.Serializable ; 20 21 27 public final class StringBufferDevice implements Device, Serializable { 28 private StringBuffer buffer; 29 private ByteArrayOutputStream byteStream = null; 30 31 public StringBufferDevice() { 32 buffer = new StringBuffer (); 33 } 34 35 public String toString() { 36 flush(); 37 return buffer.toString(); 38 } 39 40 public boolean isSizePreserving() { return true; } 41 42 45 public void flush() { 46 if (byteStream != null) { 47 buffer.append(byteStream.toString()); 48 byteStream = null; 49 } 50 } 51 52 public void close() { 53 flush(); 54 } 55 56 public void reset() { 57 flush(); 58 buffer.setLength(0); 59 } 60 61 private OutputStream getStream() { 62 if (byteStream != null) 63 return byteStream; 64 byteStream = new ByteArrayOutputStream (); 65 return byteStream; 66 } 67 68 71 public Device print(String s) { 72 if (byteStream != null) flush(); 73 buffer.append(s); 74 return this; 75 } 76 77 80 public Device print(char c) { 81 if (byteStream != null) flush(); 82 buffer.append(c); 83 return this; 84 } 85 86 89 public Device print(char[] c) throws IOException { 90 if (byteStream != null) flush(); 91 buffer.append(c); 92 return this; 93 } 94 95 98 public Device print(char[] c, int start, int len) throws IOException { 99 if (byteStream != null) flush(); 100 buffer.append(c, start, len); 101 return this; 102 } 103 104 107 public Device print(int i) { 108 if (byteStream != null) flush(); 109 buffer.append(i); 110 return this; 111 } 112 113 116 public Device print(Object o) { 117 if (byteStream != null) flush(); 118 buffer.append(o); 119 return this; 120 } 121 122 125 public Device write(int c) throws IOException { 126 getStream().write(c); 127 return this; 128 } 129 130 134 public Device write(byte b[]) throws IOException { 135 getStream().write(b); 136 return this; 137 } 138 139 143 public Device write(byte b[], int off, int len) throws IOException { 144 getStream().write(b, off, len); 145 return this; 146 } 147 } 148 149 150 | Popular Tags |