public class PartInputStream extends FilterInputStream
PartInputStream
filters a ServletInputStream
,
providing access to a single MIME part contained with in which ends with
the boundary specified. It uses buffering to provide maximum performance.
Note the readLine
method of ServletInputStream
has the annoying habit of adding a \r\n to the end of the last line. Since
we want a byte-for-byte transfer, we have to cut those chars. This means
that we must always maintain at least 2 characters in our buffer to allow
us to trim when necessary.
Modifier and Type | Field and Description |
---|---|
private String |
boundary
boundary which "ends" the stream
|
private byte[] |
buf
our buffer
|
private int |
count
number of bytes we've read into the buffer
|
private boolean |
eof
flag that indicates if we have encountered the boundary
|
private int |
pos
current position in the buffer
|
in
Constructor and Description |
---|
PartInputStream(ServletInputStream in,
String boundary)
Creates a
PartInputStream which stops at the specified
boundary from a ServletInputStream |
Modifier and Type | Method and Description |
---|---|
int |
available()
Returns the number of bytes that can be read from this input stream
without blocking.
|
void |
close()
Closes this input stream and releases any system resources
associated with the stream.
|
private void |
fill()
Fill up our buffer from the underlying input stream, and check for the
boundary that signifies end-of-file.
|
int |
read()
See the general contract of the
read
method of InputStream . |
int |
read(byte[] b)
See the general contract of the
read
method of InputStream . |
int |
read(byte[] b,
int off,
int len)
See the general contract of the
read
method of InputStream . |
mark, markSupported, reset, skip
private byte[] buf
private int count
private int pos
private boolean eof
PartInputStream(ServletInputStream in, String boundary) throws IOException
PartInputStream
which stops at the specified
boundary from a ServletInputStream.
in
- a servlet input stream.boundary
- the MIME boundary to stop at.IOException
private void fill() throws IOException
IOException
- if an I/O error occurs.public int read() throws IOException
read
method of InputStream
.
Returns -1
(end of file) when the MIME
boundary of this part is encountered.
read
in class FilterInputStream
-1
if the end of the
stream is reached.IOException
- if an I/O error occurs.public int read(byte[] b) throws IOException
read
method of InputStream
.
Returns -1
(end of file) when the MIME
boundary of this part is encountered.
read
in class FilterInputStream
b
- the buffer into which the data is read.-1
if there is no more data because the end
of the stream has been reached.IOException
- if an I/O error occurs.public int read(byte[] b, int off, int len) throws IOException
read
method of InputStream
.
Returns -1
(end of file) when the MIME
boundary of this part is encountered.
read
in class FilterInputStream
b
- the buffer into which the data is read.off
- the start offset of the data.len
- the maximum number of bytes read.-1
if there is no more data because the end
of the stream has been reached.IOException
- if an I/O error occurs.public int available() throws IOException
InputStream
idiom
to deal with buffering gracefully, and is not same as the length of the
part arriving in this stream.available
in class FilterInputStream
IOException
- if an I/O error occurs.public void close() throws IOException
This method will read any unread data in the MIME part so that the next
part starts an an expected place in the parent InputStream
.
Note that if the client code forgets to call this method on error,
MultipartParser
will call it automatically if you call
readNextPart()
.
close
in interface Closeable
close
in interface AutoCloseable
close
in class FilterInputStream
IOException
- if an I/O error occurs.WebARTS Library Licensed Under the GNU - General Public License. Other Libraries licensed under their respective Open Source Licenses