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;
import java.io.UnsupportedEncodingException;

/**
 * <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 final JestfulHeadResponseServletOutputStream out = new JestfulHeadResponseServletOutputStream();
    private PrintWriter writer;

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

    public void finish() {
        super.setContentLength(out.size);
    }

    @Override
    public ServletOutputStream getOutputStream() throws IOException {
        return out;
    }

    @Override
    public PrintWriter getWriter() throws UnsupportedEncodingException {
        if (writer == null) {
            String charset = getCharacterEncoding();
            OutputStreamWriter osw = new OutputStreamWriter(out, 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;
        }

    }

}

@@ -264,53 +264,8 @@
 ter;
-%0Aimport java.io.UnsupportedEncodingException;
 %0A%0A/*
@@ -518,14 +518,8 @@
 ate 
-final 
 Jest
@@ -560,55 +560,8 @@
  out
- = new JestfulHeadResponseServletOutputStream()
 ;%0A  
@@ -751,13 +751,31 @@
 (out
+ != null ? out
 .size
+ : 0
 );%0A 
@@ -856,32 +856,133 @@
 s IOException %7B%0A
+        if (out == null) %7B%0A            out = new JestfulHeadResponseServletOutputStream();%0A        %7D%0A
         return o
@@ -1052,27 +1052,10 @@
 ows 
-UnsupportedEncoding
+IO
 Exce
@@ -1141,24 +1141,81 @@
 Encoding();%0A
+            ServletOutputStream sos = getOutputStream();%0A
             
@@ -1262,19 +1262,19 @@
 mWriter(
-out
+sos
 , charse

Make Framework Supports Servlet-API 3.0.1 or Later