Buffer

Buffer for writing and reading binary data.

Constructors

this
this(size_t chunk)

Creates a buffer specifying the chunk size. This should be the default constructor for re-used buffers and input buffers.

this
this(in T[] data...)

Creates a buffer from an array of data. The chunk size is set to the size of the array.

Destructor

A destructor is present on this object, but not explicitly documented in the source.

Members

Functions

canRead
bool canRead(size_t size)
bool canRead()

Indicates whether an array of length size or the given type can be read without any exceptions thrown.

opEquals
bool opEquals(in T[] data)
bool opEquals(in T[] data)

Check whether the data of the buffer is equals to the given array.

peek
T peek()

Peeks a value using the given endianness.

peek
B peek()

Peeks a varint.

peek
T peek()

Peeks some data using the system's endianness.

peekData
void[] peekData(size_t size)

Peeks some data (read it but without changing the buffer's index).

peekVar
T peekVar()

Peeks a varint.

read
T read()

Reads a value with the given endianness.

read
T read()

Reads a value using the system's endianness.

read
T read(size_t length)

Reads an array using the given endianness.

read
T read(size_t size)

Reads an array using the system's endianness.

read
B read()

Reads a varint.

readData
void[] readData(size_t size)

Reads the amount of data requested.

readVar
T readVar()

Reads a varint.

reset
void reset()

Resets the buffer setting the index and its length to 0.

write
void write(T value)

Writes data to buffer using the given endianness.

write
void write(T value)

Writes data to the buffer using the system's endianness.

write
void write(in T value)

Writes an array using the given endianness.

write
void write(in T value)

Writes an array using the system's endianness.

write
void write(B value)

Writes a varint.

write
void write(in T value, size_t index)

Writes data at the given index.

writeData
void writeData(in void[] data)

Writes data to the buffer and expands if it is not big enough.

writeVar
void writeVar(T value)

Writes a varint.

Properties

capacity
size_t capacity [@property getter]

Gets the size of the data allocated by the buffer.

data
T[] data [@property setter]

Sets new data and resets the index.

index
size_t index [@property getter]

Gets the current write/read index of the buffer. The index can be set to 0 using the reset method.

length
size_t length [@property getter]

Gets the length of the buffer.

Examples

1 t {
2 	
3 	import xbuffer.memory;
4 	
5 	// a buffer can be garbage collected
6 	Buffer gc = new Buffer(16);
7 	
8 	// or manually allocated
9 	// alloc is a function provided by the xbuffer.memory module
10 	Buffer b = alloc!Buffer(16);
11 	
12 	// the memory is realsed with free, which is called by the garbage
13 	// collector or by the `free` function in the `xbuffer.memory` module
14 	free(b);

Meta