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;
22 import java.security.AccessController;
23 import java.security.PrivilegedActionException;
24 import java.security.PrivilegedExceptionAction;
25
26 import javax.servlet.ServletInputStream;
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 {
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 clone()
72 throws CloneNotSupportedException {
73 throw new CloneNotSupportedException();
74 }
75
76
77 // --------------------------------------------- ServletInputStream Methods
78
79
80 public int read()
81 throws IOException {
82 if (SecurityUtil.isPackageProtectionEnabled()){
83
84 try{
85 Integer result =
86 (Integer)AccessController.doPrivileged(
87 new PrivilegedExceptionAction(){
88
89 public Object run() throws IOException{
90 Integer integer = new Integer(ib.readByte());
91 return integer;
92 }
93
94 });
95 return result.intValue();
96 } catch(PrivilegedActionException pae){
97 Exception e = pae.getException();
98 if (e instanceof IOException){
99 throw (IOException)e;
100 } else {
101 throw new RuntimeException(e.getMessage());
102 }
103 }
104 } else {
105 return ib.readByte();
106 }
107 }
108
109 public int available() throws IOException {
110
111 if (SecurityUtil.isPackageProtectionEnabled()){
112 try{
113 Integer result =
114 (Integer)AccessController.doPrivileged(
115 new PrivilegedExceptionAction(){
116
117 public Object run() throws IOException{
118 Integer integer = new Integer(ib.available());
119 return integer;
120 }
121
122 });
123 return result.intValue();
124 } catch(PrivilegedActionException pae){
125 Exception e = pae.getException();
126 if (e instanceof IOException){
127 throw (IOException)e;
128 } else {
129 throw new RuntimeException(e.getMessage());
130 }
131 }
132 } else {
133 return ib.available();
134 }
135 }
136
137 public int read(final byte[] b) throws IOException {
138
139 if (SecurityUtil.isPackageProtectionEnabled()){
140 try{
141 Integer result =
142 (Integer)AccessController.doPrivileged(
143 new PrivilegedExceptionAction(){
144
145 public Object run() throws IOException{
146 Integer integer =
147 new Integer(ib.read(b, 0, b.length));
148 return integer;
149 }
150
151 });
152 return result.intValue();
153 } catch(PrivilegedActionException pae){
154 Exception e = pae.getException();
155 if (e instanceof IOException){
156 throw (IOException)e;
157 } else {
158 throw new RuntimeException(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 {
169
170 if (SecurityUtil.isPackageProtectionEnabled()){
171 try{
172 Integer result =
173 (Integer)AccessController.doPrivileged(
174 new PrivilegedExceptionAction(){
175
176 public Object run() throws IOException{
177 Integer integer =
178 new Integer(ib.read(b, off, len));
179 return integer;
180 }
181
182 });
183 return result.intValue();
184 } catch(PrivilegedActionException pae){
185 Exception e = pae.getException();
186 if (e instanceof IOException){
187 throw (IOException)e;
188 } else {
189 throw new RuntimeException(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 {
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 {
209
210 if (SecurityUtil.isPackageProtectionEnabled()){
211 try{
212 AccessController.doPrivileged(
213 new PrivilegedExceptionAction(){
214
215 public Object run() throws IOException{
216 ib.close();
217 return null;
218 }
219
220 });
221 } catch(PrivilegedActionException pae){
222 Exception e = pae.getException();
223 if (e instanceof IOException){
224 throw (IOException)e;
225 } else {
226 throw new RuntimeException(e.getMessage());
227 }
228 }
229 } else {
230 ib.close();
231 }
232 }
233
234 }