org.mortbay.http
Class HttpOutputStream

java.lang.Object
  |
  +--java.io.OutputStream
        |
        +--java.io.FilterOutputStream
              |
              +--org.mortbay.http.HttpOutputStream
All Implemented Interfaces:
HttpMessage.HeaderWriter, OutputObserver

public class HttpOutputStream
extends java.io.FilterOutputStream
implements OutputObserver, HttpMessage.HeaderWriter

HTTP Http OutputStream. Acts as a BufferedOutputStream until setChunking() is called. Once chunking is enabled, the raw stream is chunk encoded as per RFC2616. Implements the following HTTP and Servlet features:

This class is not synchronized and should be synchronized explicitly if an instance is used by multiple threads.

Version:
$Id: HttpOutputStream.java,v 1.16.2.1 2003/07/19 03:42:46 gregwilkins Exp $
Author:
Greg Wilkins

Field Summary
static java.lang.Class[] __filterArg
           
 
Fields inherited from interface org.mortbay.util.OutputObserver
__CLOSED, __CLOSING, __COMMITING, __FIRST_WRITE, __RESET_BUFFER
 
Constructor Summary
HttpOutputStream(java.io.OutputStream outputStream)
          Constructor.
HttpOutputStream(java.io.OutputStream outputStream, int bufferSize)
          Constructor.
HttpOutputStream(java.io.OutputStream outputStream, int bufferSize, int headerReserve)
          Constructor.
 
Method Summary
 void addObserver(OutputObserver observer)
          Add an Output Observer.
 void addObserver(OutputObserver observer, java.lang.Object data)
          Add an Output Observer.
 void close()
          Close the stream.
 void destroy()
           
 void flush()
           
 int getBufferSize()
          Get the output buffer capacity.
 int getBytesWritten()
           
 java.io.OutputStream getFilterStream()
          Get Filter OutputStream.
 java.io.OutputStream getOutputStream()
          Get the backing output stream.
 java.io.Writer getWriter(java.lang.String encoding)
           
 boolean isChunking()
          Get chunking mode
 boolean isClosed()
          Close the stream.
 boolean isNullOutput()
          is the output Nulled?
 boolean isWritten()
          Has any data been written to the stream.
 void nullOutput()
          Null the output.
 void outputNotify(java.io.OutputStream out, int action, java.lang.Object ignoredData)
          Output Notification.
 void resetBuffer()
          Reset Buffered output.
 void resetObservers()
          Reset the observers.
 void resetStream()
          Reset the stream.
 void setBufferedOutputStream(BufferedOutputStream bos, boolean bufferHeaders)
           
 void setBufferSize(int size)
          Set the output buffer size.
 void setChunking()
          Set chunking mode.
 void setFilterStream(java.io.OutputStream filter)
          Set Filter OutputStream.
 void setTrailer(HttpFields trailer)
          Set the trailer to send with a chunked close.
 java.lang.String toString()
           
 void write(byte[] b)
           
 void write(byte[] b, int off, int len)
           
 void write(java.io.InputStream in, int len)
           
 void write(int b)
           
 void writeHeader(HttpMessage httpMessage)
           
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

__filterArg

public static final java.lang.Class[] __filterArg
Constructor Detail

HttpOutputStream

public HttpOutputStream(java.io.OutputStream outputStream)
Constructor.

Parameters:
outputStream - The outputStream to buffer or chunk to.

HttpOutputStream

public HttpOutputStream(java.io.OutputStream outputStream,
                        int bufferSize)
Constructor.

Parameters:
outputStream - The outputStream to buffer or chunk to.

HttpOutputStream

public HttpOutputStream(java.io.OutputStream outputStream,
                        int bufferSize,
                        int headerReserve)
Constructor.

Parameters:
outputStream - The outputStream to buffer or chunk to.
Method Detail

setBufferedOutputStream

public void setBufferedOutputStream(BufferedOutputStream bos,
                                    boolean bufferHeaders)

getOutputStream

public java.io.OutputStream getOutputStream()
Get the backing output stream. A stream without filters or chunking is returned.

Returns:
Raw OutputStream.

getFilterStream

public java.io.OutputStream getFilterStream()
Get Filter OutputStream. Get the current top of the OutputStream filter stack

Returns:
OutputStream.

setFilterStream

public void setFilterStream(java.io.OutputStream filter)
Set Filter OutputStream. Set output filter stream, which should be constructed to wrap the stream returned from get FilterStream.


isWritten

public boolean isWritten()
Has any data been written to the stream.

Returns:
True if write has been called.

getBufferSize

public int getBufferSize()
Get the output buffer capacity.

Returns:
Buffer capacity in bytes.

setBufferSize

public void setBufferSize(int size)
                   throws java.lang.IllegalStateException
Set the output buffer size. Note that this is the minimal buffer size and that installed filters may perform their own buffering and are likely to change the size of the output. Also the pre and post reserve buffers may be allocated within the buffer for headers and chunking.

Parameters:
size - Minimum buffer size in bytes
Throws:
java.lang.IllegalStateException - If output has been written.

getBytesWritten

public int getBytesWritten()

resetBuffer

public void resetBuffer()
                 throws java.lang.IllegalStateException
Reset Buffered output. If no data has been committed, the buffer output is discarded and the filters may be reinitialized.

Throws:
java.lang.IllegalStateException

addObserver

public void addObserver(OutputObserver observer)
Add an Output Observer. Output Observers get notified of significant events on the output stream. Observers are called in the reverse order they were added. They are removed when the stream is closed.

Parameters:
observer - The observer.

addObserver

public void addObserver(OutputObserver observer,
                        java.lang.Object data)
Add an Output Observer. Output Observers get notified of significant events on the output stream. Observers are called in the reverse order they were added. They are removed when the stream is closed.

Parameters:
observer - The observer.
data - Data to be passed wit notify calls.

resetObservers

public void resetObservers()
Reset the observers.


nullOutput

public void nullOutput()
                throws java.io.IOException
Null the output. All output written is discarded until the stream is reset. Used for HEAD requests.

java.io.IOException

isNullOutput

public boolean isNullOutput()
                     throws java.io.IOException
is the output Nulled?

java.io.IOException

setChunking

public void setChunking()
Set chunking mode.


isChunking

public boolean isChunking()
Get chunking mode


resetStream

public void resetStream()
                 throws java.io.IOException,
                        java.lang.IllegalStateException
Reset the stream. Turn disable all filters.

Throws:
java.lang.IllegalStateException - The stream cannot be reset if chunking is enabled.
java.io.IOException

destroy

public void destroy()

setTrailer

public void setTrailer(HttpFields trailer)
Set the trailer to send with a chunked close.

Parameters:
trailer -

writeHeader

public void writeHeader(HttpMessage httpMessage)
                 throws java.io.IOException
Specified by:
writeHeader in interface HttpMessage.HeaderWriter
java.io.IOException

write

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

write

public void write(byte[] b)
           throws java.io.IOException
Overrides:
write in class java.io.FilterOutputStream
java.io.IOException

write

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

flush

public void flush()
           throws java.io.IOException
Overrides:
flush in class java.io.FilterOutputStream
java.io.IOException

isClosed

public boolean isClosed()
                 throws java.io.IOException
Close the stream.

Throws:
java.io.IOException

close

public void close()
           throws java.io.IOException
Close the stream.

Overrides:
close in class java.io.FilterOutputStream
Throws:
java.io.IOException

outputNotify

public void outputNotify(java.io.OutputStream out,
                         int action,
                         java.lang.Object ignoredData)
                  throws java.io.IOException
Output Notification. Called by the internal Buffered Output and the event is passed on to this streams observers.

Specified by:
outputNotify in interface OutputObserver
Parameters:
out - The OutputStream that caused the event
action - The action taken
ignoredData - Data associated with the event.
java.io.IOException

write

public void write(java.io.InputStream in,
                  int len)
           throws java.io.IOException
java.io.IOException

getWriter

public java.io.Writer getWriter(java.lang.String encoding)
                         throws java.io.IOException
java.io.IOException

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object


Copyright ? 2000 Mortbay Consulting Pty. Ltd. All Rights Reserved.