KickJava   Java API By Example, From Geeks To Geeks.

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


1
2
3 /*
4  * The contents of this file are subject to the terms
5  * of the Common Development and Distribution License
6  * (the "License"). You may not use this file except
7  * in compliance with the License.
8  *
9  * You can obtain a copy of the license at
10  * glassfish/bootstrap/legal/CDDLv1.0.txt or
11  * https://glassfish.dev.java.net/public/CDDLv1.0.html.
12  * See the License for the specific language governing
13  * permissions and limitations under the License.
14  *
15  * When distributing Covered Code, include this CDDL
16  * HEADER in each file and include the License file at
17  * glassfish/bootstrap/legal/CDDLv1.0.txt. If applicable,
18  * add the following below this CDDL HEADER, with the
19  * fields enclosed by brackets "[]" replaced with your
20  * own identifying information: Portions Copyright [yyyy]
21  * [name of copyright owner]
22  *
23  * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
24  *
25  * Portions Copyright Apache Software Foundation.
26  */

27
28
29 package org.apache.coyote.tomcat5;
30
31 import java.io.IOException JavaDoc;
32 import java.security.AccessController JavaDoc;
33 import java.security.PrivilegedAction JavaDoc;
34 import java.security.PrivilegedExceptionAction JavaDoc;
35 import java.security.PrivilegedActionException JavaDoc;
36 import javax.servlet.ServletInputStream JavaDoc;
37
38 import org.apache.catalina.security.SecurityUtil;
39
40 /**
41  * This class handles reading bytes.
42  *
43  * @author Remy Maucherat
44  * @author Jean-Francois Arcand
45  */

46 public class CoyoteInputStream
47     extends ServletInputStream JavaDoc {
48
49
50     // ----------------------------------------------------- Instance Variables
51

52
53     protected InputBuffer ib;
54
55
56     // ----------------------------------------------------------- Constructors
57

58
59     public CoyoteInputStream(InputBuffer ib) {
60         this.ib = ib;
61     }
62     
63     
64     // --------------------------------------------------------- Public Methods
65

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

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

78
79     /**
80      * Clear facade.
81      */

82     void clear() {
83         ib = null;
84     }
85
86
87     // --------------------------------------------- ServletInputStream Methods
88

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

217     public void close() throws IOException JavaDoc {
218         if (SecurityUtil.isPackageProtectionEnabled()){
219             try{
220                 AccessController.doPrivileged(
221                     new PrivilegedExceptionAction JavaDoc(){
222
223                         public Object JavaDoc run() throws IOException JavaDoc{
224                             ib.close();
225                             return null;
226                         }
227
228                 });
229             } catch(PrivilegedActionException JavaDoc pae){
230                 Exception JavaDoc e = pae.getException();
231                 if (e instanceof IOException JavaDoc){
232                     throw (IOException JavaDoc)e;
233                 } else {
234                     throw new RuntimeException JavaDoc(e.getMessage());
235                 }
236             }
237         } else {
238              ib.close();
239         }
240     }
241
242 }
243
Popular Tags