rabbit.io
Class HTTPInputStream

java.lang.Object
  extended by java.io.InputStream
      extended by java.io.FilterInputStream
          extended by java.io.DataInputStream
              extended by rabbit.io.HTTPInputStream
All Implemented Interfaces:
java.io.Closeable, java.io.DataInput

public class HTTPInputStream
extends java.io.DataInputStream

This is an extended DataInputStream suitable for reading data from the web. It handles chunked encoding etc. If the underlying input is chunked this class will throw away any trailing footer.


Field Summary
 
Fields inherited from class java.io.FilterInputStream
in
 
Constructor Summary
HTTPInputStream(java.io.InputStream is, Logger logger)
          Create a new HTTPInputStream on the underlying stream.
HTTPInputStream(java.nio.channels.SocketChannel socket, boolean connected, Logger logger, NLSOHandler nlsoHandler)
          Create a new HTTPInputStream on the underlying channel
 
Method Summary
 boolean chunked()
          Get the chunked value.
 void close()
           
 long dataSize()
          Get the size of the page being read.
protected  void finish()
           
protected  int getChunkSize()
          Get the size of the next chunk.
 java.io.InputStream getChunkStream()
          Get the chunked page.
 java.nio.channels.FileChannel getFileChannel()
          Try to get a file channel from this stream.
 boolean getKeepAlive()
          Get the keep alive value.
 java.nio.channels.SocketChannel getSocketChannel()
          Try to get a socket channel from this stream.
 void readCRLF()
          Read of an CR LF combination.
 HTTPHeader readHTTPHeader()
          Read a HTTPHeader of this stream.
 HTTPHeader readHTTPHeader(boolean response)
          Read a HTTPHeader of this stream.
 void setChunked(boolean b)
          Set the chunkin of this stream.
 void setKeepAlive(boolean keepalive)
          Set the keep alive value to currentkeepalive & keepalive
protected  void verifyResponse()
          Verify that the response starts with "HTTP/" Failure to verify response => treat all of data as content = HTTP/0.9.
 
Methods inherited from class java.io.DataInputStream
read, read, readBoolean, readByte, readChar, readDouble, readFloat, readFully, readFully, readInt, readLine, readLong, readShort, readUnsignedByte, readUnsignedShort, readUTF, readUTF, skipBytes
 
Methods inherited from class java.io.FilterInputStream
available, mark, markSupported, read, reset, skip
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

HTTPInputStream

public HTTPInputStream(java.io.InputStream is,
                       Logger logger)
Create a new HTTPInputStream on the underlying stream.

Parameters:
is - the underlying stream

HTTPInputStream

public HTTPInputStream(java.nio.channels.SocketChannel socket,
                       boolean connected,
                       Logger logger,
                       NLSOHandler nlsoHandler)
                throws java.io.IOException
Create a new HTTPInputStream on the underlying channel

Parameters:
socket - the socket to use.
connected - false if there is a asynchronous connection pending, true if the socket is fully connected
Throws:
java.io.IOException
Method Detail

readHTTPHeader

public HTTPHeader readHTTPHeader()
                          throws java.io.IOException
Read a HTTPHeader of this stream. If data is chunked the whole page will be read in so to get it use the getChunkStream. something like this HTTPHeader header = readHeader (); if (chunked) { contentStream = getChunkStream (); }

Returns:
a new HTTPHeader read from the stream.
Throws:
java.io.IOException - if the HTTPHeader could not be read correctly.

readHTTPHeader

public HTTPHeader readHTTPHeader(boolean response)
                          throws java.io.IOException
Read a HTTPHeader of this stream. If data is chunked the whole page will be read in so to get it use the getChunkStream. something like this HTTPHeader header = readHeader (); if (chunked) { contentStream = getChunkStream (); }

Parameters:
response - if true a response will be read. If the underlying stream does not start with HTTP/ a newly created http response header will be returned and the bytes read will be pushed back onto the stream.
Returns:
a new HTTPHeader read from the stream.
Throws:
java.io.IOException - if the HTTPHeader could not be read correctly.

verifyResponse

protected void verifyResponse()
                       throws java.io.IOException
Verify that the response starts with "HTTP/" Failure to verify response => treat all of data as content = HTTP/0.9.

Throws:
java.io.IOException

getKeepAlive

public boolean getKeepAlive()
Get the keep alive value.

Returns:
if this stream is in keepalive mode.

setKeepAlive

public void setKeepAlive(boolean keepalive)
Set the keep alive value to currentkeepalive & keepalive

Parameters:
keepalive - the new keepalive value.

chunked

public boolean chunked()
Get the chunked value.

Returns:
true if the last read HTTPHeader was chunked.

setChunked

public void setChunked(boolean b)
Set the chunkin of this stream. Useful for turning off chunking on persistent connections.

Parameters:
b - the new chunking value.

dataSize

public long dataSize()
Get the size of the page being read.

Returns:
the size of the page being read or -1 if unknown.

getChunkSize

protected int getChunkSize()
                    throws java.io.IOException
Get the size of the next chunk.

Returns:
the size of the next chunk.
Throws:
java.io.IOException - if the chunk size could not be read correctly.

readCRLF

public void readCRLF()
              throws java.io.IOException
Read of an CR LF combination.

Throws:
java.io.IOException - if the CR LF combination could not be read correctly.

getChunkStream

public java.io.InputStream getChunkStream()
                                   throws java.io.IOException
Get the chunked page.

Returns:
an InputStream to the previously read chunked pagel.
Throws:
java.io.IOException

finish

protected void finish()

close

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

getFileChannel

public java.nio.channels.FileChannel getFileChannel()
Try to get a file channel from this stream.

Returns:
a FileChannel or null if no file channel is available.

getSocketChannel

public java.nio.channels.SocketChannel getSocketChannel()
Try to get a socket channel from this stream.

Returns:
a SocketChanne or null if no socket channel is available.