KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > winstone > WinstoneInputStream


1 /*
2  * Copyright 2003-2006 Rick Knowles <winstone-devel at lists sourceforge net>
3  * Distributed under the terms of either:
4  * - the common development and distribution license (CDDL), v1.0; or
5  * - the GNU Lesser General Public License, v2.1 or later
6  */

7 package winstone;
8
9 import java.io.ByteArrayInputStream JavaDoc;
10 import java.io.ByteArrayOutputStream JavaDoc;
11 import java.io.IOException JavaDoc;
12 import java.io.InputStream JavaDoc;
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 javax.servlet.ServletInputStream JavaDoc {
21     final int BUFFER_SIZE = 4096;
22     private InputStream JavaDoc inData;
23     private Integer JavaDoc contentLength;
24     private int readSoFar;
25     private ByteArrayOutputStream JavaDoc dump;
26     
27     /**
28      * Constructor
29      */

30     public WinstoneInputStream(InputStream JavaDoc inData) {
31         super();
32         this.inData = inData;
33         this.dump = new ByteArrayOutputStream JavaDoc();
34     }
35
36     public WinstoneInputStream(byte inData[]) {
37         this(new ByteArrayInputStream JavaDoc(inData));
38     }
39
40     public InputStream JavaDoc getRawInputStream() {
41         return this.inData;
42     }
43
44     public void setContentLength(int length) {
45         this.contentLength = new Integer JavaDoc(length);
46         this.readSoFar = 0;
47     }
48
49     public int read() throws IOException JavaDoc {
50         if (this.contentLength == null) {
51             int data = this.inData.read();
52             this.dump.write(data);
53 // System.out.println("Char: " + (char) data);
54
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 // System.out.println("Char: " + (char) data);
60
return data;
61         } else
62             return -1;
63     }
64
65     public void finishRequest() {
66         // this.inData = null;
67
// byte content[] = this.dump.toByteArray();
68
// com.rickknowles.winstone.ajp13.Ajp13Listener.packetDump(content,
69
// content.length);
70
}
71
72     public int available() throws IOException JavaDoc {
73         return this.inData.available();
74     }
75
76     /**
77      * Wrapper for the servletInputStream's readline method
78      */

79     public byte[] readLine() throws IOException JavaDoc {
80         // System.out.println("ReadLine()");
81
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