# RakNet::BitStream Class Reference

Packets encoding and decoding facilities. More...

`#include <BitStream.h>`

List of all members.

## Public Member Functions

| BitStream ()
| BitStream (int initialBytesToAllocate)
| BitStream (const char *_data, unsigned int lengthInBytes, bool _copyData)
| ~BitStream ()
void | Reset (void)
void | Write (bool input)
void | Write (unsigned char input)
void | Write (char input)
void | Write (unsigned short input)
void | Write (short input)
void | Write (unsigned int input)
void | Write (int input)
void | Write (unsigned long input)
void | Write (long input)
void | Write (float input)
void | Write (double input)
void | Write (char *input, int numberOfBytes)
void | WriteCompressed (unsigned char input)
void | WriteCompressed (char input)
void | WriteCompressed (unsigned short input)
void | WriteCompressed (short input)
void | WriteCompressed (unsigned int input)
void | WriteCompressed (int input)
void | WriteCompressed (unsigned long input)
void | WriteCompressed (long input)
void | WriteCompressed (float input)
void | WriteCompressed (double input)
bool | Read (bool &output)
bool | Read (unsigned char &output)
bool | Read (char &output)
bool | Read (unsigned short &output)
bool | Read (short &output)
bool | Read (unsigned int &output)
bool | Read (int &output)
bool | Read (unsigned long &output)
bool | Read (long &output)
bool | Read (float &output)
bool | Read (double &output)
bool | Read (char *output, int numberOfBytes)
bool | ReadCompressed (unsigned char &output)
bool | ReadCompressed (char &output)
bool | ReadCompressed (unsigned short &output)
bool | ReadCompressed (short &output)
bool | ReadCompressed (unsigned int &output)
bool | ReadCompressed (int &output)
bool | ReadCompressed (unsigned long &output)
bool | ReadCompressed (long &output)
bool | ReadCompressed (float &output)
bool | ReadCompressed (double &output)
void | ResetReadPointer (void)
void | AssertStreamEmpty (void)
void | PrintBits (void) const
void | IgnoreBits (int numberOfBits)
void | SetWriteOffset (int offset)
int | GetNumberOfBitsUsed (void) const
int | GetNumberOfBytesUsed (void) const
int | GetReadOffset (void) const
int | GetNumberOfUnreadBits (void) const
int | CopyData (unsigned char **_data) const
void | SetData (const unsigned char *input, int numberOfBits)
unsigned char * | GetData (void) const
void | WriteBits (unsigned char *input, int numberOfBitsToWrite, bool rightAlignedBits=true)
void | WriteAlignedBytes (unsigned char *input, int numberOfBytesToWrite)
bool | ReadAlignedBytes (unsigned char *output, int numberOfBytesToRead)
void | AlignWriteToByteBoundary (void)
void | AlignReadToByteBoundary (void)
bool | ReadBits (unsigned char *output, int numberOfBitsToRead, bool alignBitsToRight=true)
void | Write0 (void)
void | Write1 (void)
bool | ReadBit (void)
void | AssertCopyData (void)
void | SetNumberOfBitsAllocated (unsigned int lengthInBits)

## Private Member Functions

void | WriteCompressed (unsigned char *input, int size, bool unsignedData)
bool | ReadCompressed (unsigned char *output, int size, bool unsignedData)
void | AddBitsAndReallocate (int numberOfBitsToWrite)

## Private Attributes

int | numberOfBitsUsed
int | numberOfBitsAllocated
int | readOffset
unsigned char * | data
bool | copyData

## Detailed Description

Packets encoding and decoding facilities.

Helper class to encode and decode packets.

Todo:
  Manage transparently endian problem within BitStream.

## Constructor & Destructor Documentation

BitStream::BitStream | ( |  | ) |

| Default Constructor

BitStream::BitStream | ( | int | initialBytesToAllocate | ) |

Preallocate some memory for the construction of the packet

Parameters:

  | initialBytesToAllocate | the amount of byte to pre-allocate.

BitStream::BitStream | ( | const char * | _data,
|  | unsigned int | lengthInBytes,
|  | bool | _copyData
| ) |

Initialize the BitStream object using data from the network. Set _copyData to true if you want to make an internal copy of the data you are passing. You can then Write and do all other operations Set it to false if you want to just use a pointer to the data you are passing, in order to save memory and speed. You should only then do read operations.

Parameters:

  | _data | An array of bytes.
  | lengthInBytes | Size of the _data.
  | _copyData | Does a copy of the input data.

BitStream::~BitStream | ( |  | ) |

| Destructor

## Member Function Documentation

void BitStream::AddBitsAndReallocate | ( | int | numberOfBitsToWrite | ) | `[private]`

| Reallocates (if necessary) in preparation of writing numberOfBitsToWrite

void BitStream::AlignReadToByteBoundary | ( | void |  | ) |

| Align the next write and/or read to a byte boundary. This can be used to 'waste' bits to byte align for efficiency reasons It can also be used to force coalesced bitstreams to start on byte boundaries so so WriteAlignedBits and ReadAlignedBits both calculate the same offset when aligning.

void BitStream::AlignWriteToByteBoundary | ( | void |  | ) |

| Align the next write and/or read to a byte boundary. This can be used to 'waste' bits to byte align for efficiency reasons It can also be used to force coalesced bitstreams to start on byte boundaries so so WriteAlignedBits and ReadAlignedBits both calculate the same offset when aligning.

void BitStream::AssertCopyData | ( | void |  | ) |

| If we used the constructor version with copy data off, this makes sure it is set to on and the data pointed to is copied.

void BitStream::AssertStreamEmpty | ( | void |  | ) |

| This is good to call when you are done with the stream to make sure you didn't leave any data left over void

int BitStream::CopyData | ( | unsigned char ** | _data | ) | const

Makes a copy of the internal data for you Data will point to the stream. Returns the length in bits of the stream. Partial bytes are left aligned

Parameters:

  | _data | the resulting byte copy of the internal state.

unsigned char * BitStream::GetData | ( | void |  | ) | const

Exposes the internal data. Partial bytes are left aligned.

Returns:
  A pointer to the internal state

int BitStream::GetNumberOfBitsUsed | ( | void |  | ) | const

| Returns the length in bits of the stream

int BitStream::GetNumberOfBytesUsed | ( | void |  | ) | const

| Returns the length in bytes of the stream

int BitStream::GetNumberOfUnreadBits | ( | void |  | ) | const

| Returns the number of bits left in the stream that haven't been read

int BitStream::GetReadOffset | ( | void |  | ) | const

| Returns the number of bits into the stream that we have read

void BitStream::IgnoreBits | ( | int | numberOfBits | ) |

Ignore data we don't intend to read

Parameters:

  | numberOfBits | The number of bits to ignore

void BitStream::PrintBits | ( | void |  | ) | const

| print to the standard output the state of the stream bit by bit

bool BitStream::Read | ( | char * | output,
|  | int | numberOfBytes
| ) |

Read an array or casted stream of byte. The array is raw data. There is no automatic conversion on big endian arch

Parameters:

  | output | The result byte array. It should be larger than numberOfBytes.
  | numberOfBytes | The number of byte to read

Returns:
  true on success false if there is some missing bytes.

bool BitStream::Read | ( | double & | output | ) |

Read the native types from the front of the buffer

Parameters:

  | output | The readed value.

Returns:
  true on success false otherwise. The result of a reading can only be wrong in the case we reach the end of the BitStream with some missing bits.

bool BitStream::Read | ( | float & | output | ) |

Read the native types from the front of the buffer

Parameters:

  | output | The readed value.

Returns:
  true on success false otherwise. The result of a reading can only be wrong in the case we reach the end of the BitStream with some missing bits.

bool BitStream::Read | ( | long & | output | ) |

Read the native types from the front of the buffer

Parameters:

  | output | The readed value.

Returns:
  true on success false otherwise. The result of a reading can only be wrong in the case we reach the end of the BitStream with some missing bits.

bool BitStream::Read | ( | unsigned long & | output | ) |

Read the native types from the front of the buffer

Parameters:

  | output | The readed value.

Returns:
  true on success false otherwise. The result of a reading can only be wrong in the case we reach the end of the BitStream with some missing bits.

bool BitStream::Read | ( | int & | output | ) |

Read the native types from the front of the buffer

Parameters:

  | output | The readed value.

Returns:
  true on success false otherwise. The result of a reading can only be wrong in the case we reach the end of the BitStream with some missing bits.

bool BitStream::Read | ( | unsigned int & | output | ) |

Read the native types from the front of the buffer

Parameters:

  | output | The readed value.

Returns:
  true on success false otherwise. The result of a reading can only be wrong in the case we reach the end of the BitStream with some missing bits.

bool BitStream::Read | ( | short & | output | ) |

Read the native types from the front of the buffer

Parameters:

  | output | The readed value.

Returns:
  true on success false otherwise. The result of a reading can only be wrong in the case we reach the end of the BitStream with some missing bits.

bool BitStream::Read | ( | unsigned short & | output | ) |

Read the native types from the front of the buffer

Parameters:

  | output | The readed value.

Returns:
  true on success false otherwise. The result of a reading can only be wrong in the case we reach the end of the BitStream with some missing bits.

bool BitStream::Read | ( | char & | output | ) |

Read the native types from the front of the buffer

Parameters:

  | output | The readed value.

Returns:
  true on success false otherwise. The result of a reading can only be wrong in the case we reach the end of the BitStream with some missing bits.

bool BitStream::Read | ( | unsigned char & | output | ) |

Read the native types from the front of the buffer

Parameters:

  | output | The readed value.

Returns:
  true on success false otherwise. The result of a reading can only be wrong in the case we reach the end of the BitStream with some missing bits.

bool BitStream::Read | ( | bool & | output | ) |

Read the native types from the front of the buffer

Parameters:

  | output | The readed value.

Returns:
  true on success false otherwise. The result of a reading can only be wrong in the case we reach the end of the BitStream with some missing bits.

bool BitStream::ReadAlignedBytes | ( | unsigned char * | output,
|  | int | numberOfBytesToRead
| ) |

Read bits, starting at the next aligned bits. Note that the modulus 8 starting offset of the sequence must be the same as was used with WriteBits. This will be a problem with packet coalescence unless you byte align the coalesced packets.

Parameters:

  | output | The byte array larger than numberOfBytesToRead
  | numberOfBytesToRead | The number of byte to read from the internal state

Returns:
  true if there is enough byte.

bool BitStream::ReadBit | ( | void |  | ) |

| --- Low level functions --- These are for when you want to deal with bits and don't care about type checking Reads 1 bit and returns true if that bit is 1 and false if it is 0

bool BitStream::ReadBits | ( | unsigned char * | output,
|  | int | numberOfBitsToRead,
|  | bool | alignBitsToRight = `true`
| ) |

Read numberOfBitsToRead bits to the output source alignBitsToRight should be set to true to convert internal bitstream data to userdata It should be false if you used WriteBits with rightAlignedBits false

Parameters:

  | output | The resulting bits array
  | numberOfBitsToRead | The number of bits to read
  | alignsBitsToRight | if true bits will be right aligned.

Returns:
  true if there is enough bits to read

bool BitStream::ReadCompressed | ( | unsigned char * | output,
|  | int | size,
|  | bool | unsignedData
| ) | `[private]`

| Assume the input source points to a compressed native type. Decompress and read it.

bool BitStream::ReadCompressed | ( | double & | output | ) |

Read the types you wrote with WriteCompressed

Parameters:

  | output | The readed value

Returns:
  true on success, false on not enough data to read

bool BitStream::ReadCompressed | ( | float & | output | ) |

Read the types you wrote with WriteCompressed

Parameters:

  | output | The readed value

Returns:
  true on success, false on not enough data to read

bool BitStream::ReadCompressed | ( | long & | output | ) |

Read the types you wrote with WriteCompressed

Parameters:

  | output | The readed value

Returns:
  true on success, false on not enough data to read

bool BitStream::ReadCompressed | ( | unsigned long & | output | ) |

Read the types you wrote with WriteCompressed

Parameters:

  | output | The readed value

Returns:
  true on success, false on not enough data to read

bool BitStream::ReadCompressed | ( | int & | output | ) |

Read the types you wrote with WriteCompressed

Parameters:

  | output | The readed value

Returns:
  true on success, false on not enough data to read

bool BitStream::ReadCompressed | ( | unsigned int & | output | ) |

Read the types you wrote with WriteCompressed

Parameters:

  | output | The readed value

Returns:
  true on success, false on not enough data to read

bool BitStream::ReadCompressed | ( | short & | output | ) |

Read the types you wrote with WriteCompressed

Parameters:

  | output | The readed value

Returns:
  true on success, false on not enough data to read

bool BitStream::ReadCompressed | ( | unsigned short & | output | ) |

Read the types you wrote with WriteCompressed

Parameters:

  | output | The readed value

Returns:
  true on success, false on not enough data to read

bool BitStream::ReadCompressed | ( | char & | output | ) |

Read the types you wrote with WriteCompressed

Parameters:

  | output | The readed value

Returns:
  true on success, false on not enough data to read

bool BitStream::ReadCompressed | ( | unsigned char & | output | ) |

Read the types you wrote with WriteCompressed

Parameters:

  | output | The readed value

Returns:
  true on success, false on not enough data to read

void BitStream::Reset | ( | void |  | ) |

| Reset the bitstream for reuse

void BitStream::ResetReadPointer | ( | void |  | ) |

| Sets the read pointer back to the beginning of your data.

void BitStream::SetData | ( | const unsigned char * | input,
|  | int | numberOfBits
| ) |

Set the stream to some initial data. For internal use Partial bytes are left aligned

Parameters:

  | input | The data
  | numberOfBits | the number of bits set in the data buffer

void BitStream::SetNumberOfBitsAllocated | ( | unsigned int | lengthInBits | ) |

| Use this if you pass a pointer copy to the constructor (_copyData==false) and want to overallocate to prevent reallocation

void BitStream::SetWriteOffset | ( | int | offset | ) |

Move the write pointer to a position on the array.

Parameters:

  | offset | the offset from the start of the array.

Attention:
  Dangerous if you don't know what you are doing!

void BitStream::Write | ( | char * | input,
|  | int | numberOfBytes
| ) |

Write an array or casted stream. It is supossed to be raw data. It is also not possible to deal with endian problem

Parameters:

  | input | a byte buffer
  | numberOfBytes | the size of the byte buffer

void BitStream::Write | ( | double | input | ) |

Write the native types to the end of the buffer without any compression mecanism.

Parameters:

  | input | The data

void BitStream::Write | ( | float | input | ) |

Write the native types to the end of the buffer without any compression mecanism.

Parameters:

  | input | The data

void BitStream::Write | ( | long | input | ) |

Write the native types to the end of the buffer without any compression mecanism.

Parameters:

  | input | The data

void BitStream::Write | ( | unsigned long | input | ) |

Write the native types to the end of the buffer without any compression mecanism.

Parameters:

  | input | The data

void BitStream::Write | ( | int | input | ) |

Write the native types to the end of the buffer without any compression mecanism.

Parameters:

  | input | The data

void BitStream::Write | ( | unsigned int | input | ) |

Write the native types to the end of the buffer without any compression mecanism.

Parameters:

  | input | The data

void BitStream::Write | ( | short | input | ) |

Write the native types to the end of the buffer without any compression mecanism.

Parameters:

  | input | The data

void BitStream::Write | ( | unsigned short | input | ) |

Write the native types to the end of the buffer without any compression mecanism.

Parameters:

  | input | The data

void BitStream::Write | ( | char | input | ) |

Write the native types to the end of the buffer without any compression mecanism.

Parameters:

  | input | The data

void BitStream::Write | ( | unsigned char | input | ) |

Write the native types to the end of the buffer without any compression mecanism.

Parameters:

  | input | The data

void BitStream::Write | ( | bool | input | ) |

Write the native types to the end of the buffer without any compression mecanism.

Parameters:

  | input | The data

void BitStream::Write0 | ( | void |  | ) |

| --- Low level functions --- These are for when you want to deal with bits and don't care about type checking Write a 0

void BitStream::Write1 | ( | void |  | ) |

| --- Low level functions --- These are for when you want to deal with bits and don't care about type checking Write a 1

void BitStream::WriteAlignedBytes | ( | unsigned char * | input,
|  | int | numberOfBytesToWrite
| ) |

Align the bitstream to the byte boundary and then write the specified number of bits. This is faster than WriteBits but wastes the bits to do the alignment and requires you to call ReadAlignedBits at the corresponding read position.

Parameters:

  | input | The data
  | numberOfBytesToWrite | The size of data.

void BitStream::WriteBits | ( | unsigned char * | input,
|  | int | numberOfBitsToWrite,
|  | bool | rightAlignedBits = `true`
| ) |

Write numberToWrite bits from the input source Right aligned data means in the case of a partial byte, the bits are aligned from the right (bit 0) rather than the left (as in the normal internal representation) You would set this to true when writing user data, and false when copying bitstream data, such as writing one bitstream to another

Parameters:

  | input | The data
  | numberOfBitsToWrite | The number of bits to write
  | rightAlignedBits | if true data will be right aligned

void BitStream::WriteCompressed | ( | unsigned char * | input,
|  | int | size,
|  | bool | unsignedData
| ) | `[private]`

| Assume the input source points to a native type, compress and write it.

void BitStream::WriteCompressed | ( | double | input | ) |

Write the native types with simple compression. Best used with negatives and positives close to 0

Parameters:

  | input | The data.

void BitStream::WriteCompressed | ( | float | input | ) |

Write the native types with simple compression. Best used with negatives and positives close to 0

Parameters:

  | input | The data.

void BitStream::WriteCompressed | ( | long | input | ) |

Write the native types with simple compression. Best used with negatives and positives close to 0

Parameters:

  | input | The data.

void BitStream::WriteCompressed | ( | unsigned long | input | ) |

Write the native types with simple compression. Best used with negatives and positives close to 0

Parameters:

  | input | The data.

void BitStream::WriteCompressed | ( | int | input | ) |

Write the native types with simple compression. Best used with negatives and positives close to 0

Parameters:

  | input | The data.

void BitStream::WriteCompressed | ( | unsigned int | input | ) |

Write the native types with simple compression. Best used with negatives and positives close to 0

Parameters:

  | input | The data.

void BitStream::WriteCompressed | ( | short | input | ) |

Write the native types with simple compression. Best used with negatives and positives close to 0

Parameters:

  | input | The data.

void BitStream::WriteCompressed | ( | unsigned short | input | ) |

Write the native types with simple compression. Best used with negatives and positives close to 0

Parameters:

  | input | The data.

void BitStream::WriteCompressed | ( | char | input | ) |

Write the native types with simple compression. Best used with negatives and positives close to 0

Parameters:

  | input | The data.

void BitStream::WriteCompressed | ( | unsigned char | input | ) |

Write the native types with simple compression. Best used with negatives and positives close to 0

Parameters:

  | input | The data.

## Member Data Documentation

bool RakNet::BitStream::copyData`[private]`

| true if the internal buffer is copy of the data passed to the constructor

unsigned char* RakNet::BitStream::data`[private]`

| array of byte storing the data

int RakNet::BitStream::numberOfBitsAllocated`[private]`

| Number of bits currently allocated

int RakNet::BitStream::numberOfBitsUsed`[private]`

| Number of bits currently used

int RakNet::BitStream::readOffset`[private]`

| Current readOffset

The documentation for this class was generated from the following files:

- Projects/raknet/include/raknet/BitStream.h
- Projects/raknet/Source/BitStream.cpp