Package org.whiteflagprotocol.java.core
Class WfBinaryBuffer
java.lang.Object
org.whiteflagprotocol.java.core.WfBinaryBuffer
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
Modifier and TypeFieldDescriptionstatic final String
The "0b" prefix of a binary stringstatic final int
The radic of a binary digitstatic final int
The bit size of a bitstatic final int
The bit size of a bytestatic final Pattern
The regex pattern describing a valid hexadecimal stringstatic final String
The "0x" prefix of a hexadecimal stringstatic final int
The radix of a hexadecimal digitstatic final int
The bit size of an octetstatic final int
The bit size of a quadbit -
Method Summary
Modifier and TypeMethodDescriptionfinal WfBinaryBuffer
addMessageField
(WfMessageField field) Encodes a Whiteflag message field and adds it to the end of the binary bufferfinal WfBinaryBuffer
append
(WfBinaryBuffer binaryBuffer) Appends a binary buffer to this binary bufferfinal WfBinaryBuffer
appendBits
(byte[] byteArray) Appends a bytes array to the binary bufferfinal WfBinaryBuffer
appendBits
(byte[] byteArray, int nBits) Appends the specified number of bits from a bytes array to the binary bufferfinal int
Returns the bit length of the binary bufferfinal int
Returns the byte length of the binary bufferprotected static final byte[]
concatinateBits
(byte[] byteArray1, int nBits1, byte[] byteArray2, int nBits2) Concatinates two bitsetsstatic final byte[]
convertToByteArray
(String hexstr) Converts a hexadecimal string to a byte arraystatic final String
convertToHexString
(byte[] byteArray) Converts a byte array to a hexadecimal stringfinal WfBinaryBuffer
copy()
Makes a copy of the binary bufferstatic final WfBinaryBuffer
create()
Creates a new Whiteflag binary bufferprotected static final byte[]
cropBits
(byte[] byteArray, int bitLength) Shortens the byte array to fit the length of the used bitsfinal byte[]
extractBits
(int startBit) Returns a byte array with a subset of the bits in the buffer from the specified start bit to the endfinal byte[]
extractBits
(int startBit, int bitLength) Returns a byte array with a subset of the bits in the bufferfinal WfMessageField
extractMessageField
(WfMessageField field, int startBit) Extracts and decodes a Whiteflag message field from the binary bufferstatic final WfBinaryBuffer
fromByteArray
(byte[] data) Creates a new Whiteflag binary encoded message buffer from a byte arraystatic final WfBinaryBuffer
fromByteArray
(byte[] data, int bitLength) Creates a new Whiteflag binary encoded message buffer from a byte arraystatic final WfBinaryBuffer
fromHexString
(String data) Creates a new Whiteflag binary encoded message buffer from a hexadecimal stringfinal boolean
Checks if the buffer is marked as complete and cannot be alteredfinal WfBinaryBuffer
Marks the buffer as complete and makes it read-onlystatic final String
removeStringPrefix
(String str, String prefix) Checks for and removes prefix from stringstatic final byte[]
shiftLeft
(byte[] byteArray, int shift) Shifts bits in a byte array to the left modulo 8static final byte[]
shiftRight
(byte[] byteArray, int shift) Shifts bits in a byte array to the right modulo 8final byte[]
Returns the Whiteflag encoded message as a byte arrayfinal String
Returns the Whiteflag encoded message as a hexademical string and caches it if the buffer is marked complete
-
Field Details
-
HEXPATTERN
The regex pattern describing a valid hexadecimal string -
BINPREFIX
The "0b" prefix of a binary string- See Also:
-
HEXPREFIX
The "0x" prefix of a hexadecimal string- See Also:
-
BINRADIX
public static final int BINRADIXThe radic of a binary digit- See Also:
-
HEXRADIX
public static final int HEXRADIXThe radix of a hexadecimal digit- See Also:
-
BYTE
public static final int BYTEThe bit size of a byte- See Also:
-
OCTET
public static final int OCTETThe bit size of an octet- See Also:
-
QUADBIT
public static final int QUADBITThe bit size of a quadbit- See Also:
-
BIT
public static final int BITThe bit size of a bit- See Also:
-
-
Method Details
-
create
Creates a new Whiteflag binary buffer- Returns:
- a new binary buffer
-
fromByteArray
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
Creates a new Whiteflag binary encoded message buffer from a byte array- Parameters:
data
- a byte array with a binary encoded Whiteflag message databitLength
- the bit length- Returns:
- a new binary buffer
-
fromHexString
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
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
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
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
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
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 encodedIllegalStateException
- 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 decodedstartBit
- 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
Checks for and removes prefix from string- Parameters:
str
- string to be checkedprefix
- the prefix to be checked for- Returns:
- a string without prefix
-
convertToByteArray
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
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 shiftedshift
- 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 shiftedshift
- 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
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
Appends the specified number of bits from a bytes array to the binary buffer- Parameters:
byteArray
- the byte array with the bits to be appendednBits
- 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 extractbitLength
- 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 bitsetnBits1
- number of bits in the first bitset, i.e. which bits to take from the first byte arraybyteArray2
- byte array containing the second bitsetnBits2
- 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 bitsetbitLength
- 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
-