org.mortbay.http
Class HttpOutputStream

java.lang.Object
  extended by java.io.OutputStream
      extended by org.mortbay.http.HttpOutputStream
All Implemented Interfaces:
java.io.Closeable, java.io.Flushable, HttpMessage.HeaderWriter, OutputObserver

public class HttpOutputStream
extends java.io.OutputStream
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.28 2006/10/08 14:13:05 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.
protected  void checkOutput()
           
 void close()
          Close the stream.
 void destroy()
           
 void flush()
           
 java.io.OutputStream getBufferedOutputStream()
          Get the buffered output stream.
 int getBufferSize()
          Get the output buffer capacity.
 int getBytesWritten()
           
 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.
protected  int prepareOutput(int length)
           
 void resetBuffer()
          Reset Buffered output.
 void resetObservers()
          Reset the observers.
 void resetStream()
          Reset the stream.
 void setBufferedOutputStream(BufferedOutputStream bos)
           
 void setBufferSize(int size)
          Set the output buffer size.
 void setChunking()
          Set chunking mode.
 void setContentLength(int length)
           
 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
clone, equals, finalize, 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

setContentLength

public void setContentLength(int length)

setBufferedOutputStream

public void setBufferedOutputStream(BufferedOutputStream bos)

getOutputStream

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

Returns:
Raw OutputStream.

getBufferedOutputStream

public java.io.OutputStream getBufferedOutputStream()
Get the buffered output stream.


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.

Throws:
java.io.IOException

isNullOutput

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

Throws:
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()

writeHeader

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

write

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

write

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

write

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

checkOutput

protected void checkOutput()

prepareOutput

protected int prepareOutput(int length)
                     throws java.io.IOException
Throws:
java.io.IOException

flush

public void flush()
           throws java.io.IOException
Specified by:
flush in interface java.io.Flushable
Overrides:
flush in class java.io.OutputStream
Throws:
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.

Specified by:
close in interface java.io.Closeable
Overrides:
close in class java.io.OutputStream
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.
Throws:
java.io.IOException

write

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

getWriter

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

toString

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


Copyright © 2004 Mortbay Consulting Pty. Ltd. All Rights Reserved.