package org.qfox.jestful.server;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletResponseWrapper;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;

/**
 * <p>
 * Description:
 * </p>
 * <p>
 * <p>
 * Company: 广州市俏狐信息科技有限公司
 * </p>
 *
 * @author Payne 646742615@qq.com
 * @date 2016年6月1日 下午10:03:56
 * @since 1.0.0
 */
public class JestfulHeadResponse extends HttpServletResponseWrapper {
    private JestfulHeadResponseServletOutputStream out;
    private PrintWriter writer;

    public JestfulHeadResponse(HttpServletResponse response) {
        super(response);
    }

    public void finish() {
        super.setContentLength(out != null ? out.size : 0);
    }

    @Override
    public ServletOutputStream getOutputStream() throws IOException {
        if (out == null) {
            out = new JestfulHeadResponseServletOutputStream();
        }
        return out;
    }

    @Override
    public PrintWriter getWriter() throws IOException {
        if (writer == null) {
            String charset = getCharacterEncoding();
            ServletOutputStream sos = getOutputStream();
            OutputStreamWriter osw = new OutputStreamWriter(sos, charset);
            writer = new PrintWriter(osw, true);
        }
        return writer;
    }

    private static class JestfulHeadResponseServletOutputStream extends ServletOutputStream {
        private int size = 0;

        @Override
        public void write(int b) {
            size++;
        }

        @Override
        public void write(byte[] b) throws IOException {
            size += b.length;
        }

        @Override
        public void write(byte buf[], int offset, int len) throws IOException {
            size += len;
        }

    }

}

@@ -27,16 +27,124 @@
 erver;%0A%0A
+import org.springframework.cglib.proxy.Enhancer;%0Aimport org.springframework.cglib.proxy.InvocationHandler;%0A%0A
 import j
@@ -368,16 +368,49 @@
 tWriter;
+%0Aimport java.lang.reflect.Method;
 %0A%0A/**%0A *
@@ -645,41 +645,104 @@
 per 
-%7B%0A    private JestfulHeadResponse
+implements InvocationHandler %7B%0A    private ServletOutputStream servletOutputStream;%0A    private 
 Serv
@@ -757,19 +757,35 @@
 tStream 
-out
+enhanceOutputStream
 ;%0A    pr
@@ -802,22 +802,62 @@
 tWriter 
-writer
+printWriter;%0A    private int contentLength = 0
 ;%0A%0A    p
@@ -1008,34 +1008,21 @@
 gth(
-out != null ? out.size : 0
+contentLength
 );%0A 
@@ -1123,19 +1123,35 @@
     if (
-out
+enhanceOutputStream
  == null
@@ -1170,57 +1170,175 @@
     
-out = new JestfulHeadResponseServletOutputStream(
+servletOutputStream = super.getOutputStream();%0A            enhanceOutputStream = (ServletOutputStream) Enhancer.create(ServletOutputStream.class, new Class%3C?%3E%5B0%5D, this
 );%0A 
@@ -1365,11 +1365,27 @@
 urn 
-out
+enhanceOutputStream
 ;%0A  
@@ -1471,17 +1471,22 @@
     if (
-w
+printW
 riter ==
@@ -1691,17 +1691,22 @@
         
-w
+printW
 riter = 
@@ -1758,17 +1758,22 @@
  return 
-w
+printW
 riter;%0A 
@@ -1786,181 +1786,266 @@
     
-private static class JestfulHeadResponseServletOutputStream extends ServletOutputStream %7B%0A        private int size = 0;%0A%0A        @Override%0A        public void write(int b) %7B
+@Override%0A    public Object invoke(Object o, Method method, Object%5B%5D args) throws Throwable %7B%0A        if (%22write%22.equals(method.getName()) && method.getParameterTypes().length == 1 && method.getParameterTypes()%5B0%5D == int.class) %7B%0A            contentLength++;
 %0A   
@@ -2057,14 +2057,19 @@
     
-size++
+return null
 ;%0A  
@@ -2072,33 +2072,32 @@
 ;%0A        %7D%0A
-%0A
         
 @Override%0A  
@@ -2088,249 +2088,560 @@
     
-@Override%0A        public void write(byte%5B%5D b) throws IOException %7B%0A            size += b.length;%0A        %7D%0A%0A        @Override%0A        public void write(byte buf%5B%5D, int offset, int len) throws IOException %7B%0A            size += len;%0A        %7D%0A
+if (%22write%22.equals(method.getName()) && method.getParameterTypes().length == 1 && method.getParameterTypes()%5B0%5D == byte%5B%5D.class) %7B%0A            byte%5B%5D b = (byte%5B%5D) args%5B0%5D;%0A            contentLength += b.length;%0A            return null;%0A        %7D%0A        if (%22write%22.equals(method.getName()) && method.getParameterTypes().length == 3 && method.getParameterTypes()%5B2%5D == int.class) %7B%0A            Integer len = (Integer) args%5B2%5D;%0A            contentLength += len;%0A            return null;%0A        %7D%0A        return method.invoke(servletOutputStream, args);
 %0A   

Make Framework Supports Servlet-API 3.0.1 or Later And JDK 1.6 or Later