📄 abstractgenerator.java
字号:
public void setPersistent(boolean persistent) { _close=!persistent; } /* ------------------------------------------------------------ */ /** * @param version The version of the client the response is being sent to (NB. Not the version * in the response, which is the version of the server). */ public void setVersion(int version) { if (_state != STATE_HEADER) throw new IllegalStateException("STATE!=START"); _version = version; if (_version==HttpVersions.HTTP_0_9_ORDINAL && _method!=null) _noContent=true; } /* ------------------------------------------------------------ */ public int getVersion() { return _version; } /* ------------------------------------------------------------ */ /** */ public void setRequest(String method, String uri) { if (method==null || HttpMethods.GET.equals(method) ) _method=HttpMethods.GET_BUFFER; else _method=HttpMethods.CACHE.lookup(method); _uri=uri; if (_version==HttpVersions.HTTP_0_9_ORDINAL) _noContent=true; } /* ------------------------------------------------------------ */ /** * @param status The status code to send. * @param reason the status message to send. */ public void setResponse(int status, String reason) { if (_state != STATE_HEADER) throw new IllegalStateException("STATE!=START"); _status = status; if (reason!=null) { int len=reason.length(); if (len>_headerBufferSize/2) len=_headerBufferSize/2; _reason=new ByteArrayBuffer(len); for (int i=0;i<len;i++) { char ch = reason.charAt(i); if (ch!='\r'&&ch!='\n') _reason.put((byte)ch); else _reason.put((byte)' '); } } } /* ------------------------------------------------------------ */ /** Prepare buffer for unchecked writes. * Prepare the generator buffer to receive unchecked writes * @return the available space in the buffer. * @throws IOException */ protected abstract int prepareUncheckedAddContent() throws IOException; /* ------------------------------------------------------------ */ void uncheckedAddContent(int b) { _buffer.put((byte)b); } /* ------------------------------------------------------------ */ void completeUncheckedAddContent() { if (_noContent) { if(_buffer!=null) _buffer.clear(); return; } else { _contentWritten+=_buffer.length(); if (_head) _buffer.clear(); } } /* ------------------------------------------------------------ */ public boolean isBufferFull() { if (_buffer != null && _buffer.space()==0) { if (_buffer.length()==0 && !_buffer.isImmutable()) _buffer.compact(); return _buffer.space()==0; } return _content!=null && _content.length()>0; } /* ------------------------------------------------------------ */ public boolean isContentWritten() { return _contentLength>=0 && _contentWritten>=_contentLength; } /* ------------------------------------------------------------ */ public abstract void completeHeader(HttpFields fields, boolean allContentAdded) throws IOException; /* ------------------------------------------------------------ */ /** * Complete the message. * * @throws IOException */ public void complete() throws IOException { if (_state == STATE_HEADER) { throw new IllegalStateException("State==HEADER"); } if (_contentLength >= 0 && _contentLength != _contentWritten && !_head) { if (Log.isDebugEnabled()) Log.debug("ContentLength written=="+_contentWritten+" != contentLength=="+_contentLength); _close = true; } } /* ------------------------------------------------------------ */ public abstract long flush() throws IOException; /* ------------------------------------------------------------ */ /** * Utility method to send an error response. If the builder is not committed, this call is * equivalent to a setResponse, addcontent and complete call. * * @param code * @param reason * @param content * @param close * @throws IOException */ public void sendError(int code, String reason, String content, boolean close) throws IOException { if (!isCommitted()) { setResponse(code, reason); _close = close; completeHeader(null, false); if (content != null) addContent(new View(new ByteArrayBuffer(content)), Generator.LAST); complete(); } } /* ------------------------------------------------------------ */ /** * @return Returns the contentWritten. */ public long getContentWritten() { return _contentWritten; } /* ------------------------------------------------------------ */ /* ------------------------------------------------------------ */ /* ------------------------------------------------------------ */ /* ------------------------------------------------------------ */ /** Output. * * <p> * Implements {@link javax.servlet.ServletOutputStream} from the {@link javax.servlet} package. * </p> * A {@link ServletOutputStream} implementation that writes content * to a {@link AbstractGenerator}. The class is designed to be reused * and can be reopened after a close. */ public static class Output extends ServletOutputStream { protected AbstractGenerator _generator; protected long _maxIdleTime; protected ByteArrayBuffer _buf = new ByteArrayBuffer(NO_BYTES); protected boolean _closed; // These are held here for reuse by Writer String _characterEncoding; Writer _converter; char[] _chars; ByteArrayOutputStream2 _bytes; /* ------------------------------------------------------------ */ public Output(AbstractGenerator generator, long maxIdleTime) { _generator=generator; _maxIdleTime=maxIdleTime; } /* ------------------------------------------------------------ */ /* * @see java.io.OutputStream#close() */ public void close() throws IOException { _closed=true; } /* ------------------------------------------------------------ */ void blockForOutput() throws IOException { if (_generator._endp.isBlocking()) { try { flush(); } catch(IOException e) { _generator._endp.close(); throw e; } } else { if (!_generator._endp.blockWritable(_maxIdleTime)) { _generator._endp.close(); throw new EofException("timeout"); } _generator.flush(); } } /* ------------------------------------------------------------ */ void reopen() { _closed=false; } /* ------------------------------------------------------------ */ public void flush() throws IOException { // block until everything is flushed Buffer content = _generator._content; Buffer buffer = _generator._buffer; if (content!=null && content.length()>0 || buffer!=null && buffer.length()>0 || _generator.isBufferFull()) { _generator.flush(); while ((content!=null && content.length()>0 ||buffer!=null && buffer.length()>0) && _generator._endp.isOpen()) blockForOutput(); } } /* ------------------------------------------------------------ */ public void write(byte[] b, int off, int len) throws IOException { _buf.wrap(b, off, len); write(_buf); } /* ------------------------------------------------------------ */ /* * @see java.io.OutputStream#write(byte[]) */ public void write(byte[] b) throws IOException { _buf.wrap(b); write(_buf); } /* ------------------------------------------------------------ */ /* * @see java.io.OutputStream#write(int) */ public void write(int b) throws IOException { if (_closed) throw new IOException("Closed"); if (!_generator._endp.isOpen()) throw new EofException(); // Block until we can add _content. while (_generator.isBufferFull()) { blockForOutput(); if (_closed) throw new IOException("Closed"); if (!_generator._endp.isOpen()) throw new EofException(); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -