public class HTTPResponseReader extends java.lang.Object implements JPIPMessageReader
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
Modifier and Type | Field and Description |
---|---|
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 and Description |
---|
HTTPResponseReader()
Constructor.
|
Modifier and Type | Method and Description |
---|---|
void |
empty()
This method reads data until there is not data in the input stream.
|
boolean |
isEndOfStream() |
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() |
private java.io.InputStream inputStream
private int transferEncoding
public static final int TRANSFER_ENCODING_NONE
public static final int TRANSFER_ENCODING_CHUNKED
public static final int TRANSFER_ENCODING_CONTENT_LENGTH
private int contentLength
private byte[] buf
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.private int pos
count
. The next byte to be read from the buffer will be buf[pos]
.private int count
buf
. It is one greater than the position of the last byte within
buf
that can ever be read from the buffer.private boolean endOfStream
private int chunkSize
private int tempByte
private boolean rewind
private static final int CR
private static final int LF
public void setInputStream(java.io.InputStream inputStream)
inputStream
- definition in inputStream
public void setTransferEncoding(java.lang.String transferEncoding, java.lang.String contentLength) throws java.net.ProtocolException
transferEncoding
- definition in transferEncoding
contentLength
- definition in contentLength
java.net.ProtocolException
public java.lang.String readLine() throws java.io.EOFException, java.io.IOException
java.io.IOException
- if an I/O error occursjava.io.EOFException
public int read() throws java.io.EOFException, java.io.IOException
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.
read
in interface JPIPMessageReader
-1
if the end of the
stream is reached.java.io.IOException
- if an I/O error occurs or the input stream reaches
the end before reading all the bytes.java.io.EOFException
public void readFully(byte[] b, int off, int len) throws java.io.EOFException, java.io.IOException
readFully
in interface JPIPMessageReader
b
- the buffer into which the data is read.off
- the start offset of the data.len
- the number of bytes to read.java.io.EOFException
- if this input stream reaches the end before
reading all the bytes.java.io.IOException
- if an I/O error occurs.public void empty()
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.
public boolean isEndOfStream()
public java.lang.String toString()
toString
in class java.lang.Object
public void list(java.io.PrintStream out)
out
- an output stream.private void readChunk() throws java.io.EOFException, java.io.IOException
buf
attribute.EOFExcpetion
- if the end of the buffer has been reached.java.io.IOException
- if an I/O error occursjava.io.EOFException
private int readChunkSize() throws java.io.IOException
java.io.IOException
private void readCRLF() throws java.io.IOException
java.io.IOException
private void readTrailer() throws java.io.IOException
java.io.IOException
private int read1() throws java.io.IOException
InputStream.read()
.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)