Class HttpInputStream
- java.lang.Object
-
- java.io.InputStream
-
- java.io.FilterInputStream
-
- sunlabs.brazil.util.http.HttpInputStream
-
- All Implemented Interfaces:
java.io.Closeable,java.lang.AutoCloseable
public class HttpInputStream extends java.io.FilterInputStreamThis class is an input stream that provides added methods that are of help when reading the result of an HTTP request. By setting up this input stream, the user can conveniently read lines of text and copy the contents of an input stream to an output stream.The underlying assumption of this class is that when reading the result of an HTTP request, each byte in the input stream represents an 8-bit ASCII character, and as such, it is perfectly valid to treat each byte as a character. Locale-based conversion is not appropriate in this circumstance, so the
java.io.BufferedReader.readLinemethod should not be used.- Version:
- 2.2
- Author:
- Colin Stevens (colin.stevens@sun.com)
-
-
Field Summary
Fields Modifier and Type Field Description static intdefaultBufsizeThe default size of the temporary buffer used when copying from an input stream to an output stream.
-
Constructor Summary
Constructors Constructor Description HttpInputStream(java.io.InputStream in)Creates a new HttpInputStream that reads its input from the specified input stream.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description intcopyTo(java.io.OutputStream out)Copies bytes from this input stream to the specified output stream until end of the input stream is reached.intcopyTo(java.io.OutputStream out, int len)Copies bytes from this input stream to the specified output stream until the specified number of bytes are copied or the end of the input stream is reached.intcopyTo(java.io.OutputStream out, int len, byte[] buf)Copies bytes from this input stream to the specified output stream until the specified number of bytes are copied or the end of the input stream is reached.intreadFully(byte[] buf)Readsbuf.lengthbytes from the input stream.intreadFully(byte[] buf, int off, int len)Reads the specified number of bytes from the input stream.java.lang.StringreadLine()Reads the next line of text from the input stream.java.lang.StringreadLine(int limit)Reads the next line of text from the input stream, up to the limit specified.-
Methods inherited from class java.io.FilterInputStream
available, close, mark, markSupported, read, read, read, reset, skip
-
-
-
-
Field Detail
-
defaultBufsize
public static int defaultBufsize
The default size of the temporary buffer used when copying from an input stream to an output stream.- See Also:
copyTo(OutputStream, int, byte[])
-
-
Method Detail
-
readLine
public java.lang.String readLine() throws java.io.IOExceptionReads the next line of text from the input stream.A line is terminated by "\r", "\n", "\r\n", or the end of the input stream. The line-terminating characters are discarded.
- Returns:
- The next line from the input stream, or
nullif the end of the input stream is reached and no bytes were found. - Throws:
java.io.IOException- if the underlying input stream throws an IOException while being read.
-
readLine
public java.lang.String readLine(int limit) throws java.io.IOExceptionReads the next line of text from the input stream, up to the limit specified.A line is terminated by "\r", "\n", "\r\n", the end of the input stream, or when the specified number of characters have been read. The line-terminating characters are discarded. It is not possible to distinguish, based on the result, between a line that was exactly
limitcharacters long and a line that was terminated becauselimitcharacters were read.- Returns:
- The next line from the input stream, or
nullif the end of the input stream is reached and no bytes were found. - Throws:
java.io.IOException- if the underlying input stream throws an IOException while being read.
-
readFully
public int readFully(byte[] buf) throws java.io.IOExceptionReadsbuf.lengthbytes from the input stream. This method reads repeatedly from the input stream until the specified number of bytes have been read or the end of the input stream is reached.The standard
InputStream.readmethod will generally return less than the specified number of bytes if the underlying input stream is "bursty", such as from a network source. Sometimes it is important to read the exact number of bytes desired.- Parameters:
buf- Buffer in which the data is stored. If buffer is of length 0, this method will return immediately.- Returns:
- The number of bytes read. This will be less than
buf.lengthif the end of the input stream was reached. - Throws:
java.io.IOException- if the underlying input stream throws an IOException while being read.
-
readFully
public int readFully(byte[] buf, int off, int len) throws java.io.IOExceptionReads the specified number of bytes from the input stream. This method reads repeatedly from the input stream until the specified number of bytes have been read or the end of the input stream is reached.The standard
InputStream.readmethod will generally return less than the specified number of bytes if the underlying input stream is "bursty", such as from a network source. Sometimes it is important to read the exact number of bytes desired.- Parameters:
buf- Buffer in which the data is stored.off- The starting offset into the buffer.len- The number of bytes to read.- Returns:
- The number of bytes read. This will be less than
lenif the end of the input stream was reached. - Throws:
java.io.IOException- if the underlying input stream throws an IOException while being read.
-
copyTo
public int copyTo(java.io.OutputStream out) throws java.io.IOExceptionCopies bytes from this input stream to the specified output stream until end of the input stream is reached.- Parameters:
out- The output stream to copy the data to.- Returns:
- The number of bytes copied to the output stream.
- Throws:
java.io.IOException- if the underlying input stream throws an IOException while being read or if the output stream throws an IOException while being written. It may not be possible to distinguish amongst the two conditions.
-
copyTo
public int copyTo(java.io.OutputStream out, int len) throws java.io.IOExceptionCopies bytes from this input stream to the specified output stream until the specified number of bytes are copied or the end of the input stream is reached.- Parameters:
out- The output stream to copy the data to.len- The number of bytes to copy, or < 0 to copy until the end of this stream.- Returns:
- The number of bytes copied to the output stream.
- Throws:
java.io.IOException- if the underlying input stream throws an IOException while being read or if the output stream throws an IOException while being written. It may not be possible to distinguish amongst the two conditions.
-
copyTo
public int copyTo(java.io.OutputStream out, int len, byte[] buf) throws java.io.IOExceptionCopies bytes from this input stream to the specified output stream until the specified number of bytes are copied or the end of the input stream is reached.- Parameters:
out- The output stream to copy the data to.len- The number of bytes to copy, or < 0 to copy until the end of this stream.buf- The buffer used to for holding the temporary results while copying data from this input stream to the output stream. May benullto allow this method copy in chunks of lengthdefaultBufsize.- Returns:
- The number of bytes read from the input stream, and copied to the output stream.
- Throws:
java.io.IOException- if the underlying input stream throws an IOException while being read.
-
-