KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > catalina > connector > CoyoteInputStream


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

17
18
19 package org.apache.catalina.connector;
20
21 import java.io.IOException JavaDoc;
22 import java.security.AccessController JavaDoc;
23 import java.security.PrivilegedActionException JavaDoc;
24 import java.security.PrivilegedExceptionAction JavaDoc;
25
26 import javax.servlet.ServletInputStream JavaDoc;
27
28 import org.apache.catalina.security.SecurityUtil;
29
30 /**
31  * This class handles reading bytes.
32  *
33  * @author Remy Maucherat
34  * @author Jean-Francois Arcand
35  */

36 public class CoyoteInputStream
37     extends ServletInputStream JavaDoc {
38
39
40     // ----------------------------------------------------- Instance Variables
41

42
43     protected InputBuffer ib;
44
45
46     // ----------------------------------------------------------- Constructors
47

48
49     protected CoyoteInputStream(InputBuffer ib) {
50         this.ib = ib;
51     }
52
53
54     // -------------------------------------------------------- Package Methods
55

56
57     /**
58      * Clear facade.
59      */

60     void clear() {
61         ib = null;
62     }
63
64
65     // --------------------------------------------------------- Public Methods
66

67
68     /**
69      * Prevent cloning the facade.
70      */

71     protected Object JavaDoc clone()
72         throws CloneNotSupportedException JavaDoc {
73         throw new CloneNotSupportedException JavaDoc();
74     }
75
76
77     // --------------------------------------------- ServletInputStream Methods
78

79
80     public int read()
81         throws IOException JavaDoc {
82         if (SecurityUtil.isPackageProtectionEnabled()){
83             
84             try{
85                 Integer JavaDoc result =
86                     (Integer JavaDoc)AccessController.doPrivileged(
87                         new PrivilegedExceptionAction JavaDoc(){
88
89                             public Object JavaDoc run() throws IOException JavaDoc{
90                                 Integer JavaDoc integer = new Integer JavaDoc(ib.readByte());
91                                 return integer;
92                             }
93
94                 });
95                 return result.intValue();
96             } catch(PrivilegedActionException JavaDoc pae){
97                 Exception JavaDoc e = pae.getException();
98                 if (e instanceof IOException JavaDoc){
99                     throw (IOException JavaDoc)e;
100                 } else {
101                     throw new RuntimeException JavaDoc(e.getMessage());
102                 }
103             }
104         } else {
105             return ib.readByte();
106         }
107     }
108
109     public int available() throws IOException JavaDoc {
110         
111         if (SecurityUtil.isPackageProtectionEnabled()){
112             try{
113                 Integer JavaDoc result =
114                     (Integer JavaDoc)AccessController.doPrivileged(
115                         new PrivilegedExceptionAction JavaDoc(){
116
117                             public Object JavaDoc run() throws IOException JavaDoc{
118                                 Integer JavaDoc integer = new Integer JavaDoc(ib.available());
119                                 return integer;
120                             }
121
122                 });
123                 return result.intValue();
124             } catch(PrivilegedActionException JavaDoc pae){
125                 Exception JavaDoc e = pae.getException();
126                 if (e instanceof IOException JavaDoc){
127                     throw (IOException JavaDoc)e;
128                 } else {
129                     throw new RuntimeException JavaDoc(e.getMessage());
130                 }
131             }
132         } else {
133            return ib.available();
134         }
135     }
136
137     public int read(final byte[] b) throws IOException JavaDoc {
138         
139         if (SecurityUtil.isPackageProtectionEnabled()){
140             try{
141                 Integer JavaDoc result =
142                     (Integer JavaDoc)AccessController.doPrivileged(
143                         new PrivilegedExceptionAction JavaDoc(){
144
145                             public Object JavaDoc run() throws IOException JavaDoc{
146                                 Integer JavaDoc integer =
147                                     new Integer JavaDoc(ib.read(b, 0, b.length));
148                                 return integer;
149                             }
150
151                 });
152                 return result.intValue();
153             } catch(PrivilegedActionException JavaDoc pae){
154                 Exception JavaDoc e = pae.getException();
155                 if (e instanceof IOException JavaDoc){
156                     throw (IOException JavaDoc)e;
157                 } else {
158                     throw new RuntimeException JavaDoc(e.getMessage());
159                 }
160             }
161         } else {
162             return ib.read(b, 0, b.length);
163          }
164     }
165
166
167     public int read(final byte[] b, final int off, final int len)
168         throws IOException JavaDoc {
169             
170         if (SecurityUtil.isPackageProtectionEnabled()){
171             try{
172                 Integer JavaDoc result =
173                     (Integer JavaDoc)AccessController.doPrivileged(
174                         new PrivilegedExceptionAction JavaDoc(){
175
176                             public Object JavaDoc run() throws IOException JavaDoc{
177                                 Integer JavaDoc integer =
178                                     new Integer JavaDoc(ib.read(b, off, len));
179                                 return integer;
180                             }
181
182                 });
183                 return result.intValue();
184             } catch(PrivilegedActionException JavaDoc pae){
185                 Exception JavaDoc e = pae.getException();
186                 if (e instanceof IOException JavaDoc){
187                     throw (IOException JavaDoc)e;
188                 } else {
189                     throw new RuntimeException JavaDoc(e.getMessage());
190                 }
191             }
192         } else {
193             return ib.read(b, off, len);
194         }
195     }
196
197
198     public int readLine(byte[] b, int off, int len) throws IOException JavaDoc {
199         return super.readLine(b, off, len);
200     }
201
202
203     /**
204      * Close the stream
205      * Since we re-cycle, we can't allow the call to super.close()
206      * which would permantely disable us.
207      */

208     public void close() throws IOException JavaDoc {
209         
210         if (SecurityUtil.isPackageProtectionEnabled()){
211             try{
212                 AccessController.doPrivileged(
213                     new PrivilegedExceptionAction JavaDoc(){
214
215                         public Object JavaDoc run() throws IOException JavaDoc{
216                             ib.close();
217                             return null;
218                         }
219
220                 });
221             } catch(PrivilegedActionException JavaDoc pae){
222                 Exception JavaDoc e = pae.getException();
223                 if (e instanceof IOException JavaDoc){
224                     throw (IOException JavaDoc)e;
225                 } else {
226                     throw new RuntimeException JavaDoc(e.getMessage());
227                 }
228             }
229         } else {
230              ib.close();
231         }
232     }
233
234 }
235
Popular Tags