Methods Summary |
---|
public void | close()Upon close, this reads the remainder of the chunked message,
leaving the underlying socket at a position to start reading the
next response without scanning.
if (!closed) {
try {
if (!eof) {
exhaustInputStream(this);
}
} finally {
eof = true;
closed = true;
}
}
|
static void | exhaustInputStream(java.io.InputStream inStream)Exhaust an input stream, reading until EOF has been encountered.
Note that this function is intended as a non-public utility.
This is a little weird, but it seemed silly to make a utility
class for this one function, so instead it is just static and
shared that way.
// read and discard the remainder of the message
byte buffer[] = new byte[1024];
while (inStream.read(buffer) >= 0) {
;
}
|
private int | getChunkSize()Expects the stream to start with a chunksize in hex with optional
comments after a semicolon. The line must end with a CRLF: "a3; some
comment\r\n" Positions the stream at the start of the next line.
// skip CRLF
if (!bof) {
int cr = in.read();
int lf = in.read();
if ((cr != HTTP.CR) || (lf != HTTP.LF)) {
throw new MalformedChunkCodingException(
"CRLF expected at end of chunk");
}
}
//parse data
this.buffer.clear();
int i = this.in.readLine(this.buffer);
if (i == -1) {
throw new MalformedChunkCodingException(
"Chunked stream ended unexpectedly");
}
int separator = this.buffer.indexOf(';");
if (separator < 0) {
separator = this.buffer.length();
}
try {
return Integer.parseInt(this.buffer.substringTrimmed(0, separator), 16);
} catch (NumberFormatException e) {
throw new MalformedChunkCodingException("Bad chunk header");
}
|
public org.apache.http.Header[] | getFooters()
return (Header[])this.footers.clone();
|
private void | nextChunk()Read the next chunk.
chunkSize = getChunkSize();
if (chunkSize < 0) {
throw new MalformedChunkCodingException("Negative chunk size");
}
bof = false;
pos = 0;
if (chunkSize == 0) {
eof = true;
parseTrailerHeaders();
}
|
private void | parseTrailerHeaders()Reads and stores the Trailer headers.
try {
this.footers = AbstractMessageParser.parseHeaders
(in, -1, -1, null);
} catch (HttpException e) {
IOException ioe = new MalformedChunkCodingException("Invalid footer: "
+ e.getMessage());
ExceptionUtils.initCause(ioe, e);
throw ioe;
}
|
public int | read() Returns all the data in a chunked stream in coalesced form. A chunk
is followed by a CRLF. The method returns -1 as soon as a chunksize of 0
is detected.
Trailer headers are read automcatically at the end of the stream and
can be obtained with the getResponseFooters() method.
if (this.closed) {
throw new IOException("Attempted read from closed stream.");
}
if (this.eof) {
return -1;
}
if (this.pos >= this.chunkSize) {
nextChunk();
if (this.eof) {
return -1;
}
}
pos++;
return in.read();
|
public int | read(byte[] b, int off, int len)Read some bytes from the stream.
if (closed) {
throw new IOException("Attempted read from closed stream.");
}
if (eof) {
return -1;
}
if (pos >= chunkSize) {
nextChunk();
if (eof) {
return -1;
}
}
len = Math.min(len, chunkSize - pos);
int count = in.read(b, off, len);
pos += count;
return count;
|
public int | read(byte[] b)Read some bytes from the stream.
return read(b, 0, b.length);
|