/* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License.
*/ package org.apache.catalina.connector;
/** * The buffer used by Tomcat request. This is a derivative of the Tomcat 3.3 OutputBuffer, adapted to handle input * instead of output. This allows complete recycling of the facade objects (the ServletInputStream and the * BufferedReader). * * @author Remy Maucherat
*/ publicclass InputBuffer extends Reader implements ByteChunk.ByteInputChannel, ApplicationBufferHandler {
/** * The string manager for this package.
*/ protectedstaticfinal StringManager sm = StringManager.getManager(InputBuffer.class);
// The buffer can be used for byte[] and char[] reading // ( this is needed to support ServletInputStream and BufferedReader ) publicfinalint INITIAL_STATE = 0; publicfinalint CHAR_STATE = 1; publicfinalint BYTE_STATE = 2;
// --------------------------------------------------------- Public Methods
/** * Recycle the output buffer.
*/ publicvoid recycle() {
state = INITIAL_STATE;
// If usage of mark made the buffer too big, reallocate it if (cb.capacity() > size) {
cb = CharBuffer.allocate(size);
clear(cb);
} else {
clear(cb);
}
readLimit = size;
markPos = -1;
clear(bb);
closed = false;
publicboolean isFinished() { int available = 0; if (state == BYTE_STATE) {
available = bb.remaining();
} elseif (state == CHAR_STATE) {
available = cb.remaining();
} if (available > 0) { returnfalse;
} else { return coyoteRequest.isFinished();
}
}
publicboolean isReady() { if (coyoteRequest.getReadListener() == null) { if (log.isDebugEnabled()) {
log.debug(sm.getString("inputBuffer.requiresNonBlocking"));
} returnfalse;
} if (isFinished()) { // If this is a non-container thread, need to trigger a read // which will eventually lead to a call to onAllDataRead() via a // container thread. if (!coyoteRequest.isRequestThread()) {
coyoteRequest.action(ActionCode.DISPATCH_READ, null);
coyoteRequest.action(ActionCode.DISPATCH_EXECUTE, null);
} returnfalse;
} // Checking for available data at the network level and registering for // read can be done sequentially for HTTP/1.x and AJP as there is only // ever a single thread processing the socket at any one time. However, // for HTTP/2 there is one thread processing the connection and separate // threads for each stream. For HTTP/2 the two operations have to be // performed atomically else it is possible for the connection thread to // read more data in to the buffer after the stream thread checks for // available network data but before it registers for read. if (availableInThisBuffer() > 0) { returntrue;
}
/** * Reads new bytes in the byte chunk. * * @throws IOException An underlying IOException occurred
*/
@Override publicint realReadBytes() throws IOException { if (closed) { return -1;
}
if (state == INITIAL_STATE) {
state = BYTE_STATE;
}
try { return coyoteRequest.doRead(this);
} catch (BadRequestException bre) { // Make the exception visible to the application
handleReadException(bre); throw bre;
} catch (IOException ioe) {
handleReadException(ioe); // Any other IOException on a read is almost always due to the remote client aborting the request. // Make the exception visible to the application thrownew ClientAbortException(ioe);
}
}
privatevoid handleReadException(Exception e) throws IOException { // Set flag used by asynchronous processing to detect errors on non-container threads
coyoteRequest.setErrorException(e); // In synchronous processing, this exception may be swallowed by the application so set error flags here.
Request request = (Request) coyoteRequest.getNote(CoyoteAdapter.ADAPTER_NOTES);
Response response = request.getResponse();
request.setAttribute(RequestDispatcher.ERROR_EXCEPTION, e);
response.sendError(400);
}
publicint read(byte[] b, int off, int len) throws IOException {
throwIfClosed();
if (checkByteBufferEof()) { return -1;
} int n = Math.min(len, bb.remaining());
bb.get(b, off, n); return n;
}
/** * Transfers bytes from the buffer to the specified ByteBuffer. After the operation the position of the ByteBuffer * will be returned to the one before the operation, the limit will be the position incremented by the number of the * transferred bytes. * * @param to the ByteBuffer into which bytes are to be written. * * @return an integer specifying the actual number of bytes read, or -1 if the end of the stream is reached * * @throws IOException if an input or output exception has occurred
*/ publicint read(ByteBuffer to) throws IOException {
throwIfClosed();
if (checkByteBufferEof()) { return -1;
} int n = Math.min(to.remaining(), bb.remaining()); int orgLimit = bb.limit();
bb.limit(bb.position() + n);
to.put(bb);
bb.limit(orgLimit);
to.limit(to.position()).position(to.position() - n); return n;
}
if (bb.remaining() <= 0) { int nRead = realReadBytes(); if (nRead < 0) {
eof = true;
}
}
if (markPos == -1) {
clear(cb);
} else { // Make sure there's enough space in the worst case
makeSpace(bb.remaining()); if ((cb.capacity() - cb.limit()) == 0 && bb.remaining() != 0) { // We went over the limit
clear(cb);
markPos = -1;
}
}
state = CHAR_STATE;
conv.convert(bb, cb, this, eof);
¤ Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.0.20Bemerkung:
(vorverarbeitet)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung ist noch experimentell.