Class WritableBodyPublisher
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(ThrowingConsumer, Executor) or MoreBodyPublishers.ofWritableByteChannel(ThrowingConsumer, 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 aWritableByteChannelfor writing this body's content.voidclose()Unless already closed, causes the subscribed (or yet to subscribe) client to be completed after the content written so far has been consumed.voidcloseExceptionally(Throwable exception) Unless already closed, causes the subscribed (or yet to subscribe) client to fail with the given exception.longstatic WritableBodyPublishercreate()Returns a newWritableBodyPublisher.static WritableBodyPublishercreate(int bufferSize) Returns a newWritableBodyPublisherwith the given buffer size (number of bytes to buffer before making them available to the HTTP client).voidflush()Makes any buffered content available for consumption by downstream.booleanisClosed()Returns aOutputStreamfor writing this body's content.voidsubscribe(Flow.Subscriber<? super ByteBuffer> subscriber)
-
Method Details
-
byteChannel
Returns aWritableByteChannelfor writing this body's content. -
outputStream
Returns aOutputStreamfor 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:
closein interfaceAutoCloseable
-
isClosed
public boolean isClosed() -
flush
public void flush()Makes any buffered content available for consumption by downstream.- Specified by:
flushin interfaceFlushable- Throws:
IllegalStateException- if closedUncheckedIOException- if an interruption occurs while waiting
-
contentLength
public long contentLength()- Specified by:
contentLengthin interfaceHttpRequest.BodyPublisher
-
subscribe
- Specified by:
subscribein interfaceFlow.Publisher<ByteBuffer>
-
create
Returns a newWritableBodyPublisher. -
create
Returns a newWritableBodyPublisherwith the given buffer size (number of bytes to buffer before making them available to the HTTP client).
-