How to design a TLV decoder?

hezjing hezjing at gmail.com
Sun Mar 28 20:55:23 EDT 2010


Hi

I want to encode and decode a message which contains a list of
Type-Length-Value (TLV).

So I have a class representing a message, and different classes for the
TLVs.
For example,

class Message {
    List<TLV> typeLenthValues;
}

interface TLV {
    int getType();
    int getLength();
    byte[] getValue();
}

class NameTLV implements TLV {
    void setFirstName() { ... }
    void setLastName() { ... }
}

class AddressTLV implements TLV {
    void setPostCode() { ... }
    void setStreet() { ... }
}


The encoding is straight-forward, I'm able to create one generic encoder
that handle all the different TLVs.

Unfortunately I'm not able to create a generic decoder to decode the raw
bytes into specific TLV.
For example the following is the code snippet from my decoder,

class decoder {
    public TLV decode(...) {
        TLV tlv = null;
        if (type == 0) {
            tlv = decodeName();
        } else if (type == 1) {
            tlv = decodeAddress();
        } else if (type == ...) {
        ...
        } else if (type == 80) {
            tlv = decodeX();
        }
    }
}


I don't like the code where I have to code each of the TLV, and there are
more TLVs introduced in the future!

Do you have any idea of how to create a decoder like this?


-- 

Hez
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.jboss.org/pipermail/netty-users/attachments/20100329/2374bfcb/attachment-0001.html 


More information about the netty-users mailing list