📄 bufferedresponse.java
字号:
package servlet;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class BufferedResponse extends HttpServletResponseWrapper {
public static final short OT_NONE = 0, OT_WRITER = 1, OT_OUTPUT_STREAM = 2;
private short outputType = OT_NONE;
private PrintWriter writer = null;
private BufServletOutputStream out = null;
private String contentType;
public BufferedResponse(HttpServletResponse response){ super(response); }
public ServletOutputStream getOutputStream() throws IOException {
//返回OutputStream之前检查outputType实例变量,增强了限制
if (outputType == OT_WRITER) {
throw new IllegalStateException();
} else if (outputType == OT_OUTPUT_STREAM) {
return out;
} else {
out = new BufServletOutputStream();
outputType = OT_OUTPUT_STREAM;
return (ServletOutputStream)out;
}
}
public PrintWriter getWriter() throws IOException {
//返回PrintWriter之前检查outputType实例变量,增强了限制
if (outputType == OT_OUTPUT_STREAM) {
throw new IllegalStateException();
} else if (outputType == OT_WRITER) {
return writer;
} else {
writer = new PrintWriter( new OutputStreamWriter(getOutputStream(),getCharacterEncoding()));
outputType = OT_WRITER;
return writer;
}
}
public short getOutputType() {
return outputType;
}
public String getContentType() {
return contentType;
}
public void setContentType(String contentType) {
this.contentType = contentType;
super.setContentType(contentType);
}
public void flushBuffer()throws IOException {
if (outputType == OT_WRITER) {
writer.flush();
} else if (outputType == OT_OUTPUT_STREAM) {
out.flush();
}
}
public void reset() {
resetBuffer();
outputType = OT_NONE;
super.reset();
}
public void resetBuffer() {
if(null != out) {
out.reset();
}
}
public byte[] toByteArray() throws IOException {
flushBuffer();
if(null != out) {
return(out.toByteArray());
} else {
return null;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -