⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 bufferedresponse.java

📁 JSP/Servelt的部署。已完成。可以查看源码来检查其功能
💻 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 + -