Class WfBinaryBuffer

java.lang.Object
org.whiteflagprotocol.java.core.WfBinaryBuffer

public final class WfBinaryBuffer extends Object
Whiteflag encoded message object

This class defines a binary buffer to represent encoded Whiteflag messages. It can either be created by: 1. appending message fields from a Whiteflag message, which are encoded when added, or 2. providing a hexadecimal string or byte array with an encoded message. The binary buffer can be converted to a hexadecimal string or a byte byffer, e.g. for encryption or embedding the message in a blockchain transaction. After the buffer is marked as complete, it cannot be altered.

Since:
1.1
Whiteflag standard x-reference:
4.1 Message Structure
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final String
    The "0b" prefix of a binary string
    static final int
    The radic of a binary digit
    static final int
    The bit size of a bit
    static final int
    The bit size of a byte
    static final Pattern
    The regex pattern describing a valid hexadecimal string
    static final String
    The "0x" prefix of a hexadecimal string
    static final int
    The radix of a hexadecimal digit
    static final int
    The bit size of an octet
    static final int
    The bit size of a quadbit
  • Method Summary

    Modifier and Type
    Method
    Description
    Encodes a Whiteflag message field and adds it to the end of the binary buffer
    append(WfBinaryBuffer binaryBuffer)
    Appends a binary buffer to this binary buffer
    appendBits(byte[] byteArray)
    Appends a bytes array to the binary buffer
    appendBits(byte[] byteArray, int nBits)
    Appends the specified number of bits from a bytes array to the binary buffer
    final int
    Returns the bit length of the binary buffer
    final int
    Returns the byte length of the binary buffer
    protected static final byte[]
    concatinateBits(byte[] byteArray1, int nBits1, byte[] byteArray2, int nBits2)
    Concatinates two bitsets
    static final byte[]
    Converts a hexadecimal string to a byte array
    static final String
    convertToHexString(byte[] byteArray)
    Converts a byte array to a hexadecimal string
    Makes a copy of the binary buffer
    static final WfBinaryBuffer
    Creates a new Whiteflag binary buffer
    protected static final byte[]
    cropBits(byte[] byteArray, int bitLength)
    Shortens the byte array to fit the length of the used bits
    final byte[]
    extractBits(int startBit)
    Returns a byte array with a subset of the bits in the buffer from the specified start bit to the end
    final byte[]
    extractBits(int startBit, int bitLength)
    Returns a byte array with a subset of the bits in the buffer
    extractMessageField(WfMessageField field, int startBit)
    Extracts and decodes a Whiteflag message field from the binary buffer
    static final WfBinaryBuffer
    fromByteArray(byte[] data)
    Creates a new Whiteflag binary encoded message buffer from a byte array
    static final WfBinaryBuffer
    fromByteArray(byte[] data, int bitLength)
    Creates a new Whiteflag binary encoded message buffer from a byte array
    static final WfBinaryBuffer
    Creates a new Whiteflag binary encoded message buffer from a hexadecimal string
    final boolean
    Checks if the buffer is marked as complete and cannot be altered
    Marks the buffer as complete and makes it read-only
    static final String
    Checks for and removes prefix from string
    static final byte[]
    shiftLeft(byte[] byteArray, int shift)
    Shifts bits in a byte array to the left modulo 8
    static final byte[]
    shiftRight(byte[] byteArray, int shift)
    Shifts bits in a byte array to the right modulo 8
    final byte[]
    Returns the Whiteflag encoded message as a byte array
    final String
    Returns the Whiteflag encoded message as a hexademical string and caches it if the buffer is marked complete

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

  • Method Details

    • create

      public static final WfBinaryBuffer create()
      Creates a new Whiteflag binary buffer
      Returns:
      a new binary buffer
    • fromByteArray

      public static final WfBinaryBuffer fromByteArray(byte[] data)
      Creates a new Whiteflag binary encoded message buffer from a byte array
      Parameters:
      data - a byte array with a binary encoded Whiteflag message data
      Returns:
      a new binary buffer
    • fromByteArray

      public static final WfBinaryBuffer fromByteArray(byte[] data, int bitLength)
      Creates a new Whiteflag binary encoded message buffer from a byte array
      Parameters:
      data - a byte array with a binary encoded Whiteflag message data
      bitLength - the bit length
      Returns:
      a new binary buffer
    • fromHexString

      public static final WfBinaryBuffer fromHexString(String data)
      Creates a new Whiteflag binary encoded message buffer from a hexadecimal string
      Parameters:
      data - a hexadecimal string with a binary encoded Whiteflag message data
      Returns:
      a new binary buffer
    • bitLength

      public final int bitLength()
      Returns the bit length of the binary buffer
      Returns:
      the buffer length in bits
    • byteLength

      public final int byteLength()
      Returns the byte length of the binary buffer
      Returns:
      the buffer length in bits
    • copy

      public final WfBinaryBuffer copy()
      Makes a copy of the binary buffer
      Returns:
      a copy of this binary buffer
    • toByteArray

      public final byte[] toByteArray()
      Returns the Whiteflag encoded message as a byte array
      Returns:
      a byte array with an encoded message
    • toHexString

      public final String toHexString()
      Returns the Whiteflag encoded message as a hexademical string and caches it if the buffer is marked complete
      Returns:
      a hexadecimal string with the encoded message
    • markComplete

      public final WfBinaryBuffer markComplete()
      Marks the buffer as complete and makes it read-only
      Returns:
      this binary buffer
    • isComplete

      public final boolean isComplete()
      Checks if the buffer is marked as complete and cannot be altered
      Returns:
      TRUE if buffer is marked as complete
    • append

      public final WfBinaryBuffer append(WfBinaryBuffer binaryBuffer) throws IllegalStateException
      Appends a binary buffer to this binary buffer
      Parameters:
      binaryBuffer - the binary buffer to append to this binary buffer
      Returns:
      this binary buffer
      Throws:
      IllegalStateException - if the buffer is marked complete and cannot be altered
    • addMessageField

      public final WfBinaryBuffer addMessageField(WfMessageField field) throws WfCoreException
      Encodes a Whiteflag message field and adds it to the end of the binary buffer
      Parameters:
      field - the next message field to be encoded and added to the buffer
      Returns:
      this binary buffer
      Throws:
      WfCoreException - if field connot be encoded
      IllegalStateException - if the buffer is marked complete and cannot be altered
    • extractMessageField

      public final WfMessageField extractMessageField(WfMessageField field, int startBit) throws WfCoreException
      Extracts and decodes a Whiteflag message field from the binary buffer
      Parameters:
      field - the message field to be extracted and decoded
      startBit - the bit where the encoded field is located in the buffer
      Returns:
      String with the decoded field value
      Throws:
      WfCoreException - if field connot be decoded
    • removeStringPrefix

      public static final String removeStringPrefix(String str, String prefix)
      Checks for and removes prefix from string
      Parameters:
      str - string to be checked
      prefix - the prefix to be checked for
      Returns:
      a string without prefix
    • convertToByteArray

      public static final byte[] convertToByteArray(String hexstr)
      Converts a hexadecimal string to a byte array
      Parameters:
      hexstr - the hexadecimal string
      Returns:
      a byte array
      Throws:
      IllegalArgumentException - if argument is not a hexadecimal string
    • convertToHexString

      public static final String convertToHexString(byte[] byteArray)
      Converts a byte array to a hexadecimal string
      Parameters:
      byteArray - the byte array
      Returns:
      a hexadecimal string
    • shiftRight

      public static final byte[] shiftRight(byte[] byteArray, int shift)
      Shifts bits in a byte array to the right modulo 8
      Parameters:
      byteArray - the byte array to be right shifted
      shift - the nummber of bits to be right shifted by modulo 8 bits
      Returns:
      a new byte array from the right shifted source byte array
    • shiftLeft

      public static final byte[] shiftLeft(byte[] byteArray, int shift)
      Shifts bits in a byte array to the left modulo 8
      Parameters:
      byteArray - the byte array to be left shifted
      shift - the nummber of bits to be left shifted by modulo 8 bits
      Returns:
      a new byte array from the left shifted source byte array
    • appendBits

      public final WfBinaryBuffer appendBits(byte[] byteArray)
      Appends a bytes array to the binary buffer
      Parameters:
      byteArray - the byte array with the bits to be appended
      Returns:
      this binary buffer
      Throws:
      IllegalStateException - if the buffer is marked complete and cannot be altered
    • appendBits

      public final WfBinaryBuffer appendBits(byte[] byteArray, int nBits)
      Appends the specified number of bits from a bytes array to the binary buffer
      Parameters:
      byteArray - the byte array with the bits to be appended
      nBits - the number of bits to be appended from the byte array
      Returns:
      this binary buffer
      Throws:
      IllegalStateException - if the buffer is marked complete and cannot be altered
    • extractBits

      public final byte[] extractBits(int startBit)
      Returns a byte array with a subset of the bits in the buffer from the specified start bit to the end
      Parameters:
      startBit - the first bit of the subset to extract
      Returns:
      a byte array with the extracted bits
    • extractBits

      public final byte[] extractBits(int startBit, int bitLength)
      Returns a byte array with a subset of the bits in the buffer
      Parameters:
      startBit - the first bit of the subset to extract
      bitLength - the length of the subset, i.e. the number of bits to extract
      Returns:
      a byte array with the extracted bits
    • concatinateBits

      protected static final byte[] concatinateBits(byte[] byteArray1, int nBits1, byte[] byteArray2, int nBits2)
      Concatinates two bitsets
      Parameters:
      byteArray1 - byte array containing the first bitset
      nBits1 - number of bits in the first bitset, i.e. which bits to take from the first byte array
      byteArray2 - byte array containing the second bitset
      nBits2 - number of bits in the second bitset, i.e. which bits to take from the second byte array
      Returns:
      a new byte array with the concatinated bits
    • cropBits

      protected static final byte[] cropBits(byte[] byteArray, int bitLength)
      Shortens the byte array to fit the length of the used bits
      Parameters:
      byteArray - the byte array containing the bitset
      bitLength - the bit length of the buffer (i.e. the number of used bits in the bitset), or, if negative, the number of bits to remove (i.e. the number of unused bits in the bitset)
      Returns:
      the byte array with the unused bits cleared