Interface ByteStreamMessage

All Superinterfaces:
org.reactivestreams.Publisher<HttpData>, StreamMessage<HttpData>

@UnstableApi public interface ByteStreamMessage extends StreamMessage<HttpData>
A StreamMessage that publishes bytes with HttpData.
  • Method Details

    • of

      static ByteStreamMessage of(org.reactivestreams.Publisher<? extends HttpData> publisher)
      Creates a new ByteStreamMessage from the specified Publisher.
    • toInputStream

      default InputStream toInputStream()
      Adapts this ByteStreamMessage to InputStream.

      For example:

      
       ByteStreamMessage streamMessage = ByteStreamMessage.of(...);
       InputStream inputStream = streamMessage.toInputStream();
       byte[] expected = "foobarbaz".getBytes();
      
       ByteBuf result = Unpooled.buffer();
       int read;
       while ((read = inputStream.read()) != -1) {
           result.writeByte(read);
       }
      
       int readableBytes = result.readableBytes();
       byte[] actual = new byte[readableBytes];
       for (int i = 0; i < readableBytes; i++) {
           actual[i] = result.readByte();
       }
       assert Arrays.equals(actual, expected);
       assert inputStream.available() == 0;
       
    • writeTo

      default CompletableFuture<Void> writeTo(Path destination, OpenOption... options)
      Writes this ByteStreamMessage to the given Path with OpenOptions. If the OpenOption is not specified, defaults to StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING and StandardOpenOption.WRITE.

      Example:

      
       Path destination = Paths.get("foo.bin");
       ByteStreamMessage streamMessage = ByteStreamMessage.of(...);
       streamMessage.writeTo(destination);
       
      See Also:
    • range

      ByteStreamMessage range(long offset, long length)
      Sets the specified range of bytes to read from this ByteStreamMessage.
      
       StreamMessage<HttpData> source = StreamMessage.of(HttpData.ofUtf8("12345"),
                                                         HttpData.ofUtf8("67890"),
                                                         HttpData.ofUtf8("12345"));
      
       // Read 8 bytes from the index 4
       List<HttpData> collected = ByteStreamMessage.of(source).range(4, 8).collect().join();
       assert collected.equals(List.of(HttpData.ofUtf8("5"),
                                       HttpData.ofUtf8("67890"),
                                       HttpData.ofUtf8("12")));
       
      Throws:
      IllegalArgumentException - if the offset is negative or the length is non-positive.
      IllegalStateException - if this ByteStreamMessage is subscribed already.
    • collectBytes

      default CompletableFuture<byte[]> collectBytes()
      Collects the bytes published by this ByteStreamMessage. The returned CompletableFuture will be notified when the elements are fully consumed.
      
       StreamMessage<HttpData> source = StreamMessage.of(HttpData.wrap(new byte[] { 1, 2, 3 }),
                                                         HttpData.wrap(new byte[] { 4, 5, 6 }));
      
       byte[] collected = ByteStreamMessage.of(source).collectBytes().join();
       assert Arrays.equals(collected, new byte[] { 1, 2, 3, 4, 5, 6 });
       
    • collectBytes

      default CompletableFuture<byte[]> collectBytes(EventExecutor executor)
      Collects the bytes published by this ByteStreamMessage. The returned CompletableFuture will be notified when the elements are fully consumed.
      
       StreamMessage<HttpData> source = StreamMessage.of(HttpData.wrap(new byte[] { 1, 2, 3 }),
                                                         HttpData.wrap(new byte[] { 4, 5, 6 }));
      
       byte[] collected = ByteStreamMessage.of(source).collectBytes(executor).join();
       assert Arrays.equals(collected, new byte[] { 1, 2, 3, 4, 5, 6 });
       
      Parameters:
      executor - the executor to collect the HttpData.