org.openqa.jetty.http
Class HttpOutputStream

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

public class HttpOutputStream
extends 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 Class[] __filterArg
           
 
Fields inherited from interface org.openqa.jetty.util.OutputObserver
__CLOSED, __CLOSING, __COMMITING, __FIRST_WRITE, __RESET_BUFFER
 
Constructor Summary
HttpOutputStream(OutputStream outputStream)
          Constructor.
HttpOutputStream(OutputStream outputStream, int bufferSize)
          Constructor.
HttpOutputStream(OutputStream outputStream, int bufferSize, int headerReserve)
          Constructor.
 
Method Summary
 void addObserver(OutputObserver observer)
          Add an Output Observer.
 void addObserver(OutputObserver observer, Object data)
          Add an Output Observer.
protected  void checkOutput()
           
 void close()
          Close the stream.
 void destroy()
           
 void flush()
           
 OutputStream getBufferedOutputStream()
          Get the buffered output stream.
 int getBufferSize()
          Get the output buffer capacity.
 int getBytesWritten()
           
 OutputStream getOutputStream()
          Get the backing output stream.
 Writer getWriter(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(OutputStream out, int action, 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)
           
 String toString()
           
 void write(byte[] b)
           
 void write(byte[] b, int off, int len)
           
 void write(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 Class[] __filterArg
Constructor Detail

HttpOutputStream

public HttpOutputStream(OutputStream outputStream)
Constructor.

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

HttpOutputStream

public HttpOutputStream(OutputStream outputStream,
                        int bufferSize)
Constructor.

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

HttpOutputStream

public HttpOutputStream(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 OutputStream getOutputStream()
Get the backing output stream. A stream without filters or chunking is returned.

Returns:
Raw OutputStream.

getBufferedOutputStream

public 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 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:
IllegalStateException - If output has been written.

getBytesWritten

public int getBytesWritten()

resetBuffer

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

Throws:
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,
                        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 IOException
Null the output. All output written is discarded until the stream is reset. Used for HEAD requests.

Throws:
IOException

isNullOutput

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

Throws:
IOException

setChunking

public void setChunking()
Set chunking mode.


isChunking

public boolean isChunking()
Get chunking mode


resetStream

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

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

destroy

public void destroy()

writeHeader

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

write

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

write

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

write

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

checkOutput

protected void checkOutput()

prepareOutput

protected int prepareOutput(int length)
                     throws IOException
Throws:
IOException

flush

public void flush()
           throws IOException
Specified by:
flush in interface Flushable
Overrides:
flush in class OutputStream
Throws:
IOException

isClosed

public boolean isClosed()
                 throws IOException
Close the stream.

Throws:
IOException

close

public void close()
           throws IOException
Close the stream.

Specified by:
close in interface Closeable
Overrides:
close in class OutputStream
Throws:
IOException

outputNotify

public void outputNotify(OutputStream out,
                         int action,
                         Object ignoredData)
                  throws 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:
IOException

write

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

getWriter

public Writer getWriter(String encoding)
                 throws IOException
Throws:
IOException

toString

public String toString()
Overrides:
toString in class Object


Copyright © 2012. All Rights Reserved.