package com.aggrepoint.winlet.utils;

import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.Collection;
import java.util.Locale;

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

public class BufferedResponse implements HttpServletResponse {
	protected BufferedResponseStream stream = null;
	protected PrintWriter writer = null;

	@Override
	public String getCharacterEncoding() {
		return null;
	}

	@Override
	public String getContentType() {
		return null;
	}

	@Override
	public void setCharacterEncoding(String charset) {

	}

	@Override
	public void setContentLength(int len) {

	}

	@Override
	public void setContentLengthLong(long len) {

	}

	@Override
	public void setContentType(String type) {

	}

	@Override
	public void setBufferSize(int size) {

	}

	@Override
	public int getBufferSize() {
		return 0;
	}

	@Override
	public void resetBuffer() {

	}

	@Override
	public boolean isCommitted() {
		return false;
	}

	@Override
	public void reset() {

	}

	@Override
	public void setLocale(Locale loc) {

	}

	@Override
	public Locale getLocale() {
		return null;
	}

	@Override
	public void addCookie(Cookie cookie) {

	}

	@Override
	public boolean containsHeader(String name) {
		return false;
	}

	@Override
	public String encodeURL(String url) {
		return null;
	}

	@Override
	public String encodeRedirectURL(String url) {
		return null;
	}

	@Override
	public String encodeUrl(String url) {
		return null;
	}

	@Override
	public String encodeRedirectUrl(String url) {
		return null;
	}

	@Override
	public void sendError(int sc, String msg) throws IOException {

	}

	@Override
	public void sendError(int sc) throws IOException {

	}

	@Override
	public void sendRedirect(String location) throws IOException {

	}

	@Override
	public void setDateHeader(String name, long date) {

	}

	@Override
	public void addDateHeader(String name, long date) {

	}

	@Override
	public void setHeader(String name, String value) {

	}

	@Override
	public void addHeader(String name, String value) {

	}

	@Override
	public void setIntHeader(String name, int value) {

	}

	@Override
	public void addIntHeader(String name, int value) {

	}

	@Override
	public void setStatus(int sc) {

	}

	@Override
	public void setStatus(int sc, String sm) {

	}

	@Override
	public int getStatus() {
		return 0;
	}

	@Override
	public String getHeader(String name) {
		return null;
	}

	@Override
	public Collection<String> getHeaders(String name) {
		return null;
	}

	@Override
	public Collection<String> getHeaderNames() {
		return null;
	}

	@Override
	public void flushBuffer() throws IOException {
		if (stream != null)
			stream.flush();
	}

	@Override
	public ServletOutputStream getOutputStream() throws IOException {
		if (writer != null) {
			throw new IllegalStateException(
					"getWriter() has already been called!");
		}

		if (stream == null)
			stream = new BufferedResponseStream();
		return (stream);
	}

	@Override
	public PrintWriter getWriter() throws IOException {
		if (writer != null) {
			return (writer);
		}

		if (stream != null) {
			throw new IllegalStateException(
					"getOutputStream() has already been called!");
		}

		stream = new BufferedResponseStream();
		writer = new PrintWriter(new OutputStreamWriter(stream, "UTF-8"));
		return (writer);
	}

	public void finishResponse() {
		try {
			if (writer != null) {
				writer.close();
			} else {
				if (stream != null) {
					stream.close();
				}
			}
		} catch (IOException e) {
		}
	}

	public byte[] getBuffered() throws IOException {
		if (stream == null)
			return null;
		if (writer != null)
			writer.flush();
		return stream.getBuffered();
	}
}

@@ -31,16 +31,139 @@
 utils;%0A%0A
+import javax.servlet.ServletOutputStream;%0Aimport javax.servlet.http.Cookie;%0Aimport javax.servlet.http.HttpServletResponse;%0A
 import j
@@ -304,132 +304,8 @@
 e;%0A%0A
-import javax.servlet.ServletOutputStream;%0Aimport javax.servlet.http.Cookie;%0Aimport javax.servlet.http.HttpServletResponse;%0A%0A
 publ
@@ -449,16 +449,61 @@
  = null;
+%0A%09private String characterEncoding = %22UTF-8%22;
 %0A%0A%09@Over
@@ -548,36 +548,54 @@
 ng() %7B%0A%09%09return 
-null
+this.characterEncoding
 ;%0A%09%7D%0A%0A%09@Override
@@ -707,24 +707,59 @@
  charset) %7B%0A
+%09%09this.characterEncoding = charset;
 %0A%09%7D%0A%0A%09@Overr

设置 BufferedResponse 的默认 characterEncoding 为 UTF-8