1 package jodd.util; 2 3 import java.io.OutputStream; 4 import java.io.Serializable; 5 6 10 public class StringOutputStream extends OutputStream implements Serializable { 11 12 15 protected StringBuffer buf = null; 16 17 20 public StringOutputStream() { 21 super(); 22 buf = new StringBuffer(); 23 } 24 25 31 public String toString() { 32 return buf.toString(); 33 } 34 35 38 public void close() { 39 buf = null; 40 41 } 42 43 48 public void write(byte[] b) { 49 buf.append(CharUtil.toCharArray(b)); 50 } 51 52 59 public void write(byte[] b, int off, int len) { 60 if ((off < 0) || (len < 0) || (off + len) > b.length) { 61 throw new IndexOutOfBoundsException("StringOutputStream.write: Parameters out of bounds."); 62 } 63 byte[] bytes = new byte[len]; 64 for (int i = 0; i < len; i++) { 65 bytes[i] = b[off]; 66 off++; 67 } 68 buf.append(CharUtil.toCharArray(bytes)); 69 } 70 71 76 public void write(int b) { 77 buf.append((char)b); 78 } 79 } 80 81 82 | Popular Tags |