/*
 * Copyright
 *   2008 axYus - www.axyus.com
 *   2008 C.Bosquet - charles.bosquet@axyus.com
 *
 * This file is part of XEMELIOS.
 *
 * XEMELIOS is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * XEMELIOS is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with XEMELIOS; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
package fr.gouv.finances.dgfip.xemelios.web.utils;

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

/**
 * @author CBO
 */
public class CharArrayResponseWrapper extends HttpServletResponseWrapper {

  private CharArrayWriter output;
  private ServletOutputStream stream;

  @Override
  public String toString() {
    return output.toString();
  }

  public CharArrayResponseWrapper(HttpServletResponse response) {
    super(response);
    output = new CharArrayWriter();
    stream = new ServletOutputStreamWriter(output);

  }

  @Override
  public PrintWriter getWriter() {
    return new PrintWriter(output);
  }

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


  public char[] toCharArray() {
    return this.output.toCharArray();
  }

  private class ServletOutputStreamWriter extends ServletOutputStream {
      private Writer writer;
      public ServletOutputStreamWriter(Writer writer) {
          super();
          this.writer=writer;
      }

    @Override
    public void write(int b) throws IOException {
        writer.write(b);
    }
  }
}
