Code Monkey home page Code Monkey logo

tlv's Introduction

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

Links

License

TLV is under the Do What The F*ck You Want To Public License. See the LICENSE file for details.

tlv's People

Contributors

yihleego avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.