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