Tlv
📦 A TLV (type-length-value or tag-length-value) tool for Java. TLV is an encoding scheme used for optional information element in a certain protocol.
Install / Use
/learn @yihleego/TlvREADME
Type Length Value
Within data communication protocols, TLV (type-length-value or tag-length-value) is an encoding scheme used for optional information element in a certain protocol. The type and length are fixed in size (typically 1-4 bytes), and the value field is of variable size. These fields are used as follows:
Type A binary code, often simply alphanumeric, which indicates the kind of field that this part of the message represents;
Length The size of the value field (typically in bytes);
Value Variable-sized series of bytes which contains data for this part of the message.
Some advantages of using a TLV representation data system solution are:
- TLV sequences are easily searched using generalized parsing functions;
- New message elements which are received at an older node can be safely skipped and the rest of the message can be parsed. This is similar to the way that unknown XML tags can be safely skipped;
- TLV elements can be placed in any order inside the message body;
- TLV elements are typically used in a binary format which makes parsing faster and the data smaller;
- It is easier to generate XML from TLV to make human inspection of the data possible.
Brief
Sample TLV Object:
| 4 Bytes | 4 Bytes | Length Bytes | | :---: | :---: | :---: | | Type | Length | Value |
Serial TLV Objects:
| 4 Bytes | 4 Bytes | Length Bytes | 4 Bytes | 4 Bytes | Length Bytes | | :---: | :---: | :---: | :---: | :---: | :---: | | Type | Length | Value | Type | Length | Value |
Nested TLV Objects:
| 4 Bytes | 4 Bytes | | Length Bytes | | | :---: | :---: | :---: | :---: | :---: | | | | 4 Bytes | 4 Bytes | Length Bytes | | Type | Length | Type | Length | Value |
Usage
Serialize
TlvBox inner = TlvBox.create();
inner.put(255, "Inner TlvBox");
TlvBox box = TlvBox.create()
.put(0, true)
.put(1, (byte) 1)
.put(2, (short) 2)
.put(3, 3)
.put(4, (long) 4)
.put(5, 5.5f)
.put(6, 6.6)
.put(7, 'A')
.put(8, "Hello world!")
.put(9, new byte[]{3, 4, 6, 3, 9})
.put(10, inner);
byte[] bytes = box.serialize();
Parse
TlvBox parsed = TlvBox.parse(bytes);
Boolean v0 = parsed.getBoolean(0);
Byte v1 = parsed.getByte(1);
Short v2 = parsed.getShort(2);
Integer v3 = parsed.getInteger(3);
Long v4 = parsed.getLong(4);
Float v5 = parsed.getFloat(5);
Double v6 = parsed.getDouble(6);
Character v7 = parsed.getCharacter(7);
String v8 = parsed.getString(8);
byte[] v9 = parsed.getBytes(9);
TlvBox v10 = parsed.getObject(10);
Contact
- Bugs: Issues
Links
License
TLV is under the Do What The F*ck You Want To Public License. See the LICENSE file for details.
Related Skills
node-connect
351.2kDiagnose OpenClaw node connection and pairing failures for Android, iOS, and macOS companion apps
frontend-design
110.6kCreate distinctive, production-grade frontend interfaces with high design quality. Use this skill when the user asks to build web components, pages, or applications. Generates creative, polished code that avoids generic AI aesthetics.
openai-whisper-api
351.2kTranscribe audio via OpenAI Audio Transcriptions API (Whisper).
qqbot-media
351.2kQQBot 富媒体收发能力。使用 <qqmedia> 标签,系统根据文件扩展名自动识别类型(图片/语音/视频/文件)。
