How to create a good wrapper for ServletOutputStream?

Discussions

Web tier: servlets, JSP, Web frameworks: How to create a good wrapper for ServletOutputStream?

  1. Hello dudes, i'm trying to create a wrapper to ServletOutputStream, this to store all response data in a file
    and use it as a cache. My problem here is that in one servlet it works fine and the others don't. I really convinced that my wrapper is the problem, but i can't figure out why, here is the code, any help would be appreciated.


    public class JCacheOutputStream extends ServletOutputStream
      implements Serializable
    {
      private ServletOutputStream sos;

      private ByteArrayOutputStream cache;
      
      public JCacheOutputStream( ServletOutputStream sos_ )
      {
        super( );
        
        sos = sos_;
        cache = new ByteArrayOutputStream( );
      }

      public ByteArrayOutputStream getBuffer( )
      {
        return cache;
      }

      public void write(int b)
        throws IOException
      {
        sos.write(b);
        cache.write(b);
      }

      public void write(byte b[])
        throws IOException
      {
        sos.write(b);
        cache.write(b);
      }

      public void write( byte buf[], int offset, int len)
        throws IOException
      {
        sos.write(buf, offset, len);
        cache.write(buf, offset, len);
      }

      Regards,
      Edson
  2. I would like to add flush() to your getBuffer() code


    Marina
    http://www.servletsuite.com - a lot of filters :-)