1 16 package org.mortbay.http; 17 import java.io.IOException ; 18 import java.io.OutputStream ; 19 import java.net.SocketException ; 20 21 import org.mortbay.util.ByteArrayISO8859Writer; 22 import org.mortbay.util.ByteBufferOutputStream; 23 import org.mortbay.util.OutputObserver; 24 25 26 31 public class BufferedOutputStream 32 extends ByteBufferOutputStream 33 implements HttpMessage.HeaderWriter 34 { 35 protected OutputStream _out; 36 protected ByteArrayISO8859Writer _httpMessageWriter; 37 private OutputObserver _commitObserver; 38 private boolean _commited ; 39 private int _preReserve; 40 private boolean _bypassBuffer ; 41 42 43 51 public BufferedOutputStream(OutputStream out, 52 int capacity, 53 int headerReserve, 54 int preReserve, 55 int postReserve) 56 { 57 super(capacity,headerReserve,postReserve); 58 _out=out; 59 _preReserve=preReserve; 60 _httpMessageWriter = new ByteArrayISO8859Writer(headerReserve); 61 } 62 63 64 public OutputStream getOutputStream() 65 { 66 return _out; 67 } 68 69 70 73 public OutputObserver getCommitObserver() 74 { 75 return _commitObserver; 76 } 77 78 79 82 public void setCommitObserver(OutputObserver commitObserver) 83 { 84 _commitObserver = commitObserver; 85 } 86 87 88 public boolean isCommitted() 89 { 90 return _commited; 91 } 92 93 94 98 public boolean getBypassBuffer() 99 { 100 return _bypassBuffer; 101 } 102 103 104 108 public void setBypassBuffer(boolean bypassBuffer) 109 { 110 _bypassBuffer = bypassBuffer; 111 } 112 113 114 public void writeHeader(HttpMessage httpMessage) 115 throws IOException 116 { 117 httpMessage.writeHeader(_httpMessageWriter); 118 if (_httpMessageWriter.size()>capacity()) 119 throw new IllegalStateException ("Header too large"); 120 } 121 122 123 public void write(byte[] b) 124 throws IOException 125 { 126 write(b,0,b.length); 127 } 128 129 130 public void write(byte[] b, int offset, int length) 131 throws IOException 132 { 133 int o=offset; 134 int l=length; 135 while (l>0) 136 { 137 int c=capacity(); 138 139 if (_bypassBuffer && isCommitted() && size()==0 && l>c) 140 { 141 bypassWrite(b,o,l); 143 break; 144 } 145 146 if (l<c || !isFixed()) 147 { 148 super.write(b,o,l); 150 break; 151 } 152 else 153 { 154 super.write(b,o,c); 156 flush(); 157 l-=c; 158 o+=c; 159 } 160 } 161 } 162 163 164 protected void bypassWrite(byte[] b, int offset, int length) 165 throws IOException 166 { 167 try 168 { 169 _out.write(b,offset,length); 170 _out.flush(); 171 } 172 catch (IOException e) 173 { 174 throw new EOFException(e); 175 } 176 } 177 178 179 183 public void flush() 184 throws IOException 185 { 186 try 187 { 188 if (!_commited) 189 { 190 _commited=true; 191 if (_commitObserver!=null) 192 _commitObserver.outputNotify(this,OutputObserver.__COMMITING,null); 193 } 194 195 wrapBuffer(); 196 197 if (_httpMessageWriter.size()>0) 199 { 200 prewrite(_httpMessageWriter.getBuf(),0,_httpMessageWriter.size()); 201 _httpMessageWriter.resetWriter(); 202 } 203 204 if (size()>0) 205 writeTo(_out); 206 } 207 catch (IOException e) 208 { 209 throw new EOFException(e); 210 } 211 finally 212 { 213 reset(_preReserve); 214 } 215 } 216 217 218 219 225 protected void wrapBuffer() 226 throws IOException 227 { 228 } 229 230 231 public void close() 232 throws IOException 233 { 234 flush(); 235 _out.close(); 236 } 237 238 239 public void resetStream() 240 { 241 super.reset(_httpMessageWriter.capacity()); 242 _commited=false; 243 } 244 245 246 public void destroy() 247 { 248 super.destroy(); 249 if (_httpMessageWriter!=null) 250 _httpMessageWriter.destroy(); 251 _httpMessageWriter=null; 252 _out=null; 253 } 254 255 } 256 | Popular Tags |