diff --git a/index.bs b/index.bs index cd8cf5f96..90f091636 100644 --- a/index.bs +++ b/index.bs @@ -287,12 +287,38 @@ copies. on streams, beyond the provided ones of piping and teeing. -
ReadableStreamBYOBReader
. It has almost the same methods as
- the ReadableStreamDefaultReader
, and can be
- read in the same manner as the ReadableStreamDefaultReader
.
+
+ const reader = readableStream.getBYOBReader();
+
+ let startingAB = new ArrayBuffer(1024);
+ readInto(startingAB, 0)
+ .then(buffer => console.log("The first 1024 bytes:", buffer))
+ .catch(e => console.error("Something went wrong!", e));
+
+ function readInto(buffer, offset) {
+ if (offset === buffer.byteLength) {
+ return buffer;
+ }
+
+ const view = new Uint8Array(buffer, offset, buffer.byteLength - offset);
+ reader.read(view).then(newView => {
+ return readInto(newView.buffer, offset + newView.byteLength);
+ });
+ }
+
+
+ An important thing to note here is that the final buffer
value is different from the
+ originalAB
, but it (and all intermediate buffers) share the same backing memory allocation. At each step,
+ the buffer is transferred to a new ArrayBuffer
object. The newView
is a
+ new Uint8Array
, with that ArrayBuffer
object as its buffer
property, the offset
+ that bytes were written to as its byteOffset
property, and the number of bytes that were written as its
+ byteLength
property.
ReadableStream