java.lang.Object
com.github.mizosoft.methanol.WritableBodyPublisher
- All Implemented Interfaces:
Flushable
,AutoCloseable
,HttpRequest.BodyPublisher
,Flow.Publisher<ByteBuffer>
public final class WritableBodyPublisher
extends Object
implements HttpRequest.BodyPublisher, Flushable, AutoCloseable
A
BodyPublisher
that allows streaming the body's content through an OutputStream
or a WritableByteChannel
. It is recommended to use MoreBodyPublishers.ofOutputStream(com.github.mizosoft.methanol.function.ThrowingConsumer<? super java.io.OutputStream>, java.util.concurrent.Executor)
or MoreBodyPublishers.ofWritableByteChannel(com.github.mizosoft.methanol.function.ThrowingConsumer<? super java.nio.channels.WritableByteChannel>, java.util.concurrent.Executor)
instead of
directly using this class.
After writing is finished, the publisher must be closed to complete the request (either by
calling close()
or closing the OutputStream
or the WritableByteChannel
,
or using a try-with-resources construct). Additionally, closeExceptionally(Throwable)
can be used to fail the request in case an error is encountered while writing.
Note that (contentLength()
always returns -1
). If the content length is known
prior to writing, HttpRequest.BodyPublishers.fromPublisher(Publisher, long)
can be used to attach the
known length to this publisher.
-
Method Summary
Modifier and TypeMethodDescriptionReturns aWritableByteChannel
for writing this body's content.void
close()
Unless already closed, causes the subscribed (or yet to subscribe) client to be completed after the content written so far has been consumed.void
closeExceptionally
(Throwable exception) Unless already closed, causes the subscribed (or yet to subscribe) client to fail with the given exception.long
static WritableBodyPublisher
create()
Returns a newWritableBodyPublisher
.static WritableBodyPublisher
create
(int bufferSize) Returns a newWritableBodyPublisher
with the given buffer size.void
flush()
Makes any buffered content available for consumption by the downstream.boolean
isClosed()
Returnstrue
if this publisher is closed by eitherclose()
orcloseExceptionally(java.lang.Throwable)
.Returns aOutputStream
for writing this body's content.void
subscribe
(Flow.Subscriber<? super ByteBuffer> subscriber)
-
Method Details
-
byteChannel
Returns aWritableByteChannel
for writing this body's content. -
outputStream
Returns aOutputStream
for writing this body's content. -
closeExceptionally
Unless already closed, causes the subscribed (or yet to subscribe) client to fail with the given exception. -
close
public void close()Unless already closed, causes the subscribed (or yet to subscribe) client to be completed after the content written so far has been consumed.- Specified by:
close
in interfaceAutoCloseable
-
isClosed
public boolean isClosed()Returnstrue
if this publisher is closed by eitherclose()
orcloseExceptionally(java.lang.Throwable)
. -
flush
public void flush()Makes any buffered content available for consumption by the downstream.- Specified by:
flush
in interfaceFlushable
- Throws:
IllegalStateException
- if closed
-
contentLength
public long contentLength()- Specified by:
contentLength
in interfaceHttpRequest.BodyPublisher
-
subscribe
- Specified by:
subscribe
in interfaceFlow.Publisher<ByteBuffer>
-
create
Returns a newWritableBodyPublisher
. -
create
Returns a newWritableBodyPublisher
with the given buffer size.
-