1 18 package org.apache.roller.ui.core.util; 19 20 import java.io.ByteArrayOutputStream ; 21 import java.io.OutputStream ; 22 23 import javax.servlet.ServletOutputStream ; 24 25 31 public class ByteArrayOutputStreamWrapper extends ServletOutputStream 32 { 33 protected OutputStream intStream; 34 protected ByteArrayOutputStream baStream; 35 protected boolean finallized = false; 36 protected boolean flushOnFinalizeOnly = true; 37 38 public ByteArrayOutputStreamWrapper(OutputStream outStream) 39 { 40 intStream = outStream; 41 baStream = new ByteArrayOutputStream (); 42 } 43 44 public ByteArrayOutputStreamWrapper() 45 { 46 intStream = System.out; 47 baStream = new ByteArrayOutputStream (); 48 } 49 50 public ByteArrayOutputStream getByteArrayStream() 51 { 52 return baStream; 53 } 54 55 public void setFinallized() 56 { 57 finallized = true; 58 } 59 60 public boolean isFinallized() 61 { 62 return finallized; 63 } 64 65 66 public void write(int i) throws java.io.IOException 67 { 68 baStream.write(i); 69 } 70 71 public void close() throws java.io.IOException 72 { 73 if (finallized) { 74 processStream(); 75 intStream.close(); 76 } 77 } 78 79 public void flush() throws java.io.IOException 80 { 81 if (baStream.size() != 0) { 82 if (!flushOnFinalizeOnly || finallized) { 83 processStream(); 84 baStream = new ByteArrayOutputStream (); 85 } 86 } 87 } 88 89 protected void processStream() throws java.io.IOException 90 { 91 intStream.write(baStream.toByteArray()); 92 intStream.flush(); 93 } 94 95 public void clear() 96 { 97 baStream = new ByteArrayOutputStream (); 98 } 99 } 100 | Popular Tags |