KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > coyote > tomcat4 > CoyoteInputStream


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.coyote.tomcat4;
18
19 import java.io.IOException JavaDoc;
20
21 import javax.servlet.ServletInputStream JavaDoc;
22
23 import org.apache.coyote.Request;
24 import org.apache.tomcat.util.buf.ByteChunk;
25
26
27 /**
28  * This class handles the readin input bytes, as well as the input buffering.
29  *
30  * @author Remy Maucherat
31  */

32 public class CoyoteInputStream extends ServletInputStream JavaDoc {
33
34
35     // ----------------------------------------------------- Instance Variables
36

37
38     private boolean closed = false;
39
40     private Request coyoteRequest;
41
42     private ByteChunk readChunk = new ByteChunk();
43
44     /**
45      * Current read position in the byte buffer.
46      */

47     private int pos = -1;
48     private int end = -1;
49     private byte[] readBuffer = null;
50
51
52     // --------------------------------------------------------- Public Methods
53

54
55     void setRequest(Request coyoteRequest) {
56         this.coyoteRequest = coyoteRequest;
57     }
58
59
60     /**
61      * Recycle the input stream.
62      */

63     void recycle() {
64         closed = false;
65         pos = -1;
66         end = -1;
67         readBuffer = null;
68     }
69
70
71     // --------------------------------------------- ServletInputStream Methods
72

73
74     public int read()
75         throws IOException JavaDoc {
76
77         if (closed)
78             throw new IOException JavaDoc("Stream closed");
79
80         // Read additional bytes if none are available
81
while (pos >= end) {
82             if (readBytes() < 0)
83                 return -1;
84         }
85
86         return (readBuffer[pos++] & 0xFF);
87
88     }
89
90     public int available() throws IOException JavaDoc {
91         if( pos < end ) return end-pos;
92         return 0;
93     }
94
95     public int read(byte[] b) throws IOException JavaDoc {
96
97         return read(b, 0, b.length);
98
99     }
100
101
102     public int read(byte[] b, int off, int len)
103         throws IOException JavaDoc {
104
105         if (closed)
106             throw new IOException JavaDoc("Stream closed");
107
108         // Read additional bytes if none are available
109
while (pos >= end) {
110             if (readBytes() < 0)
111                 return -1;
112         }
113
114         int n = -1;
115         if ((end - pos) > len) {
116             n = len;
117         } else {
118             n = end - pos;
119         }
120
121         System.arraycopy(readBuffer, pos, b, off, n);
122
123         pos += n;
124         return n;
125
126     }
127
128
129     public int readLine(byte[] b, int off, int len) throws IOException JavaDoc {
130         return super.readLine(b, off, len);
131     }
132
133
134     /**
135      * Close the stream
136      * Since we re-cycle, we can't allow the call to super.close()
137      * which would permantely disable us.
138      */

139     public void close() {
140         closed = true;
141     }
142
143
144     // ------------------------------------------------------ Protected Methods
145

146
147     /**
148      * Read bytes to the read chunk buffer.
149      */

150     protected int readBytes()
151         throws IOException JavaDoc {
152
153         int result = coyoteRequest.doRead(readChunk);
154         if (result > 0) {
155             readBuffer = readChunk.getBytes();
156             end = readChunk.getEnd();
157             pos = readChunk.getStart();
158         }
159         return result;
160
161     }
162
163
164 }
165
Popular Tags