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