01: /*
02: * Copyright 2003-2006 Rick Knowles <winstone-devel at lists sourceforge net>
03: * Distributed under the terms of either:
04: * - the common development and distribution license (CDDL), v1.0; or
05: * - the GNU Lesser General Public License, v2.1 or later
06: */
07: package winstone;
08:
09: import java.io.ByteArrayInputStream;
10: import java.io.ByteArrayOutputStream;
11: import java.io.IOException;
12: import java.io.InputStream;
13:
14: /**
15: * The request stream management class.
16: *
17: * @author <a href="mailto:rick_knowles@hotmail.com">Rick Knowles</a>
18: * @version $Id: WinstoneInputStream.java,v 1.4 2006/02/28 07:32:47 rickknowles Exp $
19: */
20: public class WinstoneInputStream extends
21: javax.servlet.ServletInputStream {
22: final int BUFFER_SIZE = 4096;
23: private InputStream inData;
24: private Integer contentLength;
25: private int readSoFar;
26: private ByteArrayOutputStream dump;
27:
28: /**
29: * Constructor
30: */
31: public WinstoneInputStream(InputStream inData) {
32: super ();
33: this .inData = inData;
34: this .dump = new ByteArrayOutputStream();
35: }
36:
37: public WinstoneInputStream(byte inData[]) {
38: this (new ByteArrayInputStream(inData));
39: }
40:
41: public InputStream getRawInputStream() {
42: return this .inData;
43: }
44:
45: public void setContentLength(int length) {
46: this .contentLength = new Integer(length);
47: this .readSoFar = 0;
48: }
49:
50: public int read() throws IOException {
51: if (this .contentLength == null) {
52: int data = this .inData.read();
53: this .dump.write(data);
54: // System.out.println("Char: " + (char) data);
55: return data;
56: } else if (this .contentLength.intValue() > this .readSoFar) {
57: this .readSoFar++;
58: int data = this .inData.read();
59: this .dump.write(data);
60: // System.out.println("Char: " + (char) data);
61: return data;
62: } else
63: return -1;
64: }
65:
66: public void finishRequest() {
67: // this.inData = null;
68: // byte content[] = this.dump.toByteArray();
69: // com.rickknowles.winstone.ajp13.Ajp13Listener.packetDump(content,
70: // content.length);
71: }
72:
73: public int available() throws IOException {
74: return this .inData.available();
75: }
76:
77: /**
78: * Wrapper for the servletInputStream's readline method
79: */
80: public byte[] readLine() throws IOException {
81: // System.out.println("ReadLine()");
82: byte buffer[] = new byte[BUFFER_SIZE];
83: int charsRead = super .readLine(buffer, 0, BUFFER_SIZE);
84: if (charsRead == -1) {
85: Logger.log(Logger.DEBUG, Launcher.RESOURCES,
86: "WinstoneInputStream.EndOfStream");
87: return new byte[0];
88: }
89: byte outBuf[] = new byte[charsRead];
90: System.arraycopy(buffer, 0, outBuf, 0, charsRead);
91: return outBuf;
92: }
93:
94: }
|