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.io.Reader;
021: import java.security.AccessController;
022: import java.security.PrivilegedActionException;
023: import java.security.PrivilegedExceptionAction;
024: import java.util.HashMap;
025:
026: import org.apache.coyote.Request;
027: import org.apache.tomcat.util.buf.B2CConverter;
028: import org.apache.tomcat.util.buf.ByteChunk;
029: import org.apache.tomcat.util.buf.CharChunk;
030:
031: /**
032: * The buffer used by Tomcat request. This is a derivative of the Tomcat 3.3
033: * OutputBuffer, adapted to handle input instead of output. This allows
034: * complete recycling of the facade objects (the ServletInputStream and the
035: * BufferedReader).
036: *
037: * @author Remy Maucherat
038: */
039: public class InputBuffer extends Reader implements
040: ByteChunk.ByteInputChannel, CharChunk.CharInputChannel,
041: CharChunk.CharOutputChannel {
042:
043: // -------------------------------------------------------------- Constants
044:
045: public static final String DEFAULT_ENCODING = org.apache.coyote.Constants.DEFAULT_CHARACTER_ENCODING;
046: public static final int DEFAULT_BUFFER_SIZE = 8 * 1024;
047: static final int debug = 0;
048:
049: // The buffer can be used for byte[] and char[] reading
050: // ( this is needed to support ServletInputStream and BufferedReader )
051: public final int INITIAL_STATE = 0;
052: public final int CHAR_STATE = 1;
053: public final int BYTE_STATE = 2;
054:
055: // ----------------------------------------------------- Instance Variables
056:
057: /**
058: * The byte buffer.
059: */
060: private ByteChunk bb;
061:
062: /**
063: * The chunk buffer.
064: */
065: private CharChunk cb;
066:
067: /**
068: * State of the output buffer.
069: */
070: private int state = 0;
071:
072: /**
073: * Number of bytes read.
074: */
075: private int bytesRead = 0;
076:
077: /**
078: * Number of chars read.
079: */
080: private int charsRead = 0;
081:
082: /**
083: * Flag which indicates if the input buffer is closed.
084: */
085: private boolean closed = false;
086:
087: /**
088: * Byte chunk used to input bytes.
089: */
090: private ByteChunk inputChunk = new ByteChunk();
091:
092: /**
093: * Encoding to use.
094: */
095: private String enc;
096:
097: /**
098: * Encoder is set.
099: */
100: private boolean gotEnc = false;
101:
102: /**
103: * List of encoders.
104: */
105: protected HashMap encoders = new HashMap();
106:
107: /**
108: * Current byte to char converter.
109: */
110: protected B2CConverter conv;
111:
112: /**
113: * Associated Coyote request.
114: */
115: private Request coyoteRequest;
116:
117: /**
118: * Buffer position.
119: */
120: private int markPos = -1;
121:
122: /**
123: * Buffer size.
124: */
125: private int size = -1;
126:
127: // ----------------------------------------------------------- Constructors
128:
129: /**
130: * Default constructor. Allocate the buffer with the default buffer size.
131: */
132: public InputBuffer() {
133:
134: this (DEFAULT_BUFFER_SIZE);
135:
136: }
137:
138: /**
139: * Alternate constructor which allows specifying the initial buffer size.
140: *
141: * @param size Buffer size to use
142: */
143: public InputBuffer(int size) {
144:
145: this .size = size;
146: bb = new ByteChunk(size);
147: bb.setLimit(size);
148: bb.setByteInputChannel(this );
149: cb = new CharChunk(size);
150: cb.setLimit(size);
151: cb.setOptimizedWrite(false);
152: cb.setCharInputChannel(this );
153: cb.setCharOutputChannel(this );
154:
155: }
156:
157: // ------------------------------------------------------------- Properties
158:
159: /**
160: * Associated Coyote request.
161: *
162: * @param coyoteRequest Associated Coyote request
163: */
164: public void setRequest(Request coyoteRequest) {
165: this .coyoteRequest = coyoteRequest;
166: }
167:
168: /**
169: * Get associated Coyote request.
170: *
171: * @return the associated Coyote request
172: */
173: public Request getRequest() {
174: return this .coyoteRequest;
175: }
176:
177: // --------------------------------------------------------- Public Methods
178:
179: /**
180: * Recycle the output buffer.
181: */
182: public void recycle() {
183:
184: if (debug > 0)
185: log("recycle()");
186:
187: state = INITIAL_STATE;
188: bytesRead = 0;
189: charsRead = 0;
190:
191: // If usage of mark made the buffer too big, reallocate it
192: if (cb.getChars().length > size) {
193: cb = new CharChunk(size);
194: cb.setLimit(size);
195: cb.setCharInputChannel(this );
196: cb.setCharOutputChannel(this );
197: } else {
198: cb.recycle();
199: }
200: markPos = -1;
201: bb.recycle();
202: closed = false;
203:
204: if (conv != null) {
205: conv.recycle();
206: }
207:
208: gotEnc = false;
209: enc = null;
210:
211: }
212:
213: /**
214: * Close the input buffer.
215: *
216: * @throws IOException An underlying IOException occurred
217: */
218: public void close() throws IOException {
219: closed = true;
220: }
221:
222: public int available() throws IOException {
223: if (state == BYTE_STATE) {
224: return bb.getLength();
225: } else if (state == CHAR_STATE) {
226: return cb.getLength();
227: } else {
228: return 0;
229: }
230: }
231:
232: // ------------------------------------------------- Bytes Handling Methods
233:
234: /**
235: * Reads new bytes in the byte chunk.
236: *
237: * @param buf Byte buffer to be written to the response
238: * @param off Offset
239: * @param cnt Length
240: *
241: * @throws IOException An underlying IOException occurred
242: */
243: public int realReadBytes(byte cbuf[], int off, int len)
244: throws IOException {
245:
246: if (debug > 2)
247: log("realRead() " + coyoteRequest);
248:
249: if (closed)
250: return -1;
251: if (coyoteRequest == null)
252: return -1;
253:
254: state = BYTE_STATE;
255:
256: int result = coyoteRequest.doRead(bb);
257:
258: return result;
259:
260: }
261:
262: public int readByte() throws IOException {
263: return bb.substract();
264: }
265:
266: public int read(byte[] b, int off, int len) throws IOException {
267: return bb.substract(b, off, len);
268: }
269:
270: // ------------------------------------------------- Chars Handling Methods
271:
272: /**
273: * Since the converter will use append, it is possible to get chars to
274: * be removed from the buffer for "writing". Since the chars have already
275: * been read before, they are ignored. If a mark was set, then the
276: * mark is lost.
277: */
278: public void realWriteChars(char c[], int off, int len)
279: throws IOException {
280: markPos = -1;
281: }
282:
283: public void setEncoding(String s) {
284: enc = s;
285: }
286:
287: public int realReadChars(char cbuf[], int off, int len)
288: throws IOException {
289:
290: if (debug > 0)
291: log("realRead() " + cb.getOffset() + " " + len);
292:
293: if (!gotEnc)
294: setConverter();
295:
296: if (debug > 0)
297: log("encoder: " + conv + " " + gotEnc);
298:
299: if (bb.getLength() <= 0) {
300: int nRead = realReadBytes(bb.getBytes(), 0,
301: bb.getBytes().length);
302: if (nRead < 0) {
303: return -1;
304: }
305: }
306:
307: if (markPos == -1) {
308: cb.setOffset(0);
309: cb.setEnd(0);
310: }
311:
312: conv.convert(bb, cb);
313: bb.setOffset(bb.getEnd());
314: state = CHAR_STATE;
315:
316: return cb.getLength();
317:
318: }
319:
320: public int read() throws IOException {
321: return cb.substract();
322: }
323:
324: public int read(char[] cbuf) throws IOException {
325: return read(cbuf, 0, cbuf.length);
326: }
327:
328: public int read(char[] cbuf, int off, int len) throws IOException {
329: return cb.substract(cbuf, off, len);
330: }
331:
332: public long skip(long n) throws IOException {
333:
334: if (n < 0) {
335: throw new IllegalArgumentException();
336: }
337:
338: long nRead = 0;
339: while (nRead < n) {
340: if (cb.getLength() >= n) {
341: cb.setOffset(cb.getStart() + (int) n);
342: nRead = n;
343: } else {
344: nRead += cb.getLength();
345: cb.setOffset(cb.getEnd());
346: int toRead = 0;
347: if (cb.getChars().length < (n - nRead)) {
348: toRead = cb.getChars().length;
349: } else {
350: toRead = (int) (n - nRead);
351: }
352: int nb = realReadChars(cb.getChars(), 0, toRead);
353: if (nb < 0)
354: break;
355: }
356: }
357:
358: return nRead;
359:
360: }
361:
362: public boolean ready() throws IOException {
363: return (cb.getLength() > 0);
364: }
365:
366: public boolean markSupported() {
367: return true;
368: }
369:
370: public void mark(int readAheadLimit) throws IOException {
371: if (cb.getLength() <= 0) {
372: cb.setOffset(0);
373: cb.setEnd(0);
374: } else {
375: if ((cb.getBuffer().length > (2 * size))
376: && (cb.getLength()) < (cb.getStart())) {
377: System.arraycopy(cb.getBuffer(), cb.getStart(), cb
378: .getBuffer(), 0, cb.getLength());
379: cb.setEnd(cb.getLength());
380: cb.setOffset(0);
381: }
382: }
383: int offset = readAheadLimit;
384: if (offset < size) {
385: offset = size;
386: }
387: cb.setLimit(cb.getStart() + offset);
388: markPos = cb.getStart();
389: }
390:
391: public void reset() throws IOException {
392: if (state == CHAR_STATE) {
393: if (markPos < 0) {
394: cb.recycle();
395: markPos = -1;
396: throw new IOException();
397: } else {
398: cb.setOffset(markPos);
399: }
400: } else {
401: bb.recycle();
402: }
403: }
404:
405: public void checkConverter() throws IOException {
406:
407: if (!gotEnc)
408: setConverter();
409:
410: }
411:
412: protected void setConverter() throws IOException {
413:
414: if (coyoteRequest != null)
415: enc = coyoteRequest.getCharacterEncoding();
416:
417: if (debug > 0)
418: log("Got encoding: " + enc);
419:
420: gotEnc = true;
421: if (enc == null)
422: enc = DEFAULT_ENCODING;
423: conv = (B2CConverter) encoders.get(enc);
424: if (conv == null) {
425: if (System.getSecurityManager() != null) {
426: try {
427: conv = (B2CConverter) AccessController
428: .doPrivileged(new PrivilegedExceptionAction() {
429:
430: public Object run() throws IOException {
431: return new B2CConverter(enc);
432: }
433:
434: });
435: } catch (PrivilegedActionException ex) {
436: Exception e = ex.getException();
437: if (e instanceof IOException)
438: throw (IOException) e;
439:
440: if (debug > 0)
441: log("setConverter: " + ex.getMessage());
442: }
443: } else {
444: conv = new B2CConverter(enc);
445: }
446: encoders.put(enc, conv);
447: }
448:
449: }
450:
451: protected void log(String s) {
452: System.out.println("InputBuffer: " + s);
453: }
454:
455: }
|