juglr.net
Class HTTPReader

java.lang.Object
  extended by juglr.net.HTTPReader
Direct Known Subclasses:
HTTPRequestReader, HTTPResponseReader

public class HTTPReader
extends java.lang.Object

base class for HTTPRequestReader and HTTPResponseReader


Field Summary
protected  java.nio.ByteBuffer buf
           
protected  java.nio.channels.SocketChannel channel
           
static int MAX_HEADER_LENGTH
           
 
Constructor Summary
HTTPReader(java.nio.channels.SocketChannel channel, java.nio.ByteBuffer buf)
           
 
Method Summary
 void close()
          Close the underlying channel if it's open
 int readBody(byte[] target)
           
 int readBody(byte[] target, int offset, int len)
           
 int readHeaderField(byte[] target)
          Read a HTTP header line (termincated by \r\n)
 boolean readLF()
          Read the next byte and and check if it is a carriage return - if it is then read the second byte and check if it's a newline - and if both bytes are good return true.
 boolean readSpace()
          Read the next byte and return true if it is a white space
 HTTP.Status readStatus()
          Read a numeric HTTP status code and return it as a HTTP.Status
 HTTP.Version readVersion()
          Parse a HTTP protocol version declaration as used in the HTTP protocol.
 HTTPReader reset(java.nio.channels.SocketChannel channel)
          Reset all state in the reader, preparing it for reading channel.
 java.io.InputStream streamBody()
          Read the message body as a stream, starting from the current position.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

MAX_HEADER_LENGTH

public static final int MAX_HEADER_LENGTH
See Also:
Constant Field Values

channel

protected java.nio.channels.SocketChannel channel

buf

protected java.nio.ByteBuffer buf
Constructor Detail

HTTPReader

public HTTPReader(java.nio.channels.SocketChannel channel,
                  java.nio.ByteBuffer buf)
Method Detail

close

public void close()
           throws java.io.IOException
Close the underlying channel if it's open

Throws:
java.io.IOException - if the underlying channel is open and there is an error closing the channel

reset

public HTTPReader reset(java.nio.channels.SocketChannel channel)
                 throws java.io.IOException
Reset all state in the reader, preparing it for reading channel.

Parameters:
channel - the channel to start reading from. If the reader refers a channel then this channel will be closed.
Returns:
always returns this
Throws:
java.io.IOException - if the reader already refers an open channel and there is an error closing it

readHeaderField

public int readHeaderField(byte[] target)
Read a HTTP header line (termincated by \r\n)

Parameters:
target - the buffer to read data into
Returns:
the number of bytes read into target. If this is 0 then this was the last header before the body and you may invoke readBody(byte[]) or streamBody()

readStatus

public HTTP.Status readStatus()
                       throws java.io.IOException
Read a numeric HTTP status code and return it as a HTTP.Status

Returns:
a symbollic representation of the HTTP status code
Throws:
java.io.IOException - upon errors reading from the socket
HTTP.UnsupportedStatusException - if the server returns and uncommon HTTP status code that is not supported by Juglr

readVersion

public HTTP.Version readVersion()
                         throws java.io.IOException
Parse a HTTP protocol version declaration as used in the HTTP protocol. The buffer will be positioned just after the last parsed character upon method return

Returns:
the HTTP version or HTTP.Version.ERROR on errors
Throws:
java.io.IOException - upon errors reading from the socket

readSpace

public boolean readSpace()
                  throws java.io.IOException
Read the next byte and return true if it is a white space

Returns:
true if the next byte is a space, false otherwise
Throws:
java.io.IOException - upon errors reading from the socket

readLF

public boolean readLF()
               throws java.io.IOException
Read the next byte and and check if it is a carriage return - if it is then read the second byte and check if it's a newline - and if both bytes are good return true.

Returns:
true if the next two bytes are \r and \n false otherwise
Throws:
java.io.IOException - upon errors reading from the socket

readBody

public int readBody(byte[] target)
             throws java.io.IOException
Throws:
java.io.IOException

readBody

public int readBody(byte[] target,
                    int offset,
                    int len)
             throws java.io.IOException
Throws:
java.io.IOException

streamBody

public java.io.InputStream streamBody()
Read the message body as a stream, starting from the current position. When the returned stream is closed this reader will also be closed

Returns:
and input stream reading the message body from the socket