1 7 package winstone; 8 9 import java.io.ByteArrayInputStream ; 10 import java.io.ByteArrayOutputStream ; 11 import java.io.IOException ; 12 import java.io.InputStream ; 13 14 20 public class WinstoneInputStream extends javax.servlet.ServletInputStream { 21 final int BUFFER_SIZE = 4096; 22 private InputStream inData; 23 private Integer contentLength; 24 private int readSoFar; 25 private ByteArrayOutputStream dump; 26 27 30 public WinstoneInputStream(InputStream inData) { 31 super(); 32 this.inData = inData; 33 this.dump = new ByteArrayOutputStream (); 34 } 35 36 public WinstoneInputStream(byte inData[]) { 37 this(new ByteArrayInputStream (inData)); 38 } 39 40 public InputStream getRawInputStream() { 41 return this.inData; 42 } 43 44 public void setContentLength(int length) { 45 this.contentLength = new Integer (length); 46 this.readSoFar = 0; 47 } 48 49 public int read() throws IOException { 50 if (this.contentLength == null) { 51 int data = this.inData.read(); 52 this.dump.write(data); 53 return data; 55 } else if (this.contentLength.intValue() > this.readSoFar) { 56 this.readSoFar++; 57 int data = this.inData.read(); 58 this.dump.write(data); 59 return data; 61 } else 62 return -1; 63 } 64 65 public void finishRequest() { 66 } 71 72 public int available() throws IOException { 73 return this.inData.available(); 74 } 75 76 79 public byte[] readLine() throws IOException { 80 byte buffer[] = new byte[BUFFER_SIZE]; 82 int charsRead = super.readLine(buffer, 0, BUFFER_SIZE); 83 if (charsRead == -1) { 84 Logger.log(Logger.DEBUG, Launcher.RESOURCES, 85 "WinstoneInputStream.EndOfStream"); 86 return new byte[0]; 87 } 88 byte outBuf[] = new byte[charsRead]; 89 System.arraycopy(buffer, 0, outBuf, 0, charsRead); 90 return outBuf; 91 } 92 93 } 94
| Popular Tags
|