ByteStream

TODO: do something about this For reading large files which otherwise, would take too much memory

Aside from reading, it can also write to files. TODO make it ready For reading/writing sequentially to a ubyte[]

be careful using maxSize and grow, they're not tested

Constructors

this
this(bool grow, uinteger maxSize)

constructor

Destructor

~this
~this()
Undocumented in source.

Members

Functions

fromFile
bool fromFile(string fname)

Reads a stream from file. if successful, seek and maxSize are set to 0;

read
T read(bool incompleteRead, ubyte n)
T read(ubyte n)

Reads a data type T from current seek. **Do not use this for reading arrays**

readArray
T[] readArray(uinteger readCount, ubyte n)
T[] readArray(ubyte n)

Reads an array.

readAt
T readAt(uinteger at, bool incompleteRead)
T readAt(uinteger at)

Reads at a seek without changing seek. **Does not work for dynamic arrays**

readRaw
uinteger readRaw(ubyte[] buffer)

Reads a slice from the stream into buffer. Will read number of bytes so as to fill buffer

toFile
bool toFile(string fname)

Writes this stream to a file

write
bool write(T data, ubyte n)

Writes data at seek. **Do not use this for arrays**

writeArray
bool writeArray(T[] data, ubyte n)

Writes an array at seek.

writeAt
bool writeAt(uinteger at, T data, ubyte n)

Writes (overwriting existing) data at a seek, without changing seek.

writeRaw
uinteger writeRaw(T[] data)

Writes an array without its size.

Properties

grow
bool grow [@property getter]
bool grow [@property setter]

if the stream is allowed to grow in size while writing

maxSize
uinteger maxSize [@property getter]
uinteger maxSize [@property setter]

maximum size stream is allowed to grow to, 0 for no limit.

seek
uinteger seek [@property getter]
uinteger seek [@property setter]

Seek position (i.e: next read/write index)

size
uinteger size [@property getter]

Size, in bytes, of stream

size
uinteger size [@property setter]

Size, setter. if new size is >maxSize, size is set to maxSize

stream
ubyte[] stream [@property getter]
ubyte[] stream [@property setter]

The stream

Examples

ByteStream stream = new ByteStream();
ubyte[] buffer;
uint[] uintArray = [12_345, 123_456, 1_234_567, 12_345_678, 123_456_789];

stream.write(1024, 8); // 1024 as ulong
stream.seek = 0;
assert(stream.read!uint(8) == 1024);
assert(stream.seek == 8, stream.seek.to!string);
stream.writeRaw(uintArray);
stream.seek = 8;
buffer.length = 50;
stream.readRaw(buffer);
assert((cast(uint*)buffer.ptr)[0 .. 5] == uintArray);

stream.seek = 0;
stream.writeArray(uintArray, 6);
assert(stream.seek == (uintArray.length * 4) + 6);
stream.seek = 0;
uintArray = stream.readArray!(uint)(6);
assert (uintArray == [12_345, 123_456, 1_234_567, 12_345_678, 123_456_789], uintArray.to!string);

stream.seek = 0;
stream.writeRaw(uintArray);
stream.writeAt(0, cast(uint)50);
buffer.length = uintArray.length * uint.sizeof;
stream.seek = 0;
assert(stream.readRaw(buffer) == buffer.length);
uintArray = (cast(uint*)buffer.ptr)[0 .. uintArray.length];
assert(uintArray[0] == 50 && uintArray[1 .. $] == [123_456, 1_234_567, 12_345_678, 123_456_789]);
assert(stream.readAt!uint(4) == 123_456);

Meta