靈感來自于前一陣一直研究的java動態(tài)代理機制(現(xiàn)在應用在spring的aop實現(xiàn)中),此處不用動態(tài)代理,就使用靜態(tài)代理,proxy模式就足夠了。
//獲得printWriterProxy
public PrintWriter getWriter() throws IOException {
PrintWriter pw = obj.getWriter();
PrintWriterProxy pwp = new PrintWriterProxy(pw);
return (PrintWriter) pwp;
}
}
PrintWriterProxy:
public class PrintWriterProxy
extends PrintWriter {
private PrintWriter pw = null;
public PrintWriterProxy(PrintWriter pw) {
super(pw);
this.pw = pw;
}
//截獲寫內(nèi)容寫入buffer
public void write(int c) {
char a = (char) c;
String s = new String(new char[] {a});
HtmlBuffer.addStr(s);
pw.write(c);
}
}
ServletOutputStreamProxy:
public class ServletOutputStreamProxy
extends ServletOutputStream {
private ServletOutputStream obj;
public ServletOutputStreamProxy(ServletOutputStream obj){
this.obj = obj;
}
//截獲寫內(nèi)容寫入buffer
public void write(int b) throws IOException {
Integer it = new Integer(b);
HtmlBuffer.addStr(new String(new byte[]{it.byteValue()}));
obj.write(b);
}
}
由于web Httpserver 是多線程執(zhí)行服務端程序,所以buffer應該分線程來存取,這樣大家才能不互相干擾。所以buffer需要實現(xiàn)TreadLocal接口。
HtmlBuffer代碼簡單實現(xiàn)如下:
public class HtmlBuffer {
private static class HtmlInfo extends ThreadLocal {
private Map values = Collections.synchronizedMap(new HashMap());
public Object initialValue() {
return new String();
}
public String getHtmlStr() {
return (String) this.get();
}
public Object get() {
Thread curThread = Thread.currentThread();
Object o = values.get(curThread);
if (o == null && !values.containsKey(curThread)) {
o = initialValue();
values.put(curThread, o);
}
return o;
}
public void set(Object newValue) {
values.put(Thread.currentThread(), newValue);
}
}
private static HtmlInfo htmlInfo = new HtmlInfo();
public static void cleanStr(){
htmlInfo.set( "");
}
public static void addStr(String htmlStr) {
String htmlstr = (String)htmlInfo.get();
if(htmlstr == null) htmlstr ="";
htmlstr += htmlStr;
htmlInfo.set( htmlstr);
}
public static String getStr() {
return (String)htmlInfo.get();
}
}