Class ArrayStream

An InputStream that encapsulates contents of an array

class ArrayStream
  : InputStream ;

Constructors

NameDescription
this () Constructor. Initializes stream as empty
this (data, size) Constructor. Initializes stream with an array of bytes. size delimits maximum read size
this (data) Constructor. Initializes stream with an array of bytes

Properties

NameTypeDescription
getPosition[get] ulongReturns current position

Methods

NameDescription
close () Closes the stream. Closed stream cannot be read or written any more
fillArray (array) Attempts to fill an array with raw data from stream. Returns true if the array was filled, false otherwise
position (pos) Attempts to set current position to pos. Throws SeekException on failure
readable () Returns true if there are any data to read. false means end of the stream.
readBE (value) Reads big-endian integer, converts to native-endian and stores in value
readBytes (buffer, count) Attempts to read count bytes from stream and stores them in memory pointing by buffer. Returns number of bytes actually read
readLE (value) Reads little-endian integer, converts to native-endian and stores in value
seek (amount) Relatively changes position. amount defines an offset from the current position (can be negative). Throws SeekException on failure
seekable () Returns true if it is legal to use Seekable functionality on this stream
setPosition (pos) Attempts to set current position to pos. Returns true on success, false on failure
size () Returns the size of a stream in bytes

Example

ubyte[] arr = [23, 13, 42, 71, 0, 1, 1, 2, 3, 5, 8];

auto stream = new ArrayStream(arr);
assert(stream.size() == arr.length);

ubyte[4] buf;
assert(stream.readBytes(buf.ptr, buf.length) == buf.length);
assert(buf == [23, 13, 42, 71]);
assert(stream.getPosition() == buf.length);

assert(stream.setPosition(6));
assert(stream.getPosition == 6);
assert(stream.readBytes(buf.ptr, buf.length) == buf.length);
assert(buf == [1, 2, 3, 5]);

assert(stream.readBytes(buf.ptr, buf.length) == 1);
assert(buf[0] == 8);
assert(!stream.readable);

stream.setPosition(1);
assert(stream.readable);
stream.seek(4);
assert(stream.readBytes(buf.ptr, buf.length) == buf.length);
assert(buf == [1, 1, 2, 3]);

assert(stream.setPosition(arr.length));
assert(!stream.setPosition(arr.length + 1));

stream.close();
assert(!stream.readable);