diff options
| author | David Daney <ddaney@avtrex.com> | 2006-02-27 22:50:33 +0000 |
|---|---|---|
| committer | David Daney <ddaney@avtrex.com> | 2006-02-27 22:50:33 +0000 |
| commit | 76a7a490b44807d0a8ede965b17cef2451afedbf (patch) | |
| tree | e729f3e9d972ca32bc6f3b9d2454b7bfdf2031c1 /gnu/java | |
| parent | fd01df1788740627e68faaa32bb26521095821c9 (diff) | |
| download | classpath-76a7a490b44807d0a8ede965b17cef2451afedbf.tar.gz | |
PR classpath/26312
* gnu/java/net/protocol/http/ChunkedInputStream.java (imports): Cleaned
up.
(ChunkedInputStream): Extend InputStream.
(in): New field.
(headers): Moved to top of class.
(constructor): Save referenct to in.
(read(byte[])): Removed method.
(read(byte[], int, int)): Made synchronized and throw IOException
on error parsing chunk header.
(available): New method.
(close): New method.
Diffstat (limited to 'gnu/java')
| -rw-r--r-- | gnu/java/net/protocol/http/ChunkedInputStream.java | 71 |
1 files changed, 59 insertions, 12 deletions
diff --git a/gnu/java/net/protocol/http/ChunkedInputStream.java b/gnu/java/net/protocol/http/ChunkedInputStream.java index 58cd2d681..9870412f2 100644 --- a/gnu/java/net/protocol/http/ChunkedInputStream.java +++ b/gnu/java/net/protocol/http/ChunkedInputStream.java @@ -38,23 +38,35 @@ exception statement from your version. */ package gnu.java.net.protocol.http; -import java.io.FilterInputStream; import java.io.IOException; import java.io.InputStream; import java.net.ProtocolException; + +// +// Note that we rely on the implemtation of skip() in the super class +// (InputStream) calling our read methods to account for chunk headers +// while skipping. +// + + /** * Input stream wrapper for the "chunked" transfer-coding. * * @author Chris Burdess (dog@gnu.org) */ public class ChunkedInputStream - extends FilterInputStream + extends InputStream { private static final byte CR = 0x0d; private static final byte LF = 0x0a; + Headers headers; + + /** The underlying stream. */ + private InputStream in; + /** Size of the chunk we're reading. */ int size; /** Number of bytes we've read in this chunk. */ @@ -66,7 +78,6 @@ public class ChunkedInputStream boolean meta; /** True when we've hit EOF. */ boolean eof; - Headers headers; /** * Constructor. @@ -75,7 +86,7 @@ public class ChunkedInputStream */ public ChunkedInputStream(InputStream in, Headers headers) { - super(in); + this.in = in; this.headers = headers; size = -1; count = 0; @@ -94,13 +105,7 @@ public class ChunkedInputStream return 0xff & buf[0]; } - public int read(byte[] buffer) - throws IOException - { - return read(buffer, 0, buffer.length); - } - - public int read(byte[] buffer, int offset, int length) + public synchronized int read(byte[] buffer, int offset, int length) throws IOException { if (eof) @@ -122,7 +127,18 @@ public class ChunkedInputStream } else if (c == 0x0a && last == 0x0d) // CRLF { - size = Integer.parseInt(buf.toString(), 16); + try + { + size = Integer.parseInt(buf.toString(), 16); + } + catch (NumberFormatException nfe) + { + IOException ioe = new IOException("Bad chunk header"); + ioe.initCause(nfe); + // Unrecoverable. Don't try to read more. + in.close(); + throw ioe; + } break; } else if (!seenSemi && c >= 0x30) @@ -174,6 +190,37 @@ public class ChunkedInputStream return len; } } + + /** + * This method returns the number of bytes that can be read from + * this stream before a read might block. Even if the underlying + * InputStream has data available past the end of the current chunk, + * we have no way of knowing how large the next chunk header will + * be. So we cannot report available data past the current chunk. + * + * @return The number of bytes that can be read before a read might + * block + * + * @exception IOException If an error occurs + */ + public int available() throws IOException + { + if (meta) + return 0; + + return Math.min(in.available(), size - count); + } + + /** + * This method closes the ChunkedInputStream by closing the underlying + * InputStream. + * + * @exception IOException If an error occurs + */ + public void close() throws IOException + { + in.close(); + } } |
