Base module

BitWriter

Class for reading/writing binary streams.

BitWriter

#include <icy/buffer.h>

Subclassed by: DynamicBitWriter

Class for reading/writing binary streams.

Note that when using the constructor with the Buffer reference as an argument, the writer will dynamically expand the given buffer when writing passed the buffer capacity. All other cases will throw a std::out_of_range error when writing past the buffer capacity.

Public Methods

ReturnNameDescription
BitWriterConstructs a [BitWriter](#bitwriter) over a raw byte array with a fixed capacity.
BitWriterConstructs a [BitWriter](#bitwriter) backed by a Buffer. Writes are bounded by the buffer's capacity; use [DynamicBitWriter](icy-DynamicBitWriter.html#dynamicbitwriter) for auto-resize.
BitWriterConstructs a [BitWriter](#bitwriter) over a [MutableBuffer](icy-MutableBuffer.html#mutablebuffer-6) with a fixed capacity.
voidput virtualAppend bytes to the buffer. Throws a std::out_of_range exception if reading past the limit.
voidputAppends the contents of a string. Throws std::out_of_range if capacity is exceeded.
voidputU8Appends an unsigned 8-bit integer. Throws std::out_of_range if capacity is exceeded.
voidputU16Appends an unsigned 16-bit integer with byte-order conversion. Throws std::out_of_range if capacity is exceeded.
voidputU24Appends the low 24 bits of a 32-bit integer with byte-order conversion. Throws std::out_of_range if capacity is exceeded.
voidputU32Appends an unsigned 32-bit integer with byte-order conversion. Throws std::out_of_range if capacity is exceeded.
voidputU64Appends an unsigned 64-bit integer with byte-order conversion. Throws std::out_of_range if capacity is exceeded.
boolupdate virtualUpdate a byte range. Throws a std::out_of_range exception if reading past the limit.
boolupdateOverwrites a previously written string at the given absolute position.
boolupdateU8Overwrites a uint8_t at the given absolute position.
boolupdateU16Overwrites a uint16_t at the given absolute position, with byte-order conversion.
boolupdateU24Overwrites 3 bytes (low 24 bits of val) at the given absolute position, with byte-order conversion.
boolupdateU32Overwrites a uint32_t at the given absolute position, with byte-order conversion.
boolupdateU64Overwrites a uint64_t at the given absolute position, with byte-order conversion.
voidseekSet position pointer to absolute position. Throws a std::out_of_range exception if the value exceeds the limit.
voidskipSet position pointer to relative position. Throws a std::out_of_range exception if the value exceeds the limit.
size_tlimit constReturns the write limit.
size_tposition const inlineReturns the current write position.
size_tavailable constReturns the number of elements between the current write position and the limit.
char *begin inlineReturns a pointer to the start of the write buffer.
char *current inlineReturns a pointer to the current write position.
const char *begin const inlineReturns a const pointer to the start of the write buffer.
const char *current const inlineReturns a const pointer to the current write position.
ByteOrderorder const inlineReturns the byte order used for multi-byte integer writes.
std::stringtoStringReturns all bytes written so far as a std::string.

BitWriter

BitWriter(char * bytes, size_t size, ByteOrder order)

Constructs a [BitWriter](#bitwriter) over a raw byte array with a fixed capacity.

Parameters

  • bytes Pointer to the writable buffer. Must remain valid for the writer's lifetime.

  • size Capacity of the buffer in bytes.

  • order Byte order used for multi-byte integer writes.


BitWriter

BitWriter(Buffer & buf, ByteOrder order)

Constructs a [BitWriter](#bitwriter) backed by a Buffer. Writes are bounded by the buffer's capacity; use [DynamicBitWriter](icy-DynamicBitWriter.html#dynamicbitwriter) for auto-resize.

Parameters

  • buf Source buffer. Must remain valid for the writer's lifetime.

  • order Byte order used for multi-byte integer writes.


BitWriter

BitWriter(MutableBuffer & pod, ByteOrder order)

Constructs a [BitWriter](#bitwriter) over a [MutableBuffer](icy-MutableBuffer.html#mutablebuffer-6) with a fixed capacity.

Parameters

  • pod Source mutable buffer. Must remain valid for the writer's lifetime.

  • order Byte order used for multi-byte integer writes.


put

virtual

virtual void put(const char * val, size_t len)

Append bytes to the buffer. Throws a std::out_of_range exception if reading past the limit.


put

void put(const std::string & val)

Appends the contents of a string. Throws std::out_of_range if capacity is exceeded.

Parameters

  • val String whose bytes are appended.

putU8

void putU8(uint8_t val)

Appends an unsigned 8-bit integer. Throws std::out_of_range if capacity is exceeded.

Parameters

  • val Value to write.

putU16

void putU16(uint16_t val)

Appends an unsigned 16-bit integer with byte-order conversion. Throws std::out_of_range if capacity is exceeded.

Parameters

  • val Value to write.

putU24

void putU24(uint32_t val)

Appends the low 24 bits of a 32-bit integer with byte-order conversion. Throws std::out_of_range if capacity is exceeded.

Parameters

  • val Value to write (only the lower 3 bytes are written).

putU32

void putU32(uint32_t val)

Appends an unsigned 32-bit integer with byte-order conversion. Throws std::out_of_range if capacity is exceeded.

Parameters

  • val Value to write.

putU64

void putU64(uint64_t val)

Appends an unsigned 64-bit integer with byte-order conversion. Throws std::out_of_range if capacity is exceeded.

Parameters

  • val Value to write.

update

virtual

virtual bool update(const char * val, size_t len, size_t pos)

Update a byte range. Throws a std::out_of_range exception if reading past the limit.


update

bool update(const std::string & val, size_t pos)

Overwrites a previously written string at the given absolute position.

Parameters

  • val String to write.

  • pos Absolute byte offset to overwrite at.

Returns

True on success, false if the range exceeds available space.


updateU8

bool updateU8(uint8_t val, size_t pos)

Overwrites a uint8_t at the given absolute position.

Parameters

  • val Value to write.

  • pos Absolute byte offset to overwrite at.

Returns

True on success, false if the range exceeds available space.


updateU16

bool updateU16(uint16_t val, size_t pos)

Overwrites a uint16_t at the given absolute position, with byte-order conversion.

Parameters

  • val Value to write.

  • pos Absolute byte offset to overwrite at.

Returns

True on success, false if the range exceeds available space.


updateU24

bool updateU24(uint32_t val, size_t pos)

Overwrites 3 bytes (low 24 bits of val) at the given absolute position, with byte-order conversion.

Parameters

  • val Value to write.

  • pos Absolute byte offset to overwrite at.

Returns

True on success, false if the range exceeds available space.


updateU32

bool updateU32(uint32_t val, size_t pos)

Overwrites a uint32_t at the given absolute position, with byte-order conversion.

Parameters

  • val Value to write.

  • pos Absolute byte offset to overwrite at.

Returns

True on success, false if the range exceeds available space.


updateU64

bool updateU64(uint64_t val, size_t pos)

Overwrites a uint64_t at the given absolute position, with byte-order conversion.

Parameters

  • val Value to write.

  • pos Absolute byte offset to overwrite at.

Returns

True on success, false if the range exceeds available space.


seek

void seek(size_t val)

Set position pointer to absolute position. Throws a std::out_of_range exception if the value exceeds the limit.


skip

void skip(size_t size)

Set position pointer to relative position. Throws a std::out_of_range exception if the value exceeds the limit.


limit

const

size_t limit() const

Returns the write limit.


position

const inline

inline size_t position() const

Returns the current write position.


available

const

size_t available() const

Returns the number of elements between the current write position and the limit.


begin

inline

inline char * begin()

Returns a pointer to the start of the write buffer.


current

inline

inline char * current()

Returns a pointer to the current write position.


begin

const inline

inline const char * begin() const

Returns a const pointer to the start of the write buffer.


current

const inline

inline const char * current() const

Returns a const pointer to the current write position.


order

const inline

inline ByteOrder order() const

Returns the byte order used for multi-byte integer writes.


toString

std::string toString()

Returns all bytes written so far as a std::string.

Returns

String containing bytes from the start of the buffer up to the current position.

Protected Attributes

ReturnNameDescription
size_t_position
size_t_limit
ByteOrder_order
char *_bytes

_position

size_t _position

_limit

size_t _limit

_order

ByteOrder _order

_bytes

char * _bytes

Protected Methods

ReturnNameDescription
voidinit virtual

init

virtual

virtual void init(char * bytes, size_t size, ByteOrder order)