Package de.cardcontact.tlv
Class TLV
- java.lang.Object
-
- de.cardcontact.tlv.TLV
-
- All Implemented Interfaces:
TreeNode
- Direct Known Subclasses:
ConstructedTLV
,PrimitiveTLV
public abstract class TLV extends java.lang.Object implements TreeNode
Class to represent a TLV encoded data object Follows the composite design pattern.- Author:
- Andreas Schwier (info@cardcontact.de)
-
-
Field Summary
Fields Modifier and Type Field Description protected boolean
alternateLengthFormat
protected java.lang.String
name
protected Tag
tag
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description java.lang.String
dump()
Dump TLV object as stringabstract java.lang.String
dump(int indent)
Convert TLV object to string presentation using a given left indentationprotected java.lang.String
dumpSingleLine(int indent)
Convert object indentifier to ASN.1 string syntax Used by subclasses to overwrite dump() when the output fit on a single line.static TLV
factory(byte[] buffer)
Factory method to create proper TLV objects determined by their tag valuestatic TLV
factory(byte[] buffer, int offset, int length)
Factory method to create proper TLV objects determined by their tag valuestatic TLV
factory(ParseBuffer pb)
Factory method to create proper TLV objects determined by their tag valuebyte[]
getBytes()
Return binary presentation of TLV objectabstract int
getLength()
Return length of value fieldstatic byte[]
getLengthFieldAsByteArray(int length, boolean altFormat)
Return length field as byte arrayint
getLengthFieldSize()
Return size of length field by calling overloaded getLength() and determining the number of bytes required to store the length in DER oder DGI codingprotected static int
getLengthFieldSizeHelper(int length, boolean altFormat)
Helper function for getSize() and getLengthFieldSize()java.lang.String
getName()
Return name assigned to TLV object or nullint
getSize()
Return the size of the TLV object in bytesTag
getTag()
Returns the tag objectbyte[]
getValue()
Returns the value fieldprotected static int
lengthFromByteArray(byte[] buffer, int offset, boolean altFormat)
Helper function to decode the length fieldprotected static int
lengthToByteArray(int length, byte[] buffer, int offset, boolean altFormat)
Encode length field in byte arrayvoid
setName(java.lang.String name)
Assign a name to the TLV objectvoid
setTag(Tag tag)
Replace tag, e.g.protected abstract int
toByteArray(byte[] buffer, int offset)
Store TLV object to binary buffer Abstract method must be overwrittenjava.lang.String
toString()
Return Tag of TLV object as stringvoid
useAlternateLengthEncoding()
Enable alternate encoding of length field.protected abstract int
valueToByteArray(byte[] buffer, int offset)
Store value to binary buffer Abtract method must be overwritten-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface de.cardcontact.tlv.TreeNode
getChildAt, getChildCount, getIndex, getParent, isLeaf
-
-
-
-
Field Detail
-
tag
protected Tag tag
-
name
protected java.lang.String name
-
alternateLengthFormat
protected boolean alternateLengthFormat
-
-
Constructor Detail
-
TLV
protected TLV()
-
TLV
protected TLV(TLV p)
Copy constructor- Parameters:
p
-
-
-
Method Detail
-
toByteArray
protected abstract int toByteArray(byte[] buffer, int offset)
Store TLV object to binary buffer Abstract method must be overwritten- Parameters:
buffer
- Byte array that received the binary dataoffset
- Offset in byte array- Returns:
- New offset behind the stored object
-
valueToByteArray
protected abstract int valueToByteArray(byte[] buffer, int offset)
Store value to binary buffer Abtract method must be overwritten- Parameters:
buffer
- Byte array that received the binary dataoffset
- Offset in byte array- Returns:
- New offset behind the stored object
-
getValue
public byte[] getValue()
Returns the value field- Returns:
- the encoded TLV object
-
getBytes
public byte[] getBytes()
Return binary presentation of TLV object- Returns:
- Binary data containing TLV structure
-
setTag
public void setTag(Tag tag)
Replace tag, e.g. for implicit encoding- Parameters:
tag
- the new tag
-
getTag
public Tag getTag()
Returns the tag object- Returns:
- the tag
-
getLength
public abstract int getLength()
Return length of value field- Returns:
- the length in bytes
-
useAlternateLengthEncoding
public void useAlternateLengthEncoding()
Enable alternate encoding of length field. If enabled then the length field is encoded in 1 or 3 bytes. Length values in the range 0 - 254 are encoded in one byte. Length values in the range 255 - 65535 are encoded on 3 bytes with the first byte set to 0xFF
-
getSize
public int getSize()
Return the size of the TLV object in bytes- Returns:
- Size of TLV object in bytes
-
getLengthFieldSize
public int getLengthFieldSize()
Return size of length field by calling overloaded getLength() and determining the number of bytes required to store the length in DER oder DGI coding- Returns:
- length
-
getLengthFieldSizeHelper
protected static int getLengthFieldSizeHelper(int length, boolean altFormat)
Helper function for getSize() and getLengthFieldSize()- Parameters:
length
-altFormat
-- Returns:
-
lengthToByteArray
protected static int lengthToByteArray(int length, byte[] buffer, int offset, boolean altFormat)
Encode length field in byte array- Parameters:
length
- Length to be encodedbuffer
- Byte arrays to copy length intooffset
- Offset in byte array to which length shall be storedaltFormat
- True if DGI format shall be used- Returns:
- New offset behind the length value
-
getLengthFieldAsByteArray
public static byte[] getLengthFieldAsByteArray(int length, boolean altFormat)
Return length field as byte array- Parameters:
length
- Length to encodealtFormat
- DER or DGI format- Returns:
- Length field as byte array
-
lengthFromByteArray
protected static int lengthFromByteArray(byte[] buffer, int offset, boolean altFormat) throws TLVEncodingException
Helper function to decode the length field- Parameters:
buffer
- Binary bufferoffset
- Offset in bufferaltFormat
- User alternative length encoding (DGI)- Returns:
- Throws:
TLVEncodingException
-
factory
public static TLV factory(ParseBuffer pb) throws TLVEncodingException
Factory method to create proper TLV objects determined by their tag value- Parameters:
pb
- ParseBuffer containing binary data- Returns:
- Newly created TLV object
- Throws:
TLVEncodingException
- Error decoding binary data into TLV structure
-
factory
public static TLV factory(byte[] buffer, int offset, int length) throws TLVEncodingException
Factory method to create proper TLV objects determined by their tag value- Parameters:
buffer
- Buffer containing binary dataoffset
- Offset in buffer to start decodinglength
- Maximum number of bytes to decode- Returns:
- Newly created TLV object
- Throws:
TLVEncodingException
- Error decoding binary data into TLV structure
-
factory
public static TLV factory(byte[] buffer) throws TLVEncodingException
Factory method to create proper TLV objects determined by their tag value- Parameters:
buffer
- Buffer containing binary data- Returns:
- Newly created TLV object
- Throws:
TLVEncodingException
- Error decoding binary data into TLV structure
-
getName
public java.lang.String getName()
Return name assigned to TLV object or null- Returns:
- Name of TLV object
-
setName
public void setName(java.lang.String name)
Assign a name to the TLV object- Parameters:
name
- Name to assign
-
dump
public abstract java.lang.String dump(int indent)
Convert TLV object to string presentation using a given left indentation- Parameters:
indent
- Left indentation- Returns:
- String containing TLV object information
-
dumpSingleLine
protected java.lang.String dumpSingleLine(int indent)
Convert object indentifier to ASN.1 string syntax Used by subclasses to overwrite dump() when the output fit on a single line.- Parameters:
indent
- Left indentation- Returns:
- String containing the ASN.1 representation
-
dump
public java.lang.String dump()
Dump TLV object as string- Returns:
- String containing dump of TLV object
-
toString
public java.lang.String toString()
Return Tag of TLV object as string- Overrides:
toString
in classjava.lang.Object
- Returns:
- String containing name of TLV object
-
-