io.vertx.core.streams

## Interface WriteStream<T>

- All Superinterfaces:
    StreamBase
  All Known Subinterfaces:
    AmqpSender, AsyncFile, BodyStream<T>, GrpcClientRequest<Req,Resp>, GrpcServerResponse<Req,Resp>, GrpcWriteStream<T>, HttpClientRequest, HttpServerResponse, KafkaProducer<K,V>, KafkaWriteStream<K,V>, NetSocket, ReactiveWriteStream<T>, ServerWebSocket, SockJSSocket, WebSocket, WebSocketBase
  All Known Implementing Classes:
    GrpcWriteStream

      public interface WriteStream<T>
      extends StreamBase

  Represents a stream of data that can be written to.

  Any class that implements this interface can be used by a `Pipe` to pipe data from a `ReadStream` to it.

  Author:
    Tim Fox

- - ### Method Summary

    All Methods Instance Methods Abstract Methods Default Methods
    Modifier and Type | Method and Description
    --- | ---
    `WriteStream<T>` | `drainHandler(Handler<Void> handler)` Set a drain handler on the stream.
    `default Future<Void>` | `end()` Ends the stream.
    `void` | `end(Handler<AsyncResult<Void>> handler)` Same as `end()` but with an `handler` called when the operation completes
    `default Future<Void>` | `end(T data)` Same as `end()` but writes some data to the stream before ending.
    `default void` | `end(T data, Handler<AsyncResult<Void>> handler)` Same as `#end(T)` but with an `handler` called when the operation completes
    `WriteStream<T>` | `exceptionHandler(Handler<Throwable> handler)` Set an exception handler on the write stream.
    `WriteStream<T>` | `setWriteQueueMaxSize(int maxSize)` Set the maximum size of the write queue to `maxSize`.
    `Future<Void>` | `write(T data)` Write some data to the stream.
    `void` | `write(T data, Handler<AsyncResult<Void>> handler)` Same as `#write(T)` but with an `handler` called when the operation completes
    `boolean` | `writeQueueFull()` This will return `true` if there are more bytes in the write queue than the value set using `setWriteQueueMaxSize(int)`

- - ### Method Detail

    - #### exceptionHandler

          WriteStream<T> exceptionHandler(Handler<Throwable> handler)

      Set an exception handler on the write stream.
      Specified by:
        `exceptionHandler` in interface `StreamBase`
      Parameters:
        `handler` - the exception handler
      Returns:
        a reference to this, so the API can be used fluently

    - #### write

          Future<Void> write(T data)

      Write some data to the stream.

      The data is usually put on an internal write queue, and the write actually happens asynchronously. To avoid running out of memory by putting too much on the write queue, check the `writeQueueFull()` method before writing. This is done automatically if using a `Pipe`.

      When the `data` is moved from the queue to the actual medium, the returned `Future` will be completed with the write result, e.g the future is succeeded when a server HTTP response buffer is written to the socket and failed if the remote client has closed the socket while the data was still pending for write.

      Parameters:
        `data` - the data to write
      Returns:
        a future completed with the write result

    - #### write

          void write(T data,
                     Handler<AsyncResult<Void>> handler)

      Same as `#write(T)` but with an `handler` called when the operation completes

    - #### end

          default Future<Void> end()

      Ends the stream.

      Once the stream has ended, it cannot be used any more.

      Returns:
        a future completed with the result

    - #### end

          void end(Handler<AsyncResult<Void>> handler)

      Same as `end()` but with an `handler` called when the operation completes

    - #### end

          default Future<Void> end(T data)

      Same as `end()` but writes some data to the stream before ending.
      Parameters:
        `data` - the data to write
      Returns:
        a future completed with the result

    - #### end

          default void end(T data,
                           Handler<AsyncResult<Void>> handler)

      Same as `#end(T)` but with an `handler` called when the operation completes

    - #### setWriteQueueMaxSize

          WriteStream<T> setWriteQueueMaxSize(int maxSize)

      Set the maximum size of the write queue to `maxSize`. You will still be able to write to the stream even if there is more than `maxSize` items in the write queue. This is used as an indicator by classes such as `Pipe` to provide flow control.

      The value is defined by the implementation of the stream, e.g in bytes for a `NetSocket`, etc...
      Parameters:
        `maxSize` - the max size of the write stream
      Returns:
        a reference to this, so the API can be used fluently

    - #### writeQueueFull

          boolean writeQueueFull()

      This will return `true` if there are more bytes in the write queue than the value set using `setWriteQueueMaxSize(int)`
      Returns:
        `true` if write queue is full

    - #### drainHandler

          WriteStream<T> drainHandler(Handler<Void> handler)

      Set a drain handler on the stream. If the write queue is full, then the handler will be called when the write queue is ready to accept buffers again. See `Pipe` for an example of this being used.

      The stream implementation defines when the drain handler, for example it could be when the queue size has been reduced to `maxSize / 2`.

      Parameters:
        `handler` - the handler
      Returns:
        a reference to this, so the API can be used fluently

Copyright © 2024 Eclipse. All rights reserved.