com.primix.tapestry
Class ResponseOutputStream

java.lang.Object
  |
  +--java.io.OutputStream
        |
        +--com.primix.tapestry.ResponseOutputStream

public class ResponseOutputStream
extends java.io.OutputStream

A special output stream works with a HttpServletResponse, buffering data so as to defer opening the response's output stream.

A ResponseOutputStream must be closed specially, using forceClose(). Otherwise, it ignores close(), because of unwanted chaining of close() from outer writers and streams.

The buffering is pretty simple because the code between IResponseWriter and this shows lots of buffering after the PrintWriter and inside the OutputStreamWriter that can't be configured.

Possibly, this needs to be rethunk. Perhaps we need a ResponseWriter class instead that could trap the output characters before they get into the PrintWriter and its crew. This would save on the number of conversions between characters and bytes.

In fact, this is now even less useful, because the Body component (which will be used on virtually all Tapestry pages), buffers its wrapped contents (that is, evertyhing inside the <body> tag in the generated HTML).

Version:
$Id: ResponseOutputStream.java,v 1.5 2001/05/02 14:15:15 hship Exp $
Author:
Howard Ship

Field Summary
static int DEFAULT_SIZE
          Default size for the buffer (2000 bytes).
 
Constructor Summary
ResponseOutputStream(HttpServletResponse response)
          Creates the stream with the default maximum buffer size.
ResponseOutputStream(HttpServletResponse response, int maxSize)
          Standard constructor.
 
Method Summary
 void close()
          Does nothing.
 void flush()
          Flushes the underlying output stream, if is has been opened.
 void forceClose()
          Flushes the internal buffer to the underlying output stream, then closes the underlying output stream.
 void forceFlush()
          Writes the internal buffer to the output stream, opening it if necessary, then flushes the output stream.
 java.lang.String getContentType()
           
 boolean getDiscard()
           
 void reset()
          Discards all output in the buffer.
 void setBufferSize(int value)
          Changes the maximum buffer size.
 void setContentType(java.lang.String value)
           
 void setDiscard(boolean value)
          Indicates whether the stream should ignore all data written to it.
 void write(byte[] b, int off, int len)
           
 void write(int b)
           
 
Methods inherited from class java.io.OutputStream
write
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DEFAULT_SIZE

public static final int DEFAULT_SIZE
Default size for the buffer (2000 bytes).
Constructor Detail

ResponseOutputStream

public ResponseOutputStream(HttpServletResponse response)
Creates the stream with the default maximum buffer size.

ResponseOutputStream

public ResponseOutputStream(HttpServletResponse response,
                            int maxSize)
Standard constructor.
Method Detail

close

public void close()
           throws java.io.IOException
Does nothing. This is because of chaining of close() from IResponseWriter.close() ... see flush().
Overrides:
close in class java.io.OutputStream

flush

public void flush()
           throws java.io.IOException
Flushes the underlying output stream, if is has been opened.

This method explicitly does not flush the internal buffer ... that's because when an IResponseWriter is closed (for instance, because an exception is thrown), that close() spawns flush()es and close()s throughout the output stream chain, eventually reaching this method.

Overrides:
flush in class java.io.OutputStream
See Also:
forceFlush(), forceClose()

forceClose

public void forceClose()
                throws java.io.IOException
Flushes the internal buffer to the underlying output stream, then closes the underlying output stream. If the output stream has not yet been opened (meaning the entire response page is in the internal buffer), then setContentLength() is invoked on the HttpServletResponse ... this allows the web server and client to use Keep-Alive connections.

In rare instances (such as sending a HTTP redirect), the ReponseOutputStream is closed when no data has been written to it. In that case, we never invoke HttpServletResponse.getOutputStream().


forceFlush

public void forceFlush()
                throws java.io.IOException
Writes the internal buffer to the output stream, opening it if necessary, then flushes the output stream. Future writes will go directly to the output stream.

getContentType

public java.lang.String getContentType()

getDiscard

public boolean getDiscard()

reset

public void reset()
           throws java.io.IOException
Discards all output in the buffer. This is used after an error to restart the output (so that the error may be presented).

Clears the discard flag.


setBufferSize

public void setBufferSize(int value)
                   throws java.io.IOException
Changes the maximum buffer size. If the new buffer size is smaller \ than the number of bytes already in the buffer, the buffer is immediately flushed.

setContentType

public void setContentType(java.lang.String value)

setDiscard

public void setDiscard(boolean value)
Indicates whether the stream should ignore all data written to it.

write

public void write(byte[] b,
                  int off,
                  int len)
           throws java.io.IOException
Overrides:
write in class java.io.OutputStream

write

public void write(int b)
           throws java.io.IOException
Overrides:
write in class java.io.OutputStream