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

📄 chararrayresponsewrapper.java

📁 Jodd是一个开源的公用Java基础类库
💻 JAVA
字号:
package jodd.servlet.filters;

import java.io.CharArrayWriter;
import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletResponseWrapper;

import jodd.util.CharUtil;

/**
 * A response wrapper that takes everything the client would normally output
 * and saves it in one big character array.
 */
public class CharArrayResponseWrapper extends HttpServletResponseWrapper {
	
	private CharArrayWriter writer;

	/**
	 * Initializes wrapper.
	 *
	 * First, this constructor calls the parent constructor. That call is crucial
	 * so that the response is stored and thus setHeader, setStatus, addCookie,
	 * and so forth work normally.
	 *
	 * Second, this constructor creates a CharArrayWriter that will be used to
	 * accumulate the response.
	 *
	 * @param response
	 */
	public CharArrayResponseWrapper(HttpServletResponse response) {
		super(response);
		writer = new CharArrayWriter();
	}

	/**
	 * When servlets or JSP pages ask for the Writer, don't give them the real
	 * one. Instead, give them a version that writes into the character array.
	 * The filter needs to send the contents of the array to the client (perhaps
	 * after modifying it).
	 */
	public PrintWriter getWriter() {
		return new PrintWriter(writer);
	}

	/**
	 * Get a String representation of the entire buffer.
	 *
	 * Be sure <B>not</B> to call this method multiple times on the same wrapper.
	 * The API for CharArrayWriter does not guarantee that it "remembers" the
	 * previous value, so the call is likely to make a new String every time.
	 */
	public String toString() {
		return writer.toString();
	}

	/**
	 * Get the underlying character array.
	 *
	 * @return content as char array
	 */
	public char[] toCharArray() {
		return writer.toCharArray();
	}

	/**
	 * Get the underlying as byte array.
	 *
	 * @return content as byte array
	 */
	public byte[] toByteArray() {
		return CharUtil.toByteArray(writer.toCharArray());
	}


	/**
	 * This empty method <b>must</b> exist.
	 *
	 * @param len
	 */
	public void setContentLength(int len) {
	}

	/**
	 * Returns the size (number of characters) of written data.
	 *
	 * @return size of written data
	 */
	public int getSize() {
		return writer.size();
	}


	private String contentType = "";

	/**
	 * Sets the content type.
	 *
	 * @param type
	 */
	public void setContentType(String type) {
		super.setContentType(type);
		contentType = type;
	}

	/**
	 * Returns content type.
	 *
	 * @return content type
	 */
	public String getContentType() {
		return contentType;
	}

	public void close() {
		writer.close();
	}

	/**
	 * Returns output stream.
	 *
	 * @return output stream
	 * @exception java.io.IOException
	 */
	public ServletOutputStream getOutputStream() throws java.io.IOException {

		return new ServletOutputStream() {
			public void write(int b) throws IOException {
				writer.write(b);
			}

			public void write(byte b[]) throws IOException {
				writer.write(CharUtil.toCharArray(b), 0, b.length);
			}

			public void write(byte b[], int off, int len) throws IOException {
				writer.write(CharUtil.toCharArray(b), off, len);
			}
		};
	}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -