public class Klv extends Object
KLV has been used for years as a repeatable, no-guesswork technique for byte-packing data, that is, sending data in a binary format with two bytes for this integer, four bytes for that float, and so forth. KLV is used in broadcast television and is defined in SMPTE 336M-2001, but it also greatly eases the burden of non-TV-related applications for an easy, interchangeable binary format.
The underlying byte array is always king. If you change the key length or the length encoding, you only change how the underlying byte array is interpreted on subsequent calls.
Everything in KLV is Big Endian.
All getValue... methods will return up to the number of bytes specified in the length fields unless there are fewer bytes actually given. In that case, the number of bytes given will be used. This is to make the code more robust for reading corrupted data.
Modifier and Type | Class and Description |
---|---|
static class |
Klv.KeyLength
The number of bytes in the key field can be one byte, two bytes, four bytes, or sixteen bytes.
|
static class |
Klv.LengthEncoding
The encoding style for the length field can be fixed at one byte, two bytes, four bytes, or
variable with Basic Encoding Rules (BER).
|
Modifier and Type | Method and Description |
---|---|
static List<Klv> |
bytesToList(byte[] bytes,
int offset,
int length,
Klv.KeyLength keyLength,
Klv.LengthEncoding lengthEncoding)
Returns a list of KLV sets in the supplied byte array assuming the provided key length and
length field encoding.
|
byte[] |
getFullKey()
Returns a byte array representing the key.
|
byte[] |
getValue()
Returns the value of this KLV set as a copy of the underlying byte array.
|
int |
getValueAs16bitSignedInt()
Returns up to the first two bytes of the value as a 16-bit signed integer.
|
int |
getValueAs16bitUnsignedInt()
Returns up to the first two bytes of the value as a 16-bit unsigned integer.
|
int |
getValueAs32bitInt()
Returns up to the first four bytes of the value as a 32-bit int.
|
long |
getValueAs64bitLong()
Returns up to the first eight bytes of the value as a 64-bit signed long.
|
int |
getValueAs8bitSignedInt()
Returns up to the first byte of the value as an 8-bit signed integer.
|
int |
getValueAs8bitUnsignedInt()
Returns up to the first byte of the value as an 8-bit unsigned integer.
|
double |
getValueAsDouble()
Returns the first eight bytes of the value as a double according to IEEE 754 byte packing.
|
float |
getValueAsFloat()
Returns the first four bytes of the value as a float according to IEEE 754 byte packing.
|
String |
getValueAsString(String charsetName)
Return the value as a String interpreted with the given encoding.
|
public byte[] getFullKey()
public byte[] getValue()
public int getValueAs8bitSignedInt()
public int getValueAs8bitUnsignedInt()
public int getValueAs16bitSignedInt()
public int getValueAs16bitUnsignedInt()
public int getValueAs32bitInt()
getValueAs64bitLong()
.public long getValueAs64bitLong()
public float getValueAsFloat()
Float.intBitsToFloat
with getValueAs32bitInt()
as the argument. However it does check to see that the value has at least
four bytes. If it does not, then Float.NaN is returned.public double getValueAsDouble()
Double.longBitsToDouble
with
getValueAs64bitLong()
as the argument. However it does check to see that the value has
at least eight bytes. If it does not, then Double.NaN is returned.public String getValueAsString(String charsetName) throws UnsupportedEncodingException
charsetName
- the character encodingUnsupportedEncodingException
- if the String value cannot be interpreted using the given
encodingpublic static List<Klv> bytesToList(byte[] bytes, int offset, int length, Klv.KeyLength keyLength, Klv.LengthEncoding lengthEncoding)
bytes
- The byte array to parseoffset
- Where to start parsinglength
- How many bytes to parsekeyLength
- Length of keys assumed in the KLV setslengthEncoding
- Flag indicating encoding typeThis work is licensed under a Creative Commons Attribution 4.0 International License.