📄 abstractgenerator.java
字号:
//========================================================================//$Id: HttpGenerator.java,v 1.7 2005/11/25 21:17:12 gregwilkins Exp $//Copyright 2004-2005 Mort Bay Consulting Pty. Ltd.//------------------------------------------------------------------------//Licensed 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.mortbay.jetty;import java.io.IOException;import java.io.OutputStreamWriter;import java.io.Writer;import java.lang.reflect.Field;import java.lang.reflect.Modifier;import javax.servlet.ServletOutputStream;import javax.servlet.http.HttpServletResponse;import org.mortbay.io.Buffer;import org.mortbay.io.Buffers;import org.mortbay.io.ByteArrayBuffer;import org.mortbay.io.EndPoint;import org.mortbay.io.View;import org.mortbay.log.Log;import org.mortbay.util.ByteArrayOutputStream2;import org.mortbay.util.StringUtil;import org.mortbay.util.TypeUtil;/* ------------------------------------------------------------ *//** * Abstract Generator. Builds HTTP Messages. * * Currently this class uses a system parameter "jetty.direct.writers" to control * two optional writer to byte conversions. buffer.writers=true will probably be * faster, but will consume more memory. This option is just for testing and tuning. * * @author gregw * */public abstract class AbstractGenerator implements Generator{ // states public final static int STATE_HEADER = 0; public final static int STATE_CONTENT = 2; public final static int STATE_FLUSHING = 3; public final static int STATE_END = 4; private static byte[] NO_BYTES = {}; private static int MAX_OUTPUT_CHARS = 512; private static Buffer[] __reasons = new Buffer[505]; static { Field[] fields = HttpServletResponse.class.getDeclaredFields(); for (int i=0;i<fields.length;i++) { if ((fields[i].getModifiers()&Modifier.STATIC)!=0 && fields[i].getName().startsWith("SC_")) { try { int code = fields[i].getInt(null); if (code<__reasons.length) __reasons[code]=new ByteArrayBuffer(fields[i].getName().substring(3)); } catch(IllegalAccessException e) {} } } } protected static Buffer getReasonBuffer(int code) { Buffer reason=(code<__reasons.length)?__reasons[code]:null; return reason==null?null:reason; } public static String getReason(int code) { Buffer reason=(code<__reasons.length)?__reasons[code]:null; return reason==null?TypeUtil.toString(code):reason.toString(); } // data protected int _state = STATE_HEADER; protected int _status = 0; protected int _version = HttpVersions.HTTP_1_1_ORDINAL; protected Buffer _reason; protected Buffer _method; protected String _uri; protected long _contentWritten = 0; protected long _contentLength = HttpTokens.UNKNOWN_CONTENT; protected boolean _last = false; protected boolean _head = false; protected boolean _noContent = false; protected boolean _close = false; protected Buffers _buffers; // source of buffers protected EndPoint _endp; protected int _headerBufferSize; protected int _contentBufferSize; protected Buffer _header; // Buffer for HTTP header (and maybe small _content) protected Buffer _buffer; // Buffer for copy of passed _content protected Buffer _content; // Buffer passed to addContent private boolean _sendServerVersion; /* ------------------------------------------------------------------------------- */ /** * Constructor. * * @param buffers buffer pool * @param headerBufferSize Size of the buffer to allocate for HTTP header * @param contentBufferSize Size of the buffer to allocate for HTTP content */ public AbstractGenerator(Buffers buffers, EndPoint io, int headerBufferSize, int contentBufferSize) { this._buffers = buffers; this._endp = io; _headerBufferSize=headerBufferSize; _contentBufferSize=contentBufferSize; } /* ------------------------------------------------------------------------------- */ public void reset(boolean returnBuffers) { _state = STATE_HEADER; _status = 0; _version = HttpVersions.HTTP_1_1_ORDINAL; _reason = null; _last = false; _head = false; _noContent=false; _close = false; _contentWritten = 0; _contentLength = HttpTokens.UNKNOWN_CONTENT; synchronized(this) { if (returnBuffers) { if (_header != null) _buffers.returnBuffer(_header); _header = null; if (_buffer != null) _buffers.returnBuffer(_buffer); _buffer = null; } else { if (_header != null) _header.clear(); if (_buffer != null) { _buffers.returnBuffer(_buffer); _buffer = null; } } } _content = null; _method=null; } /* ------------------------------------------------------------------------------- */ public void resetBuffer() { if(_state>=STATE_FLUSHING) throw new IllegalStateException("Flushed"); _last = false; _close = false; _contentWritten = 0; _contentLength = HttpTokens.UNKNOWN_CONTENT; _content=null; if (_buffer!=null) _buffer.clear(); } /* ------------------------------------------------------------ */ /** * @return Returns the contentBufferSize. */ public int getContentBufferSize() { return _contentBufferSize; } /* ------------------------------------------------------------ */ /** * @param contentBufferSize The contentBufferSize to set. */ public void increaseContentBufferSize(int contentBufferSize) { if (contentBufferSize > _contentBufferSize) { _contentBufferSize = contentBufferSize; if (_buffer != null) { Buffer nb = _buffers.getBuffer(_contentBufferSize); nb.put(_buffer); _buffers.returnBuffer(_buffer); _buffer = nb; } } } /* ------------------------------------------------------------ */ public Buffer getUncheckedBuffer() { return _buffer; } /* ------------------------------------------------------------ */ public boolean getSendServerVersion () { return _sendServerVersion; } /* ------------------------------------------------------------ */ public void setSendServerVersion (boolean sendServerVersion) { _sendServerVersion = sendServerVersion; } /* ------------------------------------------------------------ */ public int getState() { return _state; } /* ------------------------------------------------------------ */ public boolean isState(int state) { return _state == state; } /* ------------------------------------------------------------ */ public boolean isComplete() { return _state == STATE_END; } /* ------------------------------------------------------------ */ public boolean isIdle() { return _state == STATE_HEADER && _method==null && _status==0; } /* ------------------------------------------------------------ */ public boolean isCommitted() { return _state != STATE_HEADER; } /* ------------------------------------------------------------ */ /** * @return Returns the head. */ public boolean isHead() { return _head; } /* ------------------------------------------------------------ */ public void setContentLength(long value) { if (value<0) _contentLength=HttpTokens.UNKNOWN_CONTENT; else _contentLength=value; } /* ------------------------------------------------------------ */ /** * @param head The head to set. */ public void setHead(boolean head) { _head = head; } /* ------------------------------------------------------------ */ /** * @return <code>false</code> if the connection should be closed after a request has been read, * <code>true</code> if it should be used for additional requests. */ public boolean isPersistent() { return !_close; } /* ------------------------------------------------------------ */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -