Class Klv
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.
- Version:
- 0.3
- Author:
- Robert Harder, rharder # users.sourceforge.net
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic enumThe number of bytes in the key field can be one byte, two bytes, four bytes, or sixteen bytes.static enumThe encoding style for the length field can be fixed at one byte, two bytes, four bytes, or variable with Basic Encoding Rules (BER). -
Method Summary
Modifier and TypeMethodDescriptionbytesToList(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[]Returns a byte array representing the key.byte[]getValue()Returns the value of this KLV set as a copy of the underlying byte array.intReturns up to the first two bytes of the value as a 16-bit signed integer.intReturns up to the first two bytes of the value as a 16-bit unsigned integer.intReturns up to the first four bytes of the value as a 32-bit int.longReturns up to the first eight bytes of the value as a 64-bit signed long.intReturns up to the first byte of the value as an 8-bit signed integer.intReturns up to the first byte of the value as an 8-bit unsigned integer.doubleReturns the first eight bytes of the value as a double according to IEEE 754 byte packing.floatReturns the first four bytes of the value as a float according to IEEE 754 byte packing.getValueAsString(String charsetName) Return the value as a String interpreted with the given encoding.
-
Method Details
-
getFullKey
public byte[] getFullKey()Returns a byte array representing the key. This is a copy of the bytes from the original byte set.- Returns:
- the key
-
getValue
public byte[] getValue()Returns the value of this KLV set as a copy of the underlying byte array.- Returns:
- the value
-
getValueAs8bitSignedInt
public int getValueAs8bitSignedInt()Returns up to the first byte of the value as an 8-bit signed integer.- Returns:
- the value as an 8-bit signed integer
-
getValueAs8bitUnsignedInt
public int getValueAs8bitUnsignedInt()Returns up to the first byte of the value as an 8-bit unsigned integer.- Returns:
- the value as an 8-bit unsigned integer
-
getValueAs16bitSignedInt
public int getValueAs16bitSignedInt()Returns up to the first two bytes of the value as a 16-bit signed integer.- Returns:
- the value as a 16-bit signed integer
-
getValueAs16bitUnsignedInt
public int getValueAs16bitUnsignedInt()Returns up to the first two bytes of the value as a 16-bit unsigned integer.- Returns:
- the value as a 16-bit unsigned integer
-
getValueAs32bitInt
public int getValueAs32bitInt()Returns up to the first four bytes of the value as a 32-bit int. Since all Java ints are signed, there is no signed/unsigned option. If you need a 32-bit unsigned int, trygetValueAs64bitLong().- Returns:
- the value as an int
-
getValueAs64bitLong
public long getValueAs64bitLong()Returns up to the first eight bytes of the value as a 64-bit signed long. Note if you expect a 32-bit unsigned int, and since Java doesn't have such a thing, you could return a long instead and get the proper effect.- Returns:
- the value as a long
-
getValueAsFloat
public float getValueAsFloat()Returns the first four bytes of the value as a float according to IEEE 754 byte packing. See Java's Float class for details. This method callsFloat.intBitsToFloatwithgetValueAs32bitInt()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.- Returns:
- the value as a float
-
getValueAsDouble
public double getValueAsDouble()Returns the first eight bytes of the value as a double according to IEEE 754 byte packing. See Java's Double class for details. This method callsDouble.longBitsToDoublewithgetValueAs64bitLong()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.- Returns:
- the value as a double
-
getValueAsString
Return the value as a String interpreted with the given encoding.- Parameters:
charsetName- the character encoding- Returns:
- value as String
- Throws:
UnsupportedEncodingException- if the String value cannot be interpreted using the given encoding
-
bytesToList
public 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.- Parameters:
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 type- Returns:
- List of KLVs
-