rabbit.io
Class HTTPOutputStream

java.lang.Object
  extended byjava.io.OutputStream
      extended byjava.io.FilterOutputStream
          extended byjava.io.DataOutputStream
              extended byrabbit.io.HTTPOutputStream
All Implemented Interfaces:
java.io.DataOutput

public class HTTPOutputStream
extends java.io.DataOutputStream

This is an extended DataOutputstream suitable for writing HTTP data. This class handles sending data in chunked manner, set the "Transfer-Encoding" header to "chunked" in the HTTPHeader before sending it to specify it. If you are sending data in chunked mode you must end it correctly. There is two ways to do that: Use the finish method (will use an empty footer) or Manually send a zero sized block followed by the HTTPFooter. Something like this:

// now send the file while ((read = fis.read (buf)) > 0) { os.write (buf, 0, read); } // finish up. if (sendingchunked) { os.write (buf, 0, 0); HTTPFooter footer = new HTTPFooter (); os.writeFooter (footer); }


Constructor Summary
HTTPOutputStream(java.io.OutputStream is)
          Create a new HTTPOutputStream on the underlying stream.
HTTPOutputStream(java.io.OutputStream is, boolean ischunked)
          Create a new HTTPOutputStream on the underlying stream.
 
Method Summary
 void finish()
          This will make sure the stream is finished.
 boolean isChunking()
          Check if this stream is chunking.
 void write(byte[] buf)
          Write a byte array to the underlying stream.
 void write(byte[] buf, int off, int len)
          Write a byte array to the underlying stream.
 void write(int b)
          Write a byte to the underlying stream.
 void writeFooter(HTTPFooter footer)
          Write a HTTPFooter on this stream.
 void writeHTTPHeader(HTTPHeader header)
          Write a HTTPHeader on this stream.
 
Methods inherited from class java.io.DataOutputStream
flush, size, writeBoolean, writeByte, writeBytes, writeChar, writeChars, writeDouble, writeFloat, writeInt, writeLong, writeShort, writeUTF
 
Methods inherited from class java.io.FilterOutputStream
close
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

HTTPOutputStream

public HTTPOutputStream(java.io.OutputStream is)
Create a new HTTPOutputStream on the underlying stream.

Parameters:
is - the underlying stream.

HTTPOutputStream

public HTTPOutputStream(java.io.OutputStream is,
                        boolean ischunked)
Create a new HTTPOutputStream on the underlying stream.

Parameters:
is - the underlying stream.
Method Detail

isChunking

public boolean isChunking()
Check if this stream is chunking.

Returns:
true if this stream is in chunk mode.

writeHTTPHeader

public void writeHTTPHeader(HTTPHeader header)
                     throws java.io.IOException
Write a HTTPHeader on this stream.

Parameters:
header - the HTTPHeader to write.
Throws:
java.io.IOException - if the header could not be written correctly.

write

public void write(int b)
           throws java.io.IOException
Write a byte to the underlying stream. This methods is not synchronized, synchronize if needed.

Parameters:
b - the byte to write.
Throws:
java.io.IOException - if the underlying write does.

write

public void write(byte[] buf)
           throws java.io.IOException
Write a byte array to the underlying stream.

Parameters:
buf - the byte array to write.
Throws:
java.io.IOException - if the underlying write does.

write

public void write(byte[] buf,
                  int off,
                  int len)
           throws java.io.IOException
Write a byte array to the underlying stream.

Parameters:
buf - the byte array to write.
off - the starting offset in the array.
len - the number of bytes to write.
Throws:
java.io.IOException - if the underlying write does.

writeFooter

public void writeFooter(HTTPFooter footer)
                 throws java.io.IOException
Write a HTTPFooter on this stream. If you are sending data in chunked manner you should always end them with writing a zero sized array followed by the footer (can be empty).

Parameters:
footer - the HTTPFooter to write.
Throws:
java.io.IOException

finish

public void finish()
            throws java.io.IOException
This will make sure the stream is finished. For chunked streams this method will send an empty block Followed by an emtyp HTTPFooter.

Throws:
java.io.IOException