Skip to main content

BitWriter

A class in Alta.Serialization.Runtime, Alta.Serialization.

Writes values bit by bit into a buffer of 32 bit words, the writing half of the game's network and save serialization. Used by Stream on both sides.

Constructors​

MemberWhat it is
BitWriter()Creates a BitWriter with default values.

Fields​

8 private and internal fields
MemberWhat it is
private uint bitCountCapacity of the buffer in bits.
private int bitIndexBit position inside the word being built.
private uint bitsWrittenBacking field for BitsWritten.
private uint[] dataBacking field for Data.
private BitWriter.GetMoreData getMoreDataCallback that grows or swaps the buffer when it fills.
private static readonly Logger LoggerClass logger, used when the buffer overflows.
private ulong scratch64 bit working value that collects bits before they are stored as a word.
private uint wordIndexIndex of the next word to store.

Properties​

MemberWhat it is
uint BitsWritten { get; }Bits written since the last Initialize or Clear.
uint ByteCount { get; }Whole bytes written so far.
uint[] Data { get; }The word buffer being written.

Methods​

MemberWhat it is
void Align(int bitAlign = 8)Pads with zero bits up to the next multiple of bitAlign bits (8 by default).
uint AlignAndFlush()Aligns to a byte, writes out the partial last word and returns the byte count.
void BackTrack(uint bitCount)Moves the write position back by a number of bits.
void Clear(uint[] data)Resets the write position. A full clear, or passing a new buffer, also drops the old buffer and callback.
void Clear(bool isFull = false)Resets the write position. A full clear, or passing a new buffer, also drops the old buffer and callback.
void CopyStateFrom(BitWriter copyFrom)Copies another writer's position and scratch state, but not its buffer.
static uint HostToNetwork(uint value)Byte swap for word order. Leaves the value as is on little endian machines.
void Initialize(BitWriter.GetMoreData getMoreData = null)Starts writing into a buffer. The callback overload calls it at once for the first buffer and again when it fills, so it must not be null.
void Initialize(uint[] data)Starts writing into a buffer. The callback overload calls it at once for the first buffer and again when it fills, so it must not be null.
void WriteBits(uint value, int writeCount)Writes the low bits of a value. If the buffer fills with no callback it logs a fatal error and wraps to the start.
void WriteBytes(byte[] bytes, int byteCount)Writes a byte array, copying whole words directly when aligned.
1 private and internal method
MemberWhat it is
private void Flush()Writes the bits still in scratch to the buffer as a final word.

Used in​

Members of other types that take, return or hold a BitWriter: