📄 internalaproutputbuffer.java
字号:
/*
* 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.coyote.http11;
import java.io.IOException;
import java.nio.ByteBuffer;
import org.apache.tomcat.jni.Socket;
import org.apache.tomcat.util.buf.ByteChunk;
import org.apache.tomcat.util.buf.CharChunk;
import org.apache.tomcat.util.buf.MessageBytes;
import org.apache.tomcat.util.http.HttpMessages;
import org.apache.tomcat.util.http.MimeHeaders;
import org.apache.tomcat.util.res.StringManager;
import org.apache.coyote.ActionCode;
import org.apache.coyote.OutputBuffer;
import org.apache.coyote.Response;
/**
* Output buffer.
*
* @author <a href="mailto:remm@apache.org">Remy Maucherat</a>
*/
public class InternalAprOutputBuffer
implements OutputBuffer {
// -------------------------------------------------------------- Constants
// ----------------------------------------------------------- Constructors
/**
* Default constructor.
*/
public InternalAprOutputBuffer(Response response) {
this(response, Constants.DEFAULT_HTTP_HEADER_BUFFER_SIZE);
}
/**
* Alternate constructor.
*/
public InternalAprOutputBuffer(Response response, int headerBufferSize) {
this.response = response;
headers = response.getMimeHeaders();
buf = new byte[headerBufferSize];
if (headerBufferSize < (8 * 1024)) {
bbuf = ByteBuffer.allocateDirect(6 * 1500);
} else {
bbuf = ByteBuffer.allocateDirect((headerBufferSize / 1500 + 1) * 1500);
}
outputStreamOutputBuffer = new SocketOutputBuffer();
filterLibrary = new OutputFilter[0];
activeFilters = new OutputFilter[0];
lastActiveFilter = -1;
committed = false;
finished = false;
// Cause loading of HttpMessages
HttpMessages.getMessage(200);
}
// -------------------------------------------------------------- Variables
/**
* The string manager for this package.
*/
protected static StringManager sm =
StringManager.getManager(Constants.Package);
// ----------------------------------------------------- Instance Variables
/**
* Associated Coyote response.
*/
protected Response response;
/**
* Headers of the associated request.
*/
protected MimeHeaders headers;
/**
* Committed flag.
*/
protected boolean committed;
/**
* Finished flag.
*/
protected boolean finished;
/**
* Pointer to the current write buffer.
*/
protected byte[] buf;
/**
* Position in the buffer.
*/
protected int pos;
/**
* Underlying socket.
*/
protected long socket;
/**
* Underlying output buffer.
*/
protected OutputBuffer outputStreamOutputBuffer;
/**
* Filter library.
* Note: Filter[0] is always the "chunked" filter.
*/
protected OutputFilter[] filterLibrary;
/**
* Active filter (which is actually the top of the pipeline).
*/
protected OutputFilter[] activeFilters;
/**
* Index of the last active filter.
*/
protected int lastActiveFilter;
/**
* Direct byte buffer used for writing.
*/
protected ByteBuffer bbuf = null;
// ------------------------------------------------------------- Properties
/**
* Set the underlying socket.
*/
public void setSocket(long socket) {
this.socket = socket;
Socket.setsbb(this.socket, bbuf);
}
/**
* Get the underlying socket input stream.
*/
public long getSocket() {
return socket;
}
/**
* Set the socket buffer size.
*/
public void setSocketBuffer(int socketBufferSize) {
// FIXME: Remove
}
/**
* Add an output filter to the filter library.
*/
public void addFilter(OutputFilter filter) {
OutputFilter[] newFilterLibrary =
new OutputFilter[filterLibrary.length + 1];
for (int i = 0; i < filterLibrary.length; i++) {
newFilterLibrary[i] = filterLibrary[i];
}
newFilterLibrary[filterLibrary.length] = filter;
filterLibrary = newFilterLibrary;
activeFilters = new OutputFilter[filterLibrary.length];
}
/**
* Get filters.
*/
public OutputFilter[] getFilters() {
return filterLibrary;
}
/**
* Clear filters.
*/
public void clearFilters() {
filterLibrary = new OutputFilter[0];
lastActiveFilter = -1;
}
/**
* Add an output filter to the filter library.
*/
public void addActiveFilter(OutputFilter filter) {
if (lastActiveFilter == -1) {
filter.setBuffer(outputStreamOutputBuffer);
} else {
for (int i = 0; i <= lastActiveFilter; i++) {
if (activeFilters[i] == filter)
return;
}
filter.setBuffer(activeFilters[lastActiveFilter]);
}
activeFilters[++lastActiveFilter] = filter;
filter.setResponse(response);
}
// --------------------------------------------------------- Public Methods
/**
* Flush the response.
*
* @throws IOException an undelying I/O error occured
*/
public void flush()
throws IOException {
if (!committed) {
// Send the connector a request for commit. The connector should
// then validate the headers, send them (using sendHeader) and
// set the filters accordingly.
response.action(ActionCode.ACTION_COMMIT, null);
}
// Flush the current buffer
flushBuffer();
}
/**
* Reset current response.
*
* @throws IllegalStateException if the response has already been committed
*/
public void reset() {
if (committed)
throw new IllegalStateException(/*FIXME:Put an error message*/);
// Recycle Request object
response.recycle();
}
/**
* Recycle the output buffer. This should be called when closing the
* connection.
*/
public void recycle() {
// Recycle Request object
response.recycle();
bbuf.clear();
socket = 0;
pos = 0;
lastActiveFilter = -1;
committed = false;
finished = false;
}
/**
* End processing of current HTTP request.
* Note: All bytes of the current request should have been already
* consumed. This method only resets all the pointers so that we are ready
* to parse the next HTTP request.
*/
public void nextRequest() {
// Recycle Request object
response.recycle();
// Recycle filters
for (int i = 0; i <= lastActiveFilter; i++) {
activeFilters[i].recycle();
}
// Reset pointers
pos = 0;
lastActiveFilter = -1;
committed = false;
finished = false;
}
/**
* End request.
*
* @throws IOException an undelying I/O error occured
*/
public void endRequest()
throws IOException {
if (!committed) {
// Send the connector a request for commit. The connector should
// then validate the headers, send them (using sendHeader) and
// set the filters accordingly.
response.action(ActionCode.ACTION_COMMIT, null);
}
if (finished)
return;
if (lastActiveFilter != -1)
activeFilters[lastActiveFilter].end();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -