Class BigEndianAscendingWordSerializer

  • All Implemented Interfaces:
    IWordSerializer

    public class BigEndianAscendingWordSerializer
    extends Object
    implements IWordSerializer
    A serializer that writes a sequence of fixed bit-width 'words' to a byte array. Bitwise OR is used to write words into bytes, so a low bit in a word is also a low bit in a byte. However, a high byte in a word is written at a lower index in the array than a low byte in a word. The first word is written at the lowest array index. Each serializer is one time use and returns its backing byte array.

    This encoding was chosen so that when reading bytes as octets in the typical first-octet-is-the-high-nibble fashion, an octet-to-binary conversion would yield a high-to-low, left-to-right view of the "short words".

    Example:

    Say short words are 5 bits wide. Our word sequence is the values [31, 1, 5]. In big-endian binary format, the values are [0b11111, 0b00001, 0b00101]. We use 15 of 16 bits in two bytes and pad the last (lowest) bit of the last byte with a zero: [0b11111000, 0b01001010] = [0xF8, 0x4A] .

    • Constructor Detail

      • BigEndianAscendingWordSerializer

        public BigEndianAscendingWordSerializer​(int wordLength,
                                                int wordCnt,
                                                int bytePadding)
        Parameters:
        wordLength - the length in bits of the words to be serialized. Must be greater than or equal to 1 and less than or equal to 64.
        wordCnt - the number of words to be serialized. Must be greater than or equal to zero.
        bytePadding - the number of leading bytes that should pad the serialized words. Must be greater than or equal to zero.
    • Method Detail

      • writeWord

        public void writeWord​(long word)
        Description copied from interface: IWordSerializer
        Writes the word to the backing array.
        Specified by:
        writeWord in interface IWordSerializer
        Parameters:
        word - the word to write.
      • getBytes

        public byte[] getBytes()
        Description copied from interface: IWordSerializer
        Returns the backing array of bytes that contain the serialized words.
        Specified by:
        getBytes in interface IWordSerializer
        Returns:
        the serialized words as a byte[].