Class InputStreamContentProvider
- All Implemented Interfaces:
Closeable
,AutoCloseable
,Iterable<ByteBuffer>
,ContentProvider
,Callback
,Invocable
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.
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.
It is possible to specify, at the constructor, a buffer size used to read content from the stream, by default 4096 bytes.
The InputStream
passed to the constructor is by default closed when is it fully
consumed (or when an exception is thrown while reading it), unless otherwise specified
to the constructor
.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprivate class
Iterating over anInputStream
is tricky, becauseInputStreamContentProvider.InputStreamContentProviderIterator.hasNext()
must return false if the stream reads -1.Nested classes/interfaces inherited from interface org.eclipse.jetty.util.Callback
Callback.Completable, Callback.Completing, Callback.InvocableCallback, Callback.Nested
Nested classes/interfaces inherited from interface org.eclipse.jetty.client.api.ContentProvider
ContentProvider.Typed
Nested classes/interfaces inherited from interface org.eclipse.jetty.util.thread.Invocable
Invocable.InvocationType
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final boolean
private final int
private static final Logger
private final InputStream
Fields inherited from interface org.eclipse.jetty.util.thread.Invocable
__nonBlocking
-
Constructor Summary
ConstructorsConstructorDescriptionInputStreamContentProvider
(InputStream stream, int bufferSize) InputStreamContentProvider
(InputStream stream, int bufferSize, boolean autoClose) -
Method Summary
Modifier and TypeMethodDescriptionvoid
close()
void
Callback invoked when the operation fails.long
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 (aByteBuffer
to the caller.protected void
onReadFailure
(Throwable failure) Callback method invoked when an exception is thrown while reading from the stream.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface org.eclipse.jetty.client.api.ContentProvider
isReproducible
Methods inherited from interface org.eclipse.jetty.util.thread.Invocable
getInvocationType
Methods inherited from interface java.lang.Iterable
forEach, spliterator
-
Field Details
-
LOG
-
iterator
-
stream
-
bufferSize
private final int bufferSize -
autoClose
private final boolean autoClose
-
-
Constructor Details
-
InputStreamContentProvider
-
InputStreamContentProvider
-
InputStreamContentProvider
-
-
Method Details
-
getLength
public long getLength()- Specified by:
getLength
in interfaceContentProvider
- Returns:
- the content length, if known, or -1 if the content length is unknown
-
onRead
Callback method invoked just after having read from the stream, but before returning the iteration element (aByteBuffer
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).
- Parameters:
buffer
- the byte array containing the bytes readoffset
- the offset from where bytes should be readlength
- the length of the bytes read- Returns:
- a
ByteBuffer
wrapping the byte array
-
onReadFailure
Callback method invoked when an exception is thrown while reading from the stream.- Parameters:
failure
- the exception thrown while reading from the stream.
-
iterator
- Specified by:
iterator
in interfaceIterable<ByteBuffer>
-
close
public void close()- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
-
failed
Description copied from interface:Callback
Callback invoked when the operation fails.
-