es.uc3m.it.xbe32.tlv
Class TLVParser

java.lang.Object
  extended byes.uc3m.it.xbe32.tlv.TLVParser

public class TLVParser
extends Object

This class allows the TLVs of a XBE32-encoded stream or buffer to be parsed.
For each TLV parsed, one or more parsing events are generated, and sent to the registered TLVParseListener, if any.

For example, parsing the following XBE32-encoded stream:

         0                   1                   2                   3
         0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
        +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
0       |C|E|    <EXT COMPLEX TLV>      |      <UNSPECIFIED LENGTH>     |  DFFF 0000
        +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
4       |           <Ext Id TLV>        |          Length = 8           |  2CFF 0008
        +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
8       |                          0x00000001                           |  0000 0001
        +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
12      |C|       Type = 0xA602         |          Length = 5           |  A602 0005
        +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
16      |      true     |                                               |  FF00 0000
        +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
20      |       <EXT ATTRIBUTE TLV>     |          LENGTH = 28          |  1F00 001C
        +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
24      |          <Ext Name TLV>       |          Length = 7           |  21FF 0007
        +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
28      |      0xC2     |      0x81     |       'b'     |               |  C281 6200
        +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
32      |       <Ext Int16 Value TLV>   |          Length = 8           |  2900 0008
        +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
36      |            -32768             |               0               |  8000 0000
        +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
40      |       <Ext Int16 Value TLV>   |          Length = 6           |  2900 0006
        +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
44      |             32767             |                               |  7FFF 0000
        +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
48      | |E|     Type = 0x7204         |          Length = 12          |  7204 000C
        +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
52      |                                                               |  0000 0000
        +                           4.9E-324                            +
56      |                                                               |  0000 0001
        +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
60      |      <END-OF-TLV 0xDFFF>      |          LENGTH = 4           |  0000 0004
        +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 

It would generate the following TLVParseListener event sequence:

        listener.startParsing();
        listener.complexTLVStart(0xDFFF, 0);
        listener.simpleTLV(0x2CFF, 8, new byte[][] {{ 0x10, 0x00, 0x00, 0x01 }});
        listener.simpleTLV(0xA602, 5, new boolean[] { true });
        listener.complexTLVStart(0x1F00, 28);
        listener.simpleTLV(0x21FF, 7, "áb");
        listener.simpleTLV(0x2900, 8, new short[] { -32768, 0 });
        listener.simpleTLV(0x2900, 6, new short[] {  32767 });
        listener.complexTLVEnd(0x1F00, 28);
        listener.simpleTLV(0x7204, 12, new double[] { 4.9E-324 });
        listener.complexTLVEnd(0x1FFF, 64);
        listener.endParsing(64);
 

See Also:
TLVParseListener, TLV

Constructor Summary
TLVParser(byte[] buffer)
          Create a TLVParser to parse the specified buffer.
TLVParser(byte[] buffer, int offset, int length)
          Create a TLVParser to parse the specified part of the buffer.
TLVParser(InputStream is)
          Create a TLVParser to parse the specified InputStream.
 
Method Summary
 TLVParseListener getListener()
          Returns the listener that is processing the XBE32 parsing events.
 int getOffset()
          Returns the number of bytes that have been parsed.
 void parse()
          Parse the full XBE32-encoded stream.
 void parseTLV()
          Parse the next TLV from the XBE32-encoded stream.
 void setListener(TLVParseListener lis)
          Sets the listener that would process XBE32 parsing events.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

TLVParser

public TLVParser(InputStream is)
Create a TLVParser to parse the specified InputStream.

Parameters:
is - The XBE32-encoded stream to be parsed

TLVParser

public TLVParser(byte[] buffer)
Create a TLVParser to parse the specified buffer.

Parameters:
buffer - The XBE32-encoded buffer to be parsed

TLVParser

public TLVParser(byte[] buffer,
                 int offset,
                 int length)
Create a TLVParser to parse the specified part of the buffer.

Parameters:
buffer - The XBE32-encoded buffer to be parsed
offset - The buffer position to start parsing from
length - The number of bytes to be parsed
Method Detail

getOffset

public int getOffset()
Returns the number of bytes that have been parsed.

Returns:
the bytes already parsed

getListener

public TLVParseListener getListener()
Returns the listener that is processing the XBE32 parsing events.

Returns:
the current parser listener

setListener

public void setListener(TLVParseListener lis)
Sets the listener that would process XBE32 parsing events.

Parameters:
lis - the new parser listener

parse

public void parse()
           throws IOException,
                  TLVParseException
Parse the full XBE32-encoded stream.
The XBE32 parsing events are sent to the specified TLVParseListener.

Throws:
IOException - if an error occurs while reading the underlying stream
TLVParseException - if a syntax error occurs while parsing a XBE32 TLVs from the stream
See Also:
TLVParseListener

parseTLV

public void parseTLV()
              throws IOException,
                     TLVParseException
Parse the next TLV from the XBE32-encoded stream.
The XBE32 parsing events are sent to the specified TLVParseListener.

If the parsed TLV is a Complex one, all its inner TLVs will be also parserd recursively, until the upper Complex TLV ends.

Throws:
IOException - if an error occurs while reading the underlying stream
TLVParseException - if a syntax error occurs while parsing a XBE32 TLVs from the stream
See Also:
TLVParseListener