CADI.Client.Network
Class HTTPResponseReader

java.lang.Object
  extended by CADI.Client.Network.HTTPResponseReader

public class HTTPResponseReader
extends java.lang.Object

This class provides a wrapper for abstracting the reading of the HTTP response. It provides functions to read the HTTP response header and to read the HTTP response body as a byte array (abstracting the underlying response body encoding).

NOTICE: when the HTTP keep alive is set, the input stream is not closed for all http request and responses. So, after reading an http response, the end of input stream is reached (see endOfStream), and it is only disable for reading the next response if the empty() method has been called before the HTTP request has been sent.

Usage example:
  construct
  readLine
  ....
  readLine
  read or readFully
  ....
  read or readFully

Version:
1.0 2007/10/27
Author:
Group on Interactive Coding of Images (GICI)

Field Summary
private  byte[] buf
          An array of bytes used to buffer the input data.
private  int chunkSize
           
private  int contentLength
           
private  int count
          The index one greater than the last valid byte in the buffer.
private static int CR
          It is the carriage return constant
private  boolean endOfStream
          This attribute indicates whether the end of stream has been reached.
private  java.io.InputStream inputStream
          The input stream where data are read from.
private static int LF
          It is the line feed constant
private  int pos
          The index of the next byte to read from the buffer.
private  boolean rewind
           
private  int tempByte
           
static int TRANSFER_ENCODING_CHUNKED
           
static int TRANSFER_ENCODING_CONTENT_LENGTH
           
static int TRANSFER_ENCODING_NONE
          Allowed values for transferEncoding value.
private  int transferEncoding
          Indicates the transfer encoding mode which is used to delivery the HTTP reesponse body.
 
Constructor Summary
HTTPResponseReader()
          Constructor.
 
Method Summary
 void empty()
          This method reads data until there is not data in the input stream.
 void list(java.io.PrintStream out)
          Prints this HTTP Response Reader fields out to the specified output stream.
 int read()
          Reads the next byte of data from the input stream.
private  int read1()
          Definition in InputStream.read().
private  void readChunk()
          Read a chunk of the http entity body.
private  int readChunkSize()
          Reads the length of the http entity body chunk.
private  void readCRLF()
          Reads the CR-LF
 void readFully(byte[] b, int off, int len)
          Reads a byte array form the input stream.
 java.lang.String readLine()
          Read a line of text.
private  void readTrailer()
          Read the trailer of the http chunked-body
 void setInputStream(java.io.InputStream inputStream)
          Sets the input parameters.
 void setTransferEncoding(java.lang.String transferEncoding, java.lang.String contentLength)
          Sets the transfer encoding type of the http message body.
 java.lang.String toString()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

inputStream

private java.io.InputStream inputStream
The input stream where data are read from.


transferEncoding

private int transferEncoding
Indicates the transfer encoding mode which is used to delivery the HTTP reesponse body.


TRANSFER_ENCODING_NONE

public static final int TRANSFER_ENCODING_NONE
Allowed values for transferEncoding value.

See Also:
Constant Field Values

TRANSFER_ENCODING_CHUNKED

public static final int TRANSFER_ENCODING_CHUNKED
See Also:
Constant Field Values

TRANSFER_ENCODING_CONTENT_LENGTH

public static final int TRANSFER_ENCODING_CONTENT_LENGTH
See Also:
Constant Field Values

contentLength

private int contentLength

buf

private byte[] buf
An array of bytes used to buffer the input data. Elements buf[0] through buf[count-1] are the only bytes that can ever be read from the buffer; element buf[pos] is the next byte to be read/write.


pos

private int pos
The index of the next byte to read from the buffer. This value should always be nonnegative and not larger than the value of count . The next byte to be read from the buffer will be buf[pos] .


count

private int count
The index one greater than the last valid byte in the buffer. This value should always be nonnegative and not larger than the length of buf . It is one greater than the position of the last byte within buf that can ever be read from the buffer.


endOfStream

private boolean endOfStream
This attribute indicates whether the end of stream has been reached.


chunkSize

private int chunkSize

tempByte

private int tempByte

rewind

private boolean rewind

CR

private static final int CR
It is the carriage return constant

See Also:
Constant Field Values

LF

private static final int LF
It is the line feed constant

See Also:
Constant Field Values
Constructor Detail

HTTPResponseReader

public HTTPResponseReader()
Constructor.

Method Detail

setInputStream

public void setInputStream(java.io.InputStream inputStream)
Sets the input parameters.

Parameters:
inputStream - definition in inputStream

setTransferEncoding

public void setTransferEncoding(java.lang.String transferEncoding,
                                java.lang.String contentLength)
                         throws java.net.ProtocolException
Sets the transfer encoding type of the http message body.

Parameters:
transferEncoding - definition in transferEncoding
contentLength - definition in contentLength
Throws:
java.net.ProtocolException

readLine

public java.lang.String readLine()
                          throws java.io.EOFException,
                                 java.io.IOException
Read a line of text. A line is considered to be terminated by any one of a line feed ('\n'), a carriage return ('\r'), or a carriage return followed immediately by a linefeed.

Returns:
A String containing the contents of the line, not including any line-termination characters, or null if the end of the stream has been reached.
Throws:
java.io.IOException - if an I/O error occurs
java.io.EOFException

read

public int read()
         throws java.io.EOFException,
                java.io.IOException
Reads the next byte of data from the input stream. The value byte is returned as an int in the range 0 to 255. If no byte is available because the end of the stream has been reached, an IOException is thrown. This method blocks until input data is availableor an exception is thrown.

A subclass must provide an implementation of this method.

Returns:
the next byte of data, or -1 if the end of the stream is reached.
Throws:
java.io.IOException - if an I/O error occurs or the input stream reaches the end before reading all the bytes.
java.io.EOFException

readFully

public void readFully(byte[] b,
                      int off,
                      int len)
               throws java.io.EOFException,
                      java.io.IOException
Reads a byte array form the input stream. The data read is returned in the byte array that is passed as the first parameters. Memory of the byte array where data will be written must be reserved before this function is called. Otherwise, an exception is thrown.

Parameters:
b - the buffer into which the data is read.
off - the start offset of the data.
len - the number of bytes to read.
Throws:
java.io.EOFException - if this input stream reaches the end before reading all the bytes.
java.io.IOException - if an I/O error occurs.

empty

public void empty()
This method reads data until there is not data in the input stream.

It is useful when the HTTP keep alive feature is used. The input stream will be used for all connections, so data of the last connection that has not been read must be read before the beginning of the new connection.


toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object

list

public void list(java.io.PrintStream out)
Prints this HTTP Response Reader fields out to the specified output stream. This method is useful for debugging.

Parameters:
out - an output stream.

readChunk

private void readChunk()
                throws java.io.EOFException,
                       java.io.IOException
Read a chunk of the http entity body. The data read is saved in the buf attribute.

Throws:
EOFExcpetion - if the end of the buffer has been reached.
java.io.IOException - if an I/O error occurs
java.io.EOFException

readChunkSize

private int readChunkSize()
                   throws java.io.IOException
Reads the length of the http entity body chunk. The chunk extension of the chunk-size length is ignorated.

Returns:
the length of the chunk.
Throws:
java.io.IOException

readCRLF

private void readCRLF()
               throws java.io.IOException
Reads the CR-LF

Throws:
java.io.IOException

readTrailer

private void readTrailer()
                  throws java.io.IOException
Read the trailer of the http chunked-body

Throws:
java.io.IOException

read1

private int read1()
           throws java.io.IOException
Definition in InputStream.read().

Throws:
java.io.IOException


CADI - Copyright (C) 2007
This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

Group on Interactive Coding of Images (GICI) (contact)