- Main Page
- Modules
- Namespaces
- Classes
- Files
- Directories
- Related Pages

- Alphabetical List
- Class List
- Class Hierarchy
- Class Members

# RakNet::BitStream Class Reference

`#include <BitStream.h>`

List of all members.

## Public Member Functions
| BitStream ()
| Default Constructor.
| BitStream (const unsigned int initialBytesToAllocate)
| BitStream (unsigned char *_data, const unsigned int lengthInBytes, bool _copyData)
| ~BitStream ()
| Destructor.
void | Reset (void)
| Resets the bitstream for reuse.
template<class templateType>
bool | Serialize (bool writeToBitstream, templateType &var)
template<class templateType>
bool | SerializeDelta (bool writeToBitstream, templateType &currentValue, templateType lastValue)
template<class templateType>
bool | SerializeDelta (bool writeToBitstream, templateType &currentValue)
template<class templateType>
bool | SerializeCompressed (bool writeToBitstream, templateType &var)
template<class templateType>
bool | SerializeCompressedDelta (bool writeToBitstream, templateType &currentValue, templateType lastValue)
template<class templateType>
bool | SerializeCompressedDelta (bool writeToBitstream, templateType &currentValue)
| Save as SerializeCompressedDelta(templateType &currentValue, templateType lastValue) when we have an unknown second parameter.
bool | Serialize (bool writeToBitstream, char *input, const unsigned int numberOfBytes)
template<class templateType>
bool | SerializeNormVector (bool writeToBitstream, templateType &x, templateType &y, templateType &z)
template<class templateType>
bool | SerializeVector (bool writeToBitstream, templateType &x, templateType &y, templateType &z)
template<class templateType>
bool | SerializeNormQuat (bool writeToBitstream, templateType &w, templateType &x, templateType &y, templateType &z)
template<class templateType>
bool | SerializeOrthMatrix (bool writeToBitstream, templateType &m00, templateType &m01, templateType &m02, templateType &m10, templateType &m11, templateType &m12, templateType &m20, templateType &m21, templateType &m22)
bool | SerializeBits (bool writeToBitstream, unsigned char *input, const BitSize_t numberOfBitsToSerialize, const bool rightAlignedBits=true)
template<class templateType>
void | Write (templateType var)
template<class templateType>
void | WritePtr (templateType *var)
template<class templateType>
void | WriteDelta (templateType currentValue, templateType lastValue)
template<class templateType>
void | WriteDelta (templateType currentValue)
template<class templateType>
void | WriteCompressed (templateType var)
template<class templateType>
void | WriteCompressedDelta (templateType currentValue, templateType lastValue)
template<class templateType>
void | WriteCompressedDelta (templateType currentValue)
| Save as WriteCompressedDelta(templateType currentValue, templateType lastValue) when we have an unknown second parameter.
template<class templateType>
bool | Read (templateType &var)
template<class templateType>
bool | ReadPtr (templateType *var)
template<class templateType>
bool | ReadDelta (templateType &var)
template<class templateType>
bool | ReadCompressed (templateType &var)
template<class templateType>
bool | ReadCompressedDelta (templateType &var)
bool | Read (BitStream *bitStream, BitSize_t numberOfBits)
void | Write (const char *input, const unsigned int numberOfBytes)
void | Write (BitStream *bitStream, BitSize_t numberOfBits)
template<class templateType>
void | WriteNormVector (templateType x, templateType y, templateType z)
template<class templateType>
void | WriteVector (templateType x, templateType y, templateType z)
template<class templateType>
void | WriteNormQuat (templateType w, templateType x, templateType y, templateType z)
template<class templateType>
void | WriteOrthMatrix (templateType m00, templateType m01, templateType m02, templateType m10, templateType m11, templateType m12, templateType m20, templateType m21, templateType m22)
bool | Read (char *output, const unsigned int numberOfBytes)
template<class templateType>
bool | ReadNormVector (templateType &x, templateType &y, templateType &z)
template<class templateType>
bool | ReadVector (templateType &x, templateType &y, templateType &z)
template<class templateType>
bool | ReadNormQuat (templateType &w, templateType &x, templateType &y, templateType &z)
template<class templateType>
bool | ReadOrthMatrix (templateType &m00, templateType &m01, templateType &m02, templateType &m10, templateType &m11, templateType &m12, templateType &m20, templateType &m21, templateType &m22)
void | ResetReadPointer (void)
| Sets the read pointer back to the beginning of your data.
void | ResetWritePointer (void)
| Sets the write pointer back to the beginning of your data.
void | AssertStreamEmpty (void)
void | PrintBits (void) const
| printf the bits in the stream. Great for debugging.
void | IgnoreBits (const BitSize_t numberOfBits)
void | IgnoreBytes (const unsigned int numberOfBytes)
void | SetWriteOffset (const BitSize_t offset)
BitSize_t | GetNumberOfBitsUsed (void) const
| Returns the length in bits of the stream.
BitSize_t | GetNumberOfBytesUsed (void) const
| Returns the length in bytes of the stream.
BitSize_t | GetReadOffset (void) const
| Returns the number of bits into the stream that we have read.
BitSize_t | GetNumberOfUnreadBits (void) const
| Returns the number of bits left in the stream that haven't been read.
BitSize_t | CopyData (unsigned char **_data) const
void | SetData (unsigned char *input)
unsigned char * | GetData (void) const
void | WriteBits (const unsigned char *input, BitSize_t numberOfBitsToWrite, const bool rightAlignedBits=true)
void | WriteAlignedBytes (const unsigned char *input, const unsigned int numberOfBytesToWrite)
void | WriteAlignedBytesSafe (const char *input, const unsigned int inputLength, const unsigned int maxBytesToWrite)
| Aligns the bitstream, writes inputLength, and writes input. Won't write beyond maxBytesToWrite.
bool | ReadAlignedBytes (unsigned char *output, const unsigned int numberOfBytesToRead)
bool | ReadAlignedBytesSafe (char *input, int &inputLength, const int maxBytesToRead)
bool | ReadAlignedBytesSafeAlloc (char **input, int &inputLength, const unsigned int maxBytesToRead)
void | AlignWriteToByteBoundary (void)
void | AlignReadToByteBoundary (void)
bool | ReadBits (unsigned char *output, BitSize_t numberOfBitsToRead, const bool alignBitsToRight=true)
void | Write0 (void)
| Write a 0.
void | Write1 (void)
| Write a 1.
bool | ReadBit (void)
| Reads 1 bit and returns true if that bit is 1 and false if it is 0.
void | AssertCopyData (void)
void | SetNumberOfBitsAllocated (const BitSize_t lengthInBits)
void | AddBitsAndReallocate (const BitSize_t numberOfBitsToWrite)
| Reallocates (if necessary) in preparation of writing numberOfBitsToWrite.
template<>
void | Write (bool var)
template<>
void | Write (SystemAddress var)
template<>
void | Write (NetworkID var)
template<>
void | Write (RakString var)
template<>
void | WriteDelta (SystemAddress currentValue, SystemAddress lastValue)
template<>
void | WriteDelta (NetworkID currentValue, NetworkID lastValue)
template<>
void | WriteDelta (bool currentValue, bool lastValue)
template<>
void | WriteCompressed (float var)
| For values between -1 and 1.
template<>
void | WriteCompressed (double var)
| For values between -1 and 1.
template<>
void | WriteCompressed (RakString var)
| Compress the string.
template<>
void | WriteCompressedDelta (bool currentValue, bool lastValue)
template<>
void | WriteCompressedDelta (bool currentValue)
| Save as WriteCompressedDelta(bool currentValue, templateType lastValue) when we have an unknown second bool.
template<>
bool | Read (bool &var)
template<>
bool | Read (SystemAddress &var)
template<>
bool | Read (NetworkID &var)
template<>
bool | Read (RakString &var)
template<>
bool | ReadDelta (bool &var)
template<>
bool | ReadCompressed (float &var)
| For values between -1 and 1.
template<>
bool | ReadCompressed (double &var)
| For values between -1 and 1.
template<>
bool | ReadCompressed (RakString &var)
| For strings.
template<>
bool | ReadCompressedDelta (bool &var)
## Static Public Member Functions
static bool | DoEndianSwap (void)
| ---- Member function template specialization declarations ----

## Detailed Description

This class allows you to write and read native types as a string of bits. BitStream is used extensively throughout RakNet and is designed to be used by users as well.

See also:
  BitStreamSample.txt

## Constructor & Destructor Documentation

BitStream::BitStream | ( | const unsigned int | initialBytesToAllocate | ) |

Create the bitstream, with some number of bytes to immediately allocate. There is no benefit to calling this, unless you know exactly how many bytes you need and it is greater than BITSTREAM_STACK_ALLOCATION_SIZE. In that case all it does is save you one or more realloc calls.

Parameters:

  `[in]` | initialBytesToAllocate | the number of bytes to pre-allocate.

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

Initialize the BitStream, immediately setting the data it contains to a predefined pointer. Set _copyData to true if you want to make an internal copy of the data you are passing. Set it to false to just save a pointer to the data. You shouldn't call Write functions with _copyData as false, as this will write to unallocated memory 99% of the time you will use this function to cast Packet::data to a bitstream for reading, in which case you should write something as follows:

     RakNet::BitStream bs(packet->data, packet->length, false);

Parameters:

  `[in]` | _data | An array of bytes.
  `[in]` | lengthInBytes | Size of the _data.
  `[in]` | _copyData | true or false to make a copy of _data or not.

## Member Function Documentation

template<class templateType>

bool BitStream::Serialize | ( | bool | writeToBitstream,
|  | templateType & | var |
| ) |  |  | `[inline]`

Bidirectional serialize/deserialize any integral type to/from a bitstream. Undefine __BITSTREAM_NATIVE_END if you need endian swapping.

Parameters:

  `[in]` | writeToBitstream | true to write from your data to this bitstream. False to read from this bitstream and write to your data
  `[in]` | var | The value to write

Returns:
  true if writeToBitstream is true. true if writeToBitstream is false and the read was successful. false if writeToBitstream is false and the read was not successful.

template<class templateType>

bool BitStream::SerializeDelta | ( | bool | writeToBitstream,
|  | templateType & | currentValue,
|  | templateType | lastValue |
| ) |  |  | `[inline]`

Bidirectional serialize/deserialize any integral type to/from a bitstream. If the current value is different from the last value the current value will be written. Otherwise, a single bit will be written

Parameters:

  `[in]` | writeToBitstream | true to write from your data to this bitstream. False to read from this bitstream and write to your data
  `[in]` | currentValue | The current value to write
  `[in]` | lastValue | The last value to compare against. Only used if writeToBitstream is true.

Returns:
  true if writeToBitstream is true. true if writeToBitstream is false and the read was successful. false if writeToBitstream is false and the read was not successful.

template<class templateType>

bool BitStream::SerializeDelta | ( | bool | writeToBitstream,
|  | templateType & | currentValue |
| ) |  |  | `[inline]`

Bidirectional version of SerializeDelta when you don't know what the last value is, or there is no last value.

Parameters:

  `[in]` | writeToBitstream | true to write from your data to this bitstream. False to read from this bitstream and write to your data
  `[in]` | currentValue | The current value to write

Returns:
  true if writeToBitstream is true. true if writeToBitstream is false and the read was successful. false if writeToBitstream is false and the read was not successful.

template<class templateType>

bool BitStream::SerializeCompressed | ( | bool | writeToBitstream,
|  | templateType & | var |
| ) |  |  | `[inline]`

Bidirectional serialize/deserialize any integral type to/from a bitstream. Undefine __BITSTREAM_NATIVE_END if you need endian swapping. If you are not using __BITSTREAM_NATIVE_END the opposite is true for types larger than 1 byte For floating point, this is lossy, using 2 bytes for a float and 4 for a double. The range must be between -1 and +1. For non-floating point, this is lossless, but only has benefit if you use less than half the range of the type

Parameters:

  `[in]` | writeToBitstream | true to write from your data to this bitstream. False to read from this bitstream and write to your data
  `[in]` | var | The value to write

Returns:
  true if writeToBitstream is true. true if writeToBitstream is false and the read was successful. false if writeToBitstream is false and the read was not successful.

template<class templateType>

bool BitStream::SerializeCompressedDelta | ( | bool | writeToBitstream,
|  | templateType & | currentValue,
|  | templateType | lastValue |
| ) |  |  | `[inline]`

Bidirectional serialize/deserialize any integral type to/from a bitstream. If the current value is different from the last value the current value will be written. Otherwise, a single bit will be written For floating point, this is lossy, using 2 bytes for a float and 4 for a double. The range must be between -1 and +1. For non-floating point, this is lossless, but only has benefit if you use less than half the range of the type If you are not using __BITSTREAM_NATIVE_END the opposite is true for types larger than 1 byte

Parameters:

  `[in]` | writeToBitstream | true to write from your data to this bitstream. False to read from this bitstream and write to your data
  `[in]` | currentValue | The current value to write
  `[in]` | lastValue | The last value to compare against. Only used if writeToBitstream is true.

Returns:
  true if writeToBitstream is true. true if writeToBitstream is false and the read was successful. false if writeToBitstream is false and the read was not successful.

bool BitStream::Serialize | ( | bool | writeToBitstream,
|  | char * | input,
|  | const unsigned int | numberOfBytes |
| ) |  |  | `[inline]`

Bidirectional serialize/deserialize an array or casted stream or raw data. This does NOT do endian swapping.

Parameters:

  `[in]` | writeToBitstream | true to write from your data to this bitstream. False to read from this bitstream and write to your data
  `[in]` | input | a byte buffer
  `[in]` | numberOfBytes | the size of input in bytes

Returns:
  true if writeToBitstream is true. true if writeToBitstream is false and the read was successful. false if writeToBitstream is false and the read was not successful.

template<class templateType>

bool BitStream::SerializeNormVector | ( | bool | writeToBitstream,
|  | templateType & | x,
|  | templateType & | y,
|  | templateType & | z |
| ) |  |  | `[inline]`

Bidirectional serialize/deserialize a normalized 3D vector, using (at most) 4 bytes + 3 bits instead of 12-24 bytes. Will further compress y or z axis aligned vectors. Accurate to 1/32767.5.

Parameters:

  `[in]` | writeToBitstream | true to write from your data to this bitstream. False to read from this bitstream and write to your data
  `[in]` | x | x
  `[in]` | y | y
  `[in]` | z | z

Returns:
  true if writeToBitstream is true. true if writeToBitstream is false and the read was successful. false if writeToBitstream is false and the read was not successful.

template<class templateType>

bool BitStream::SerializeVector | ( | bool | writeToBitstream,
|  | templateType & | x,
|  | templateType & | y,
|  | templateType & | z |
| ) |  |  | `[inline]`

Bidirectional serialize/deserialize a vector, using 10 bytes instead of 12. Loses accuracy to about 3/10ths and only saves 2 bytes, so only use if accuracy is not important.

Parameters:

  `[in]` | writeToBitstream | true to write from your data to this bitstream. False to read from this bitstream and write to your data
  `[in]` | x | x
  `[in]` | y | y
  `[in]` | z | z

Returns:
  true if writeToBitstream is true. true if writeToBitstream is false and the read was successful. false if writeToBitstream is false and the read was not successful.

template<class templateType>

bool BitStream::SerializeNormQuat | ( | bool | writeToBitstream,
|  | templateType & | w,
|  | templateType & | x,
|  | templateType & | y,
|  | templateType & | z |
| ) |  |  | `[inline]`

Bidirectional serialize/deserialize a normalized quaternion in 6 bytes + 4 bits instead of 16 bytes. Slightly lossy.

Parameters:

  `[in]` | writeToBitstream | true to write from your data to this bitstream. False to read from this bitstream and write to your data
  `[in]` | w | w
  `[in]` | x | x
  `[in]` | y | y
  `[in]` | z | z

Returns:
  true if writeToBitstream is true. true if writeToBitstream is false and the read was successful. false if writeToBitstream is false and the read was not successful.

template<class templateType>

bool BitStream::SerializeOrthMatrix | ( | bool | writeToBitstream,
|  | templateType & | m00,
|  | templateType & | m01,
|  | templateType & | m02,
|  | templateType & | m10,
|  | templateType & | m11,
|  | templateType & | m12,
|  | templateType & | m20,
|  | templateType & | m21,
|  | templateType & | m22 |
| ) |  |  | `[inline]`

Bidirectional serialize/deserialize an orthogonal matrix by creating a quaternion, and writing 3 components of the quaternion in 2 bytes each for 6 bytes instead of 36 Lossy, although the result is renormalized

bool BitStream::SerializeBits | ( | bool | writeToBitstream,
|  | unsigned char * | input,
|  | const BitSize_t | numberOfBitsToSerialize,
|  | const bool | rightAlignedBits = `true` |
| ) |  |  | `[inline]`

Bidirectional serialize/deserialize numberToSerialize bits to/from the input. 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:

  `[in]` | writeToBitstream | true to write from your data to this bitstream. False to read from this bitstream and write to your data
  `[in]` | input | The data
  `[in]` | numberOfBitsToSerialize | The number of bits to write
  `[in]` | rightAlignedBits | if true data will be right aligned

Returns:
  true if writeToBitstream is true. true if writeToBitstream is false and the read was successful. false if writeToBitstream is false and the read was not successful.

template<class templateType>

void BitStream::Write | ( | templateType | var | ) | `[inline]`

Write any integral type to a bitstream. Undefine __BITSTREAM_NATIVE_END if you need endian swapping.

Parameters:

  `[in]` | var | The value to write

template<class templateType>

void BitStream::WritePtr | ( | templateType * | var | ) | `[inline]`

Write the dereferenced pointer to any integral type to a bitstream. Undefine __BITSTREAM_NATIVE_END if you need endian swapping.

Parameters:

  `[in]` | var | The value to write

template<class templateType>

void BitStream::WriteDelta | ( | templateType | currentValue,
|  | templateType | lastValue |
| ) |  |  | `[inline]`

Write any integral type to a bitstream. If the current value is different from the last value the current value will be written. Otherwise, a single bit will be written

Parameters:

  `[in]` | currentValue | The current value to write
  `[in]` | lastValue | The last value to compare against

template<class templateType>

void BitStream::WriteDelta | ( | templateType | currentValue | ) | `[inline]`

WriteDelta when you don't know what the last value is, or there is no last value.

Parameters:

  `[in]` | currentValue | The current value to write

template<class templateType>

void BitStream::WriteCompressed | ( | templateType | var | ) | `[inline]`

Write any integral type to a bitstream. Undefine __BITSTREAM_NATIVE_END if you need endian swapping. If you are not using __BITSTREAM_NATIVE_END the opposite is true for types larger than 1 byte For floating point, this is lossy, using 2 bytes for a float and 4 for a double. The range must be between -1 and +1. For non-floating point, this is lossless, but only has benefit if you use less than half the range of the type

Parameters:

  `[in]` | var | The value to write

template<class templateType>

void BitStream::WriteCompressedDelta | ( | templateType | currentValue,
|  | templateType | lastValue |
| ) |  |  | `[inline]`

Write any integral type to a bitstream. If the current value is different from the last value the current value will be written. Otherwise, a single bit will be written For floating point, this is lossy, using 2 bytes for a float and 4 for a double. The range must be between -1 and +1. For non-floating point, this is lossless, but only has benefit if you use less than half the range of the type If you are not using __BITSTREAM_NATIVE_END the opposite is true for types larger than 1 byte

Parameters:

  `[in]` | currentValue | The current value to write
  `[in]` | lastValue | The last value to compare against

template<class templateType>

bool BitStream::Read | ( | templateType & | var | ) | `[inline]`

Read any integral type from a bitstream. Define __BITSTREAM_NATIVE_END if you need endian swapping.

Parameters:

  `[in]` | var | The value to read

template<class templateType>

bool BitStream::ReadPtr | ( | templateType * | var | ) | `[inline]`

Read into a pointer to any integral type from a bitstream. Define __BITSTREAM_NATIVE_END if you need endian swapping.

Parameters:

  `[in]` | var | The value to read

template<class templateType>

bool BitStream::ReadDelta | ( | templateType & | var | ) | `[inline]`

Read any integral type from a bitstream. If the written value differed from the value compared against in the write function, var will be updated. Otherwise it will retain the current value. ReadDelta is only valid from a previous call to WriteDelta

Parameters:

  `[in]` | var | The value to read

template<class templateType>

bool BitStream::ReadCompressed | ( | templateType & | var | ) | `[inline]`

Read any integral type from a bitstream. Undefine __BITSTREAM_NATIVE_END if you need endian swapping. For floating point, this is lossy, using 2 bytes for a float and 4 for a double. The range must be between -1 and +1. For non-floating point, this is lossless, but only has benefit if you use less than half the range of the type If you are not using __BITSTREAM_NATIVE_END the opposite is true for types larger than 1 byte

Parameters:

  `[in]` | var | The value to read

template<class templateType>

bool BitStream::ReadCompressedDelta | ( | templateType & | var | ) | `[inline]`

Read any integral type from a bitstream. If the written value differed from the value compared against in the write function, var will be updated. Otherwise it will retain the current value. the current value will be updated. For floating point, this is lossy, using 2 bytes for a float and 4 for a double. The range must be between -1 and +1. For non-floating point, this is lossless, but only has benefit if you use less than half the range of the type If you are not using __BITSTREAM_NATIVE_END the opposite is true for types larger than 1 byte ReadCompressedDelta is only valid from a previous call to WriteDelta

Parameters:

  `[in]` | var | The value to read

bool BitStream::Read | ( | BitStream * | bitStream,
|  | BitSize_t | numberOfBits |
| ) |  |  |

Read one bitstream to another

Parameters:

  `[in]` | numberOfBits | bits to read
  | bitStream | the bitstream to read into from

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

Write an array or casted stream or raw data. This does NOT do endian swapping.

Parameters:

  `[in]` | input | a byte buffer
  `[in]` | numberOfBytes | the size of input in bytes

void BitStream::Write | ( | BitStream * | bitStream,
|  | BitSize_t | numberOfBits |
| ) |  |  |

Write one bitstream to another

Parameters:

  `[in]` | numberOfBits | bits to write
  | bitStream | the bitstream to copy from

template<class templateType>

void BitStream::WriteNormVector | ( | templateType | x,
|  | templateType | y,
|  | templateType | z |
| ) |  |  |

Read a normalized 3D vector, using (at most) 4 bytes + 3 bits instead of 12-24 bytes. Will further compress y or z axis aligned vectors. Accurate to 1/32767.5.

Parameters:

  `[in]` | x | x
  `[in]` | y | y
  `[in]` | z | z

template<class templateType>

void BitStream::WriteVector | ( | templateType | x,
|  | templateType | y,
|  | templateType | z |
| ) |  |  |

Write a vector, using 10 bytes instead of 12. Loses accuracy to about 3/10ths and only saves 2 bytes, so only use if accuracy is not important.

Parameters:

  `[in]` | x | x
  `[in]` | y | y
  `[in]` | z | z

template<class templateType>

void BitStream::WriteNormQuat | ( | templateType | w,
|  | templateType | x,
|  | templateType | y,
|  | templateType | z |
| ) |  |  |

Write a normalized quaternion in 6 bytes + 4 bits instead of 16 bytes. Slightly lossy.

Parameters:

  `[in]` | w | w
  `[in]` | x | x
  `[in]` | y | y
  `[in]` | z | z

template<class templateType>

void BitStream::WriteOrthMatrix | ( | templateType | m00,
|  | templateType | m01,
|  | templateType | m02,
|  | templateType | m10,
|  | templateType | m11,
|  | templateType | m12,
|  | templateType | m20,
|  | templateType | m21,
|  | templateType | m22 |
| ) |  |  |

Write an orthogonal matrix by creating a quaternion, and writing 3 components of the quaternion in 2 bytes each for 6 bytes instead of 36 Lossy, although the result is renormalized

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

Read an array or casted stream of byte. The array is raw data. There is no automatic endian conversion with this function

Parameters:

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

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

template<class templateType>

bool BitStream::ReadNormVector | ( | templateType & | x,
|  | templateType & | y,
|  | templateType & | z |
| ) |  |  |

Read a normalized 3D vector, using (at most) 4 bytes + 3 bits instead of 12-24 bytes. Will further compress y or z axis aligned vectors. Accurate to 1/32767.5.

Parameters:

  `[in]` | x | x
  `[in]` | y | y
  `[in]` | z | z

template<class templateType>

bool BitStream::ReadVector | ( | templateType & | x,
|  | templateType & | y,
|  | templateType & | z |
| ) |  |  |

Read 3 floats or doubles, using 10 bytes, where those float or doubles comprise a vector Loses accuracy to about 3/10ths and only saves 2 bytes, so only use if accuracy is not important.

Parameters:

  `[in]` | x | x
  `[in]` | y | y
  `[in]` | z | z

template<class templateType>

bool BitStream::ReadNormQuat | ( | templateType & | w,
|  | templateType & | x,
|  | templateType & | y,
|  | templateType & | z |
| ) |  |  |

Read a normalized quaternion in 6 bytes + 4 bits instead of 16 bytes.

Parameters:

  `[in]` | w | w
  `[in]` | x | x
  `[in]` | y | y
  `[in]` | z | z

template<class templateType>

bool BitStream::ReadOrthMatrix | ( | templateType & | m00,
|  | templateType & | m01,
|  | templateType & | m02,
|  | templateType & | m10,
|  | templateType & | m11,
|  | templateType & | m12,
|  | templateType & | m20,
|  | templateType & | m21,
|  | templateType & | m22 |
| ) |  |  |

Read an orthogonal matrix from a quaternion, reading 3 components of the quaternion in 2 bytes each and extrapolatig the 4th. for 6 bytes instead of 36 Lossy, although the result is renormalized

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

void BitStream::IgnoreBits | ( | const BitSize_t | numberOfBits | ) |

Ignore data we don't intend to read

Parameters:

  `[in]` | numberOfBits | The number of bits to ignore

void BitStream::IgnoreBytes | ( | const unsigned int | numberOfBytes | ) |

Ignore data we don't intend to read

Parameters:

  `[in]` | numberOfBits | The number of bytes to ignore

void BitStream::SetWriteOffset | ( | const BitSize_t | offset | ) |

Move the write pointer to a position on the array.

Parameters:

  `[in]` | offset | the offset from the start of the array.

Attention:
  Dangerous if you don't know what you are doing! For efficiency reasons you can only write mid-stream if your data is byte aligned.

BitSize_t 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:

  `[out]` | _data | The allocated copy of GetData()

void BitStream::SetData | ( | unsigned char * | input | ) |

Set the stream to some initial data.

unsigned char* RakNet::BitStream::GetData | ( | void |  | ) | const`[inline]`

Gets the data that BitStream is writing to / reading from Partial bytes are left aligned.

Returns:
  A pointer to the internal state

void BitStream::WriteBits | ( | const unsigned char * | input,
|  | BitSize_t | numberOfBitsToWrite,
|  | const 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:

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

void BitStream::WriteAlignedBytes | ( | const unsigned char * | input,
|  | const unsigned 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:

  `[in]` | input | The data
  `[in]` | numberOfBytesToWrite | The size of input.

void BitStream::WriteAlignedBytesSafe | ( | const char * | input,
|  | const unsigned int | inputLength,
|  | const unsigned int | maxBytesToWrite |
| ) |  |  |

Aligns the bitstream, writes inputLength, and writes input. Won't write beyond maxBytesToWrite.

Aligns the bitstream, writes inputLength, and writes input. Won't write beyond maxBytesToWrite

Parameters:

  `[in]` | input | The data
  `[in]` | inputLength | The size of input.
  `[in]` | maxBytesToWrite | Max bytes to write

bool BitStream::ReadAlignedBytes | ( | unsigned char * | output,
|  | const unsigned 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:

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

Returns:
  true if there is enough byte.

bool BitStream::ReadAlignedBytesSafe | ( | char * | input,
|  | int & | inputLength,
|  | const int | maxBytesToRead |
| ) |  |  |

Reads what was written by WriteAlignedBytesSafe

Parameters:

  `[in]` | input | The data
  `[in]` | maxBytesToRead | Maximum number of bytes to read

bool BitStream::ReadAlignedBytesSafeAlloc | ( | char ** | input,
|  | int & | inputLength,
|  | const unsigned int | maxBytesToRead |
| ) |  |  |

Same as ReadAlignedBytesSafe() but allocates the memory for you using new, rather than assuming it is safe to write to

Parameters:

  `[in]` | input | input will be deleted if it is not a pointer to 0

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::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.

bool BitStream::ReadBits | ( | unsigned char * | output,
|  | BitSize_t | numberOfBitsToRead,
|  | const 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:

  `[in]` | output | The resulting bits array
  `[in]` | numberOfBitsToRead | The number of bits to read
  `[in]` | alignBitsToRight | if true bits will be right aligned.

Returns:
  true if there is enough bits to read

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::SetNumberOfBitsAllocated | ( | const BitSize_t | lengthInBits | ) |

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

void BitStream::AddBitsAndReallocate | ( | const BitSize_t | numberOfBitsToWrite | ) |

Reallocates (if necessary) in preparation of writing numberOfBitsToWrite.

Cap to 1 meg buffer to save on huge allocations

template<>

void RakNet::BitStream::Write | ( | bool | var | ) | `[inline]`

Write a bool to a bitstream

Parameters:

  `[in]` | var | The value to write

template<>

void RakNet::BitStream::Write | ( | SystemAddress | var | ) | `[inline]`

Write a systemAddress to a bitstream

Parameters:

  `[in]` | var | The value to write

template<>

void RakNet::BitStream::Write | ( | NetworkID | var | ) | `[inline]`

Write an networkID to a bitstream

Parameters:

  `[in]` | var | The value to write

template<>

void RakNet::BitStream::Write | ( | RakString | var | ) | `[inline]`

Write a string to a bitstream

Parameters:

  `[in]` | var | The value to write

template<>

void RakNet::BitStream::WriteDelta | ( | SystemAddress | currentValue,
|  | SystemAddress | lastValue |
| ) |  |  | `[inline]`

Write a systemAddress. If the current value is different from the last value the current value will be written. Otherwise, a single bit will be written

Parameters:

  `[in]` | currentValue | The current value to write
  `[in]` | lastValue | The last value to compare against

template<>

void RakNet::BitStream::WriteDelta | ( | NetworkID | currentValue,
|  | NetworkID | lastValue |
| ) |  |  | `[inline]`

Write a systemAddress. If the current value is different from the last value the current value will be written. Otherwise, a single bit will be written

Parameters:

  `[in]` | currentValue | The current value to write
  `[in]` | lastValue | The last value to compare against

template<>

void RakNet::BitStream::WriteDelta | ( | bool | currentValue,
|  | bool | lastValue |
| ) |  |  | `[inline]`

Write a bool delta. Same thing as just calling Write

Parameters:

  `[in]` | currentValue | The current value to write
  `[in]` | lastValue | The last value to compare against

template<>

void RakNet::BitStream::WriteCompressedDelta | ( | bool | currentValue,
|  | bool | lastValue |
| ) |  |  | `[inline]`

Write a bool delta. Same thing as just calling Write

Parameters:

  `[in]` | currentValue | The current value to write
  `[in]` | lastValue | The last value to compare against

template<>

bool RakNet::BitStream::Read | ( | bool & | var | ) | `[inline]`

Read a bool from a bitstream

Parameters:

  `[in]` | var | The value to read

template<>

bool RakNet::BitStream::Read | ( | SystemAddress & | var | ) | `[inline]`

Read a systemAddress from a bitstream

Parameters:

  `[in]` | var | The value to read

template<>

bool RakNet::BitStream::Read | ( | NetworkID & | var | ) | `[inline]`

Read an networkID from a bitstream

Parameters:

  `[in]` | var | The value to read

template<>

bool RakNet::BitStream::Read | ( | RakString & | var | ) | `[inline]`

Read an networkID from a bitstream

Parameters:

  `[in]` | var | The value to read

template<>

bool RakNet::BitStream::ReadDelta | ( | bool & | var | ) | `[inline]`

Read a bool from a bitstream

Parameters:

  `[in]` | var | The value to read

template<>

bool RakNet::BitStream::ReadCompressedDelta | ( | bool & | var | ) | `[inline]`

Read a bool from a bitstream

Parameters:

  `[in]` | var | The value to read

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

- C:/RakNet/Source/BitStream.h
- C:/RakNet/Source/BitStream.cpp