Interface ByteStreamMessage
- All Superinterfaces:
org.reactivestreams.Publisher<HttpData>
,StreamMessage<HttpData>
A
StreamMessage
that publishes bytes with HttpData
.-
Method Summary
Modifier and TypeMethodDescriptiondefault CompletableFuture<byte[]>
Collects the bytes published by thisByteStreamMessage
.default CompletableFuture<byte[]>
collectBytes
(EventExecutor executor) Collects the bytes published by thisByteStreamMessage
.static ByteStreamMessage
Creates a newByteStreamMessage
from the specifiedPublisher
.range
(long offset, long length) Sets the specified range of bytes to read from thisByteStreamMessage
.default InputStream
Adapts thisByteStreamMessage
toInputStream
.default CompletableFuture<Void>
writeTo
(Path destination, OpenOption... options) Methods inherited from interface com.linecorp.armeria.common.stream.StreamMessage
abort, abort, collect, collect, collect, decode, decode, defaultSubscriberExecutor, demand, filter, isComplete, isEmpty, isOpen, map, mapAsync, mapError, mapParallel, mapParallel, peek, peek, peekError, recoverAndResume, recoverAndResume, subscribe, subscribe, subscribe, subscribe, subscribe, toDuplicator, toDuplicator, toInputStream, toInputStream, whenComplete, writeTo
-
Method Details
-
of
Creates a newByteStreamMessage
from the specifiedPublisher
. -
toInputStream
Adapts thisByteStreamMessage
toInputStream
.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
Writes thisByteStreamMessage
to the givenPath
withOpenOption
s. If theOpenOption
is not specified, defaults toStandardOpenOption.CREATE
,StandardOpenOption.TRUNCATE_EXISTING
andStandardOpenOption.WRITE
.Example:
Path destination = Paths.get("foo.bin"); ByteStreamMessage streamMessage = ByteStreamMessage.of(...); streamMessage.writeTo(destination);
-
range
Sets the specified range of bytes to read from thisByteStreamMessage
.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 theoffset
is negative or thelength
is non-positive.IllegalStateException
- if thisByteStreamMessage
is subscribed already.
-
collectBytes
Collects the bytes published by thisByteStreamMessage
. The returnedCompletableFuture
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
Collects the bytes published by thisByteStreamMessage
. The returnedCompletableFuture
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 theHttpData
.
-