Skip to content

TlvDecoder

com.rvi.application.TlvDecoder · Class · SocialCard 1.0.3

Read an NDEF message from its Type 2 Tag TLV wrapper.

Use decode(bytes) when you have a complete buffer that begins with an NDEF TLV. Use encodedLength(headerBytes) with the first bytes of tag memory to determine how much more data to read.

final NDefMessage message = new NDefEncoder().encodeURI("https://rithush.dev");
final byte[] tlv = new TlvEncoder().encode(message);
final int length = new TlvDecoder().encodedLength(Arrays.copyOf(tlv, 2));
System.out.println(length == tlv.length); // true for this short TLV
System.out.println(new TlvDecoder().decode(tlv).equals(message)); // true
Java imports
import com.rvi.application.NDefEncoder;
import com.rvi.application.TlvDecoder;
import com.rvi.application.TlvEncoder;
import com.rvi.domain.NDefMessage;
import java.util.Arrays;

A short length field needs two header bytes. When byte 1 is 0xFF, supply the full four-byte header.

encodedLength() returns the header plus declared value plus one byte for the terminator. It needs only a complete header. decode() checks that the declared bytes and terminator exist, rejects empty values and passes the extracted NDEF bytes to NDefMessage for structural validation.

TlvDecoder.Header · TlvEncoder · NDefMessage · TlvException

Method signatures in the Javadoc · All API types