1 16 package org.mortbay.http; 17 18 import java.io.FilterInputStream ; 19 import java.io.IOException ; 20 import java.io.InputStream ; 21 import java.io.OutputStream ; 22 import java.io.UnsupportedEncodingException ; 23 24 import org.apache.commons.logging.Log; 25 import org.mortbay.log.LogFactory; 26 import org.mortbay.util.LineInput; 27 import org.mortbay.util.StringUtil; 28 29 30 31 46 public class HttpInputStream extends FilterInputStream 47 { 48 private static Log log = LogFactory.getLog(HttpInputStream.class); 49 50 51 private static ClosedStream __closedStream=new ClosedStream(); 52 53 54 private ChunkingInputStream _deChunker; 55 private LineInput _realIn; 56 private boolean _chunking; 57 private OutputStream _expectContinues; 58 59 60 62 public HttpInputStream( InputStream in) 63 { 64 this(in,4096); 65 } 66 67 68 70 public HttpInputStream(InputStream in, int bufferSize) 71 { 72 super(null); 73 try { 74 _realIn= new LineInput(in,bufferSize,StringUtil.__ISO_8859_1); 75 } 76 catch(UnsupportedEncodingException e) 77 { 78 log.fatal(e); System.exit(1); 79 } 80 this.in=_realIn; 81 } 82 83 84 87 public OutputStream getExpectContinues() 88 { 89 return _expectContinues; 90 } 91 92 93 96 public void setExpectContinues(OutputStream expectContinues) 97 { 98 _expectContinues = expectContinues; 99 } 100 101 102 105 public int read() throws IOException 106 { 107 if (_expectContinues!=null) 108 expectContinues(); 109 return super.read(); 110 } 111 112 113 116 public int read(byte[] b, int off, int len) throws IOException 117 { 118 if (_expectContinues!=null) 119 expectContinues(); 120 return super.read(b, off, len); 121 } 122 123 124 127 public int read(byte[] b) throws IOException 128 { 129 if (_expectContinues!=null) 130 expectContinues(); 131 return super.read(b); 132 } 133 134 135 138 public long skip(long n) throws IOException 139 { 140 if (_expectContinues!=null) 141 expectContinues(); 142 return super.skip(n); 143 } 144 145 146 private void expectContinues() 147 throws IOException 148 { 149 try 150 { 151 if (available()<=0) 152 { 153 _expectContinues.write(HttpResponse.__Continue); 154 _expectContinues.flush(); 155 } 156 } 157 finally 158 { 159 _expectContinues=null; 160 } 161 162 } 163 164 165 170 public InputStream getInputStream() 171 { 172 return _realIn; 173 } 174 175 176 180 public InputStream getFilterStream() 181 { 182 return in; 183 } 184 185 186 190 public void setFilterStream(InputStream filter) 191 { 192 in=filter; 193 } 194 195 196 198 public boolean isChunking() 199 { 200 return _chunking; 201 } 202 203 204 209 public void setChunking() 210 throws IllegalStateException 211 { 212 if (_realIn.getByteLimit()>=0) 213 throw new IllegalStateException ("Has Content-Length"); 214 if (_deChunker==null) 215 _deChunker=new ChunkingInputStream(_realIn); 216 in=_deChunker; 217 218 _chunking=true; 219 _deChunker._trailer=null; 220 } 221 222 223 229 public void resetStream() 230 throws IllegalStateException 231 { 232 if ((_deChunker!=null && _deChunker._chunkSize>0) || 233 _realIn.getByteLimit()>0) 234 throw new IllegalStateException ("Unread input"); 235 if(log.isTraceEnabled())log.trace("resetStream()"); 236 in=_realIn; 237 if (_deChunker!=null) 238 _deChunker.resetStream(); 239 _chunking=false; 240 _realIn.setByteLimit(-1); 241 } 242 243 244 public void close() 245 throws IOException 246 { 247 in=__closedStream; 248 } 249 250 251 252 256 public void setContentLength(int len) 257 { 258 if (_chunking && len>=0 && getExpectContinues()==null) 259 throw new IllegalStateException ("Chunking"); 260 _realIn.setByteLimit(len); 261 } 262 263 264 void unsafeSetContentLength(int len) 265 { 266 _realIn.setByteLimit(len); 267 } 268 269 270 273 public int getContentLength() 274 { 275 return _realIn.getByteLimit(); 276 } 277 278 279 public HttpFields getTrailer() 280 { 281 return _deChunker._trailer; 282 } 283 284 285 public void destroy() 286 { 287 if (_realIn!=null) 288 _realIn.destroy(); 289 _realIn=null; 290 _deChunker=null; 291 _expectContinues=null; 292 } 293 294 295 296 298 private static class ClosedStream extends InputStream 299 { 300 301 public int read() 302 throws IOException 303 { 304 return -1; 305 } 306 } 307 } 308 | Popular Tags |