001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.catalina.connector;
019:
020: import java.io.IOException;
021: import java.security.AccessController;
022: import java.security.PrivilegedActionException;
023: import java.security.PrivilegedExceptionAction;
024:
025: import javax.servlet.ServletInputStream;
026:
027: import org.apache.catalina.security.SecurityUtil;
028:
029: /**
030: * This class handles reading bytes.
031: *
032: * @author Remy Maucherat
033: * @author Jean-Francois Arcand
034: */
035: public class CoyoteInputStream extends ServletInputStream {
036:
037: // ----------------------------------------------------- Instance Variables
038:
039: protected InputBuffer ib;
040:
041: // ----------------------------------------------------------- Constructors
042:
043: protected CoyoteInputStream(InputBuffer ib) {
044: this .ib = ib;
045: }
046:
047: // -------------------------------------------------------- Package Methods
048:
049: /**
050: * Clear facade.
051: */
052: void clear() {
053: ib = null;
054: }
055:
056: // --------------------------------------------------------- Public Methods
057:
058: /**
059: * Prevent cloning the facade.
060: */
061: protected Object clone() throws CloneNotSupportedException {
062: throw new CloneNotSupportedException();
063: }
064:
065: // --------------------------------------------- ServletInputStream Methods
066:
067: public int read() throws IOException {
068: if (SecurityUtil.isPackageProtectionEnabled()) {
069:
070: try {
071: Integer result = (Integer) AccessController
072: .doPrivileged(new PrivilegedExceptionAction() {
073:
074: public Object run() throws IOException {
075: Integer integer = new Integer(ib
076: .readByte());
077: return integer;
078: }
079:
080: });
081: return result.intValue();
082: } catch (PrivilegedActionException pae) {
083: Exception e = pae.getException();
084: if (e instanceof IOException) {
085: throw (IOException) e;
086: } else {
087: throw new RuntimeException(e.getMessage());
088: }
089: }
090: } else {
091: return ib.readByte();
092: }
093: }
094:
095: public int available() throws IOException {
096:
097: if (SecurityUtil.isPackageProtectionEnabled()) {
098: try {
099: Integer result = (Integer) AccessController
100: .doPrivileged(new PrivilegedExceptionAction() {
101:
102: public Object run() throws IOException {
103: Integer integer = new Integer(ib
104: .available());
105: return integer;
106: }
107:
108: });
109: return result.intValue();
110: } catch (PrivilegedActionException pae) {
111: Exception e = pae.getException();
112: if (e instanceof IOException) {
113: throw (IOException) e;
114: } else {
115: throw new RuntimeException(e.getMessage());
116: }
117: }
118: } else {
119: return ib.available();
120: }
121: }
122:
123: public int read(final byte[] b) throws IOException {
124:
125: if (SecurityUtil.isPackageProtectionEnabled()) {
126: try {
127: Integer result = (Integer) AccessController
128: .doPrivileged(new PrivilegedExceptionAction() {
129:
130: public Object run() throws IOException {
131: Integer integer = new Integer(ib.read(
132: b, 0, b.length));
133: return integer;
134: }
135:
136: });
137: return result.intValue();
138: } catch (PrivilegedActionException pae) {
139: Exception e = pae.getException();
140: if (e instanceof IOException) {
141: throw (IOException) e;
142: } else {
143: throw new RuntimeException(e.getMessage());
144: }
145: }
146: } else {
147: return ib.read(b, 0, b.length);
148: }
149: }
150:
151: public int read(final byte[] b, final int off, final int len)
152: throws IOException {
153:
154: if (SecurityUtil.isPackageProtectionEnabled()) {
155: try {
156: Integer result = (Integer) AccessController
157: .doPrivileged(new PrivilegedExceptionAction() {
158:
159: public Object run() throws IOException {
160: Integer integer = new Integer(ib.read(
161: b, off, len));
162: return integer;
163: }
164:
165: });
166: return result.intValue();
167: } catch (PrivilegedActionException pae) {
168: Exception e = pae.getException();
169: if (e instanceof IOException) {
170: throw (IOException) e;
171: } else {
172: throw new RuntimeException(e.getMessage());
173: }
174: }
175: } else {
176: return ib.read(b, off, len);
177: }
178: }
179:
180: public int readLine(byte[] b, int off, int len) throws IOException {
181: return super .readLine(b, off, len);
182: }
183:
184: /**
185: * Close the stream
186: * Since we re-cycle, we can't allow the call to super.close()
187: * which would permantely disable us.
188: */
189: public void close() throws IOException {
190:
191: if (SecurityUtil.isPackageProtectionEnabled()) {
192: try {
193: AccessController
194: .doPrivileged(new PrivilegedExceptionAction() {
195:
196: public Object run() throws IOException {
197: ib.close();
198: return null;
199: }
200:
201: });
202: } catch (PrivilegedActionException pae) {
203: Exception e = pae.getException();
204: if (e instanceof IOException) {
205: throw (IOException) e;
206: } else {
207: throw new RuntimeException(e.getMessage());
208: }
209: }
210: } else {
211: ib.close();
212: }
213: }
214:
215: }
|