public class InputStreamContentProvider extends Object implements ContentProvider
ContentProvider
for an InputStream
.
The input stream is read once and therefore fully consumed.
Invocations to the iterator()
method after the first will return an "empty" iterator
because the stream has been consumed on the first invocation.
It is possible to specify, at the constructor, a buffer size used to read content from the
stream, by default 4096 bytes.
However, it is possible for subclasses to override onRead(byte[], int, int)
to copy
the content read from the stream to another location (for example a file), and be able to
support multiple invocations of iterator()
, returning the iterator provided by this
class on the first invocation, and an iterator on the bytes copied to the other location
for subsequent invocations.Constructor and Description |
---|
InputStreamContentProvider(InputStream stream) |
InputStreamContentProvider(InputStream stream,
int bufferSize) |
Modifier and Type | Method and Description |
---|---|
long |
getLength() |
Iterator<ByteBuffer> |
iterator() |
protected ByteBuffer |
onRead(byte[] buffer,
int offset,
int length)
Callback method invoked just after having read from the stream,
but before returning the iteration element (a
ByteBuffer
to the caller. |
public InputStreamContentProvider(InputStream stream)
public InputStreamContentProvider(InputStream stream, int bufferSize)
public long getLength()
getLength
in interface ContentProvider
protected ByteBuffer onRead(byte[] buffer, int offset, int length)
ByteBuffer
to the caller.
Subclasses may override this method to copy the content read from
the stream to another location (a file, or in memory if the content
is known to fit).buffer
- the byte array containing the bytes readoffset
- the offset from where bytes should be readlength
- the length of the bytes readByteBuffer
wrapping the byte arraypublic Iterator<ByteBuffer> iterator()
iterator
in interface Iterable<ByteBuffer>
Copyright © 1995-2012 Mort Bay Consulting. All Rights Reserved.