Module methanol

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(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 Details

    • byteChannel

      public WritableByteChannel byteChannel()
      Returns a WritableByteChannel for writing this body's content.
    • outputStream

      public OutputStream outputStream()
      Returns a OutputStream for writing this body's content.
    • closeExceptionally

      public void closeExceptionally(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:
      close in interface AutoCloseable
    • isClosed

      public boolean isClosed()
      Returns true if this publisher is closed by either close() or closeExceptionally(java.lang.Throwable).
    • flush

      public void flush()
      Makes any buffered content available for consumption by the downstream.
      Specified by:
      flush in interface Flushable
      Throws:
      IllegalStateException - if closed
    • contentLength

      public long contentLength()
      Specified by:
      contentLength in interface HttpRequest.BodyPublisher
    • subscribe

      public void subscribe(Flow.Subscriber<? super ByteBuffer> subscriber)
      Specified by:
      subscribe in interface Flow.Publisher<ByteBuffer>
    • create

      public static WritableBodyPublisher create()
      Returns a new WritableBodyPublisher.
    • create

      public static WritableBodyPublisher create(int bufferSize)
      Returns a new WritableBodyPublisher with the given buffer size.