rabbit.io
Class HTTPOutputStream

java.lang.Object
  extended by java.io.OutputStream
      extended by java.io.FilterOutputStream
          extended by java.io.DataOutputStream
              extended by rabbit.io.HTTPOutputStream
All Implemented Interfaces:
java.io.Closeable, java.io.DataOutput, java.io.Flushable

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); }


Field Summary
 
Fields inherited from class java.io.DataOutputStream
written
 
Fields inherited from class java.io.FilterOutputStream
out
 
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.
HTTPOutputStream(java.nio.channels.SocketChannel socket)
          Create a new HTTPOutputStream on the underlying channel.
 
Method Summary
 void close()
           
protected  void crlf(java.io.OutputStream is)
           
 void finish()
          This will make sure the stream is finished.
 java.nio.channels.WritableByteChannel getChannel()
           
 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.
 void writeHTTPHeader(HTTPHeader header, boolean proxyConnected, java.lang.String proxyAuth)
          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.lang.Object
clone, equals, finalize, 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.nio.channels.SocketChannel socket)
                 throws java.io.IOException
Create a new HTTPOutputStream on the underlying channel.

Parameters:
socket - the socket to use.
Throws:
java.io.IOException

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.

writeHTTPHeader

public void writeHTTPHeader(HTTPHeader header,
                            boolean proxyConnected,
                            java.lang.String proxyAuth)
                     throws java.io.IOException
Write a HTTPHeader on this stream.

Parameters:
header - the HTTPHeader to write.
proxyConnected - true if this connection is connected to another proxy.
proxyAuth - the proxy authentication for the next proxy in the proxy chain.
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.

Specified by:
write in interface java.io.DataOutput
Overrides:
write in class java.io.DataOutputStream
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.

Specified by:
write in interface java.io.DataOutput
Overrides:
write in class java.io.FilterOutputStream
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.

Specified by:
write in interface java.io.DataOutput
Overrides:
write in class java.io.DataOutputStream
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.

crlf

protected void crlf(java.io.OutputStream is)
             throws java.io.IOException
Throws:
java.io.IOException

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

close

public void close()
           throws java.io.IOException
Specified by:
close in interface java.io.Closeable
Overrides:
close in class java.io.FilterOutputStream
Throws:
java.io.IOException

getChannel

public java.nio.channels.WritableByteChannel getChannel()