es.uc3m.it.xbe32.tlv
Class TLVWriter

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

public class TLVWriter
extends Object

This class allows XBE32 TLVs to be serialized and written in an output stream.

Data is not actually written in the stream until the serialize() method is called. It can be called multiple times, thus when serialized, unclosed Complex TLVs are set to "unspecified" length and, once closed, the next serialize() method writes an END-OF-TLV.

For example, the following code:

         writer.openComplexTLV(0xDFFF);
         writer.writeSimpleTLV(0x2CFF, new byte[][] { { 0x00, 0x00, 0x00, 0x01 } });
         writer.serialize();
         writer.writeSimpleTLV(0xA602, new boolean[] { true });
         writer.openComplexTLV(0x1F00);
         writer.writeSimpleTLV(0x21FF, "áb");
         writer.writeSimpleTLV(0x2900, new short[] { -32768, 0 });
         writer.writeSimpleTLV(0x2900, new short[] {  32767 });
         writer.closeComplexTLV();
         writer.writeSimpleTLV(0x7204, new double[] { 4.9E-324 });
         writer.closeComplexTLV();
         writer.serialize();
 

Would generate the following XBE32-encoded stream (TLV view):

         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
        +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 

TLV's Length field is calculated automatically from the Values field of a Simple TLV, or from inner TLVs in the case of a Complex TLV.

See Also:
TLV

Constructor Summary
TLVWriter(OutputStream os)
          Creates a new TLVWriter to serialize XBE32 TLVs into the specified OutputStream.
 
Method Summary
 void closeComplexTLV()
          Closes the last opened Complex TLV.
 int getOffset()
          Returns the total length of the TLVs that have been serialized.
 int getOpenTLVs()
          Returns the number of Complex TLVs that have been opened, but not closed yet.
 void openComplexTLV(int type)
          Writes a Complex TLV with the specified Type field.
 int serialize()
          This method is the one that actually writes the Simple and Complex TLVs defined before into the OutputStream
 void writeSimpleTLV(int type, Object value)
          Writes a Simple TLV with the specified Type and Values fields.
 void writeTLV(TLV tlv)
          Writes the specified TLV, that could be a Simple or a Complex one.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

TLVWriter

public TLVWriter(OutputStream os)
Creates a new TLVWriter to serialize XBE32 TLVs into the specified OutputStream.

Parameters:
os - the stream to write TLVs at
Method Detail

getOffset

public int getOffset()
Returns the total length of the TLVs that have been serialized.

Returns:
the number of bytes already written in the stream

getOpenTLVs

public int getOpenTLVs()
Returns the number of Complex TLVs that have been opened, but not closed yet.

Returns:
the number of open Complex TLVs left

openComplexTLV

public void openComplexTLV(int type)
                    throws IllegalArgumentException
Writes a Complex TLV with the specified Type field.

Parameters:
type - the Complex TLV Type
Throws:
IllegalArgumentException - If the speficied Type value is not a Complex one
See Also:
closeComplexTLV(), serialize()

closeComplexTLV

public void closeComplexTLV()
Closes the last opened Complex TLV. All Complex TLVs MUST be closed in order to build a valid XBE32 TLV stream.

See Also:
openComplexTLV(int), serialize()

writeSimpleTLV

public void writeSimpleTLV(int type,
                           Object value)
                    throws IllegalArgumentException
Writes a Simple TLV with the specified Type and Values fields.

Parameters:
type - the Simple TLV Type
value - the Simple TLV Values
Throws:
IllegalArgumentException - If the specified Type value is not a Simple one, it does not match with the specified Value, or the Value has an incorrect length
See Also:
serialize(), TLV

writeTLV

public void writeTLV(TLV tlv)
Writes the specified TLV, that could be a Simple or a Complex one.

Parameters:
tlv - the TLV to be written
Throws:
NullPointerException - if tlv is null
See Also:
serialize(), TLV

serialize

public int serialize()
              throws IOException
This method is the one that actually writes the Simple and Complex TLVs defined before into the OutputStream

Returns:
the number of bytes written
Throws:
IOException - if an error occurs while writing in the underlying stream