API Docs for:
Show:

Bitstream Class

Defined in: src/bitstream.js:6

Manages the packing/unpacking of values as a set number of bits

Constructor

Bitstream

(
  • buffer
)

Defined in src/bitstream.js:6

Parameters:

  • buffer Array

    an array of 7-bit integers representing the intial data for this Bitstream

Methods

_advance

(
  • bits
)
private

Advance the head by the specified number of bits and check for overread

Parameters:

  • bits Number

    The number of bits to advance the index by

_extend

(
  • bits
)
private

Extend the buffer size by the specified number of bits. Also advances the index

Parameters:

  • bits Number

    The number of bits to expand the buffer by

_getBits

(
  • offset
  • n
)
Number private

Return the value of the first n bits starting at offset

Parameters:

  • offset Number

    The zero-based bit offset to start at

  • n Number

    The number of bits to unpack the value from

Returns:

Number:

The unsigned value after unpacking

_serializeObject

(
  • obj
  • description
)
private

Calls all serialize methods in this object's prototype chain with description as its argument. This allows packing and unpacking classes which use prototypal inheritance.

Parameters:

  • obj NetObject

    The object to serialize

  • description Description

    The description to serialize through

_setBits

(
  • offset
  • n
  • value
)
private

Defined in src/bitstream.js:59

Set the n bits starting at offset to contain the unsigned integer value

Parameters:

  • offset Number

    The zero-based bit offset to start at

  • n Number

    The number of bits to pack the value in to

  • value Number

    The value to pack. Will be cast to an unsigned integer and truncated or padded to n bits

align

()

Defined in src/bitstream.js:45

Move the _index to the first position >= the current index which is the beginning of a cell. Used to burn off padding when processing data from "appendData" since it pads to the nearest multiple of CELLSIZE

appendChars

()

Append UTF-8 encoded data from a string to this Bitstream

appendData

()

Append data from an ArrayBuffer received over a binary websocket to this Bitstream

bitsLeft

() Number

Defined in src/bitstream.js:26

Returns:

Number:

the number of bits which can be read without causing an overread

empty

()

Defined in src/bitstream.js:35

Empty the buffer and reset the index

equals

(
  • other
)
Boolean

See if the contents and byte length of the buffer of this Bitstream and other are exactly equal

Parameters:

  • other Bitstream

    The bitstream to compare with

Returns:

Boolean:

true if the bistreams are effectively equal

fromArrayBuffer

()

Populate the data in this Bitstream from an ArrayBuffer received over a binary websocket

fromChars

() static

Create a bitstream from a valid UTF-8 string

pack

(
  • obj
)

Pack an object with a .serialize() method into this bitstream

Parameters:

  • obj NetObject

    The object to serialize

peekFloat

(
  • bits
)

Read a normalized float without consuming any bits

Parameters:

  • bits Number

    The number of bits to peek at

peekSInt

(
  • bits
)

Read a signed integer without consuming any bits

Parameters:

  • bits Number

    The number of bits to peek at

peekUInt

(
  • bits
)

Read an unsigned integer without consuming any bits

Parameters:

  • bits Number

    The number of bits to unpack

readFloat

(
  • bits
)

Read a float value, consuming the specified number of bits

Parameters:

  • bits Number

    The number of bits to unpack

readSInt

(
  • bits
)

Read a signed integer consuming the specified number of bits

Parameters:

  • bits Number

    The number of bits to unpack

readString

()

Read a zero-terminated string value

Returns:

the String read from the bitstream

readUInt

(
  • bits
)

Read an unsigned integer consuming the specified number of bits

Parameters:

  • bits Number

    The number of bits to unpack

toArrayBuffer

()

Convert the data in this Bitstream to a Uint8Array containing an ArrayBuffer suitable for transmitting this data over a binary websocket

Returns:

A Uint8Array containing the data in this Bitstream

toChars

()

Convert the data to a valid UTF-8 string

unpack

(
  • obj
)

Unpack an object with a .serialize() method from this bitstream

Parameters:

  • obj NetObject

    The object to deserialize to

writeFloat

(
  • value
  • bits
)

Write a normalized float in the range [0.0, 1.0] using the specified number of bits

Parameters:

  • value Number

    Value to write.

  • bits Number

    The number of bits to unpack

writeSInt

(
  • value
  • bits
)

write a signed integer using the specified number of bits

Parameters:

  • value Number

    Value to write. Will be truncated or padded to the specified number of bits

  • bits Number

    The number of bits to unpack

writeString

(
  • value
)

Write a zero-terminated string number of bits

Parameters:

  • value String

    Value to write.

writeUInt

(
  • value
  • bits
)

Write an unsigned integer using the specified number of bits

Parameters:

  • value Number

    Value to write.

  • bits Number

    The number of bits to unpack