1 3 package jodd.servlet.filter; 4 5 import jodd.io.FastByteArrayOutputStream; 6 7 import java.io.ByteArrayOutputStream ; 8 import java.io.OutputStream ; 9 10 import javax.servlet.ServletOutputStream ; 11 12 16 public class ByteArrayOutputStreamWrapper extends ServletOutputStream { 17 protected OutputStream intStream; 18 protected FastByteArrayOutputStream baStream; 19 protected boolean finallized = false; 20 protected boolean flushOnFinalizeOnly = true; 21 22 public ByteArrayOutputStreamWrapper(OutputStream outStream) { 23 intStream = outStream; 24 baStream = new FastByteArrayOutputStream(); 25 } 26 27 public ByteArrayOutputStreamWrapper() { 28 intStream = System.out; 29 baStream = new FastByteArrayOutputStream(); 30 } 31 32 public FastByteArrayOutputStream getByteArrayStream() { 33 return baStream; 34 } 35 36 public void setFinallized() { 37 finallized = true; 38 } 39 40 public boolean isFinallized() { 41 return finallized; 42 } 43 44 45 public void write(int i) throws java.io.IOException { 46 baStream.write(i); 47 } 48 49 public void close() throws java.io.IOException { 50 if (finallized) { 51 processStream(); 52 intStream.close(); 53 } 54 } 55 public void reset() { 56 baStream.reset(); 57 } 58 59 public void flush() throws java.io.IOException { 60 if (baStream.size() != 0) { 61 if (!flushOnFinalizeOnly || finallized) { 62 processStream(); 63 baStream = new FastByteArrayOutputStream(); 64 } 65 } 66 } 67 68 protected void processStream() throws java.io.IOException { 69 intStream.write(baStream.toByteArray()); 70 intStream.flush(); 71 } 72 73 public void clear() { 74 baStream = new FastByteArrayOutputStream(); 75 } 76 } 77 78 | Popular Tags |