All Packages  Class Hierarchy  This Package  Previous  Next  Index

Class Acme.Serve.servlet.http.ChunkedOutputStream

java.lang.Object
   |
   +----java.io.OutputStream
           |
           +----java.io.FilterOutputStream
                   |
                   +----java.io.BufferedOutputStream
                           |
                           +----Acme.Serve.servlet.http.ChunkedOutputStream

public class ChunkedOutputStream
extends BufferedOutputStream
An OutputStream that implements HTTP/1.1 chunking.

This class lets a Servlet send its response data as an HTTP/1.1 chunked stream. Chunked streams are a way to send arbitrary-length data without having to know beforehand how much you're going to send. They are introduced by a "Transfer-Encoding: chunked" header, so you have to set that header when you make one of these streams.

Sample usage:


 res.setHeader( "Transfer-Encoding", "chunked" );
 OutputStream out = res.getOutputStream();
 ChunkedOutputStream chunkOut = new ChunkedOutputStream( out );
 (write data to chunkOut instead of out)
 (optionally set footers)
 chunkOut.done();
 

Every time the stream gets flushed, a chunk is sent. When done() is called, an empty chunk is sent, marking the end of the chunked stream as per the chunking spec.

Fetch the software.
Fetch the entire Acme package.


Constructor Index

 o ChunkedOutputStream(OutputStream)
Make a ChunkedOutputStream with a default buffer size.
 o ChunkedOutputStream(OutputStream, int)
Make a ChunkedOutputStream with a specified buffer size.

Method Index

 o close()
Make sure that calling close() terminates the chunked stream.
 o done()
Indicate the end of the chunked data by sending a zero-length chunk, possible including footers.
 o flush()
Flush the stream.
 o setFooter(String, String)
Set a footer.
 o write(byte[], int, int)
Write a sub-array of bytes.

Constructors

 o ChunkedOutputStream
 public ChunkedOutputStream(OutputStream out)
Make a ChunkedOutputStream with a default buffer size.

Parameters:
out - the underlying output stream
 o ChunkedOutputStream
 public ChunkedOutputStream(OutputStream out,
                            int size)
Make a ChunkedOutputStream with a specified buffer size.

Parameters:
out - the underlying output stream
size - the buffer size

Methods

 o flush
 public synchronized void flush() throws IOException
Flush the stream. This will write any buffered output bytes as a chunk.

Throws: IOException
if an I/O error occurred
Overrides:
flush in class BufferedOutputStream
 o setFooter
 public void setFooter(String name,
                       String value)
Set a footer. Footers are much like HTTP headers, except that they come at the end of the data instead of at the beginning.

 o done
 public void done() throws IOException
Indicate the end of the chunked data by sending a zero-length chunk, possible including footers.

Throws: IOException
if an I/O error occurred
 o close
 public void close() throws IOException
Make sure that calling close() terminates the chunked stream.

Overrides:
close in class FilterOutputStream
 o write
 public synchronized void write(byte b[],
                                int off,
                                int len) throws IOException
Write a sub-array of bytes.

The only reason we have to override the BufferedOutputStream version of this is that it writes the array directly to the output stream if doesn't fit in the buffer. So we make it use our own chunk-write routine instead. Otherwise this is identical to the parent-class version.

Parameters:
b - the data to be written
off - the start offset in the data
len - the number of bytes that are written
Throws: IOException
if an I/O error occurred
Overrides:
write in class BufferedOutputStream

All Packages  Class Hierarchy  This Package  Previous  Next  Index

ACME Java  ACME Labs