BinaryReader
BinaryReader (defined in binary.reader) provides an interface for reading binary data.
Reading methods (such as read) come in two variants: either data is returned or written to
specified parameter (taken by ref).
Example
auto source = [...]; // binary data
auto reader = binaryReader(source); // Construct BinaryReader with binaryReader helper
byte b;
writeln(reader.read!ulong()); // return ulong
writer.read(b); // reads by reference into ch
writeln(writer.position); // ulong.sizeof + byte.sizeof = 9
// change byte order
writer.byteOrder = ByteOrder.BigEndian;
writer.read!ushort();
writer.clear(); // resets source
Methods
-
T read(T)()/void read(T)(ref T target)Reads value of type
Tand returns it or writes it intotarget. -
T[] readArray(T)(size_t num)/void readArray(T)(ref T[] arr, size_t length)Reads
numelements of typeTfrom source. -
T readString(T)()/void readString(T)(ref T str)Reads null terminated string from source.
-
T read(T...)(ref T values)Variable length arguments version of
read.byte b; int i; reader.read(b, i); -
ubyte[] readBytes(size_t bytes)Reads
bytesnumber of raw data from source. -
ubyte[] readUntil(ubyte term, bool skip = true)Reads raw input until
termis found, skippingtermifskipistrue. -
void skipBytes(size_t bytes)Skips
bytesbytes from input.Note
Argument is intentionally made
size_trather thanptrdiff_tbecause negative offsets are not supported. -
void skipTo(size_t offset)Moves current position to
offset(absolute offset). Ifoffsetis smaller than currentposition, current position is not changed. -
void clear()Clears internal buffer and resets position to 0.
Fields
-
ubyte[] bufferBuffer with built binary data.
Properties
-
size_t position()Gets or sets current position in buffer.
-
bool empty()Determines whenever source range is empty.
BinaryWriter
BinaryWriter (defined in binary.writer) provides an interface for writing binary data. It is an valid OutputRange.
Example
BinaryWriter writer = BinaryWriter(ByteOrder.LittleEndian);
writer.write(15UL);
writer.write!byte('a');
writeln(writer.position); // ulong.sizeof + byte.sizeof = 9
writeln(writer.buffer); // [15, 0, 0, 0, 0, 0, 0, 0, 97]
writer.byteOrder = ByteOrder.BigEndian;
writer.write!ushort(5);
writeln(writer.buffer); // [15, 0, 0, 0, 0, 0, 0, 0, 97, 0, 5]
writer.clear(); // buffer is now empty
Methods
-
void write(T)(T data)/void put(T)(T data)Writes
dataof specified type to internalbuffer.Since 0.3
If
datais an array, data is written as is, without null terminator nor length indicator. To write null terminated arrays (such as strings) usewriteStringinstead. To write arrays with their length usewriteArrayinstead.Before 0.3
If
Tis a string, null terminator is appended, for any other array typedatais written as is. -
void writeArray(T)(T[] arr)Added in 0.3
Writes
arrtobuffer. First it writes array length down-casted touintwhich makes it possible to read on 32 bit architectures. Then it writes array elements. If array length is bigger thanuint.maxyou’ll be informed by subtleException. -
void writeString(T)(T[] str)Added in 0.3
Works like
writefor arrays with appending null terminator at the end. -
void fill(size_t times, ubyte value = 0)Writes
valuetimestimes. -
void padFill(size_t offset, ubyte value = 0)Writes
valueuntil position isoffset. Allows to fill buffer with values up to specified offset. If offset is lower than current value, nothing happens. -
void clear()Clears internal buffer and resets position to 0.
Fields
-
ubyte[] bufferBuffer with built binary data.
Properties
-
size_t position()Gets or sets current position in buffer.