|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectes.uc3m.it.xbe32.tlv.TLVWriter
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.
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 |
public TLVWriter(OutputStream os)
os
- the stream to write TLVs atMethod Detail |
public int getOffset()
public int getOpenTLVs()
public void openComplexTLV(int type) throws IllegalArgumentException
type
- the Complex TLV Type
IllegalArgumentException
- If the speficied Type value is not a
Complex onecloseComplexTLV()
,
serialize()
public void closeComplexTLV()
openComplexTLV(int)
,
serialize()
public void writeSimpleTLV(int type, Object value) throws IllegalArgumentException
type
- the Simple TLV Typevalue
- the Simple TLV Values
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 lengthserialize()
,
TLV
public void writeTLV(TLV tlv)
tlv
- the TLV to be written
NullPointerException
- if tlv
is null
serialize()
,
TLV
public int serialize() throws IOException
IOException
- if an error occurs while writing in the underlying
stream
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |