- java.lang.Object
-
- com.github.mizosoft.methanol.WritableBodyPublisher
-
- All Implemented Interfaces:
java.io.Flushable,java.lang.AutoCloseable,java.net.http.HttpRequest.BodyPublisher,java.util.concurrent.Flow.Publisher<java.nio.ByteBuffer>
public final class WritableBodyPublisher extends java.lang.Object implements java.net.http.HttpRequest.BodyPublisher, java.io.Flushable, java.lang.AutoCloseableABodyPublisherthat allows streaming the body's content through anOutputStreamor aWritableByteChannel. It is recommended to useMoreBodyPublishers.ofOutputStream(com.github.mizosoft.methanol.function.ThrowingConsumer<? super java.io.OutputStream>, java.util.concurrent.Executor)orMoreBodyPublishers.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 theOutputStreamor theWritableByteChannel, 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
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description java.nio.channels.WritableByteChannelbyteChannel()Returns 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(java.lang.Throwable exception)Unless already closed, causes the subscribed (or yet to subscribe) client to fail with the given exception.longcontentLength()static 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()Returnstrueif this publisher is closed by eitherclose()orcloseExceptionally(java.lang.Throwable).java.io.OutputStreamoutputStream()Returns aOutputStreamfor writing this body's content.voidsubscribe(java.util.concurrent.Flow.Subscriber<? super java.nio.ByteBuffer> subscriber)
-
-
-
Method Detail
-
byteChannel
public java.nio.channels.WritableByteChannel byteChannel()
Returns aWritableByteChannelfor writing this body's content.
-
outputStream
public java.io.OutputStream outputStream()
Returns aOutputStreamfor writing this body's content.
-
closeExceptionally
public void closeExceptionally(java.lang.Throwable exception)
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 interfacejava.lang.AutoCloseable
-
isClosed
public boolean isClosed()
Returnstrueif this publisher is closed by eitherclose()orcloseExceptionally(java.lang.Throwable).
-
flush
public void flush()
Makes any buffered content available for consumption by downstream.- Specified by:
flushin interfacejava.io.Flushable- Throws:
java.lang.IllegalStateException- if closedjava.io.UncheckedIOException- if an interruption occurs while waiting
-
contentLength
public long contentLength()
- Specified by:
contentLengthin interfacejava.net.http.HttpRequest.BodyPublisher
-
subscribe
public void subscribe(java.util.concurrent.Flow.Subscriber<? super java.nio.ByteBuffer> subscriber)
- Specified by:
subscribein interfacejava.util.concurrent.Flow.Publisher<java.nio.ByteBuffer>
-
create
public static WritableBodyPublisher create()
Returns a newWritableBodyPublisher.
-
create
public static WritableBodyPublisher create(int bufferSize)
Returns a newWritableBodyPublisherwith the given buffer size (number of bytes to buffer before making them available to the HTTP client).
-
-