Class 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)
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      protected TLV()  
      protected TLV​(TLV p)
      Copy constructor
    • 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 string
      abstract java.lang.String dump​(int indent)
      Convert TLV object to string presentation using a given left indentation
      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.
      static TLV factory​(byte[] buffer)
      Factory method to create proper TLV objects determined by their tag value
      static TLV factory​(byte[] buffer, int offset, int length)
      Factory method to create proper TLV objects determined by their tag value
      static TLV factory​(ParseBuffer pb)
      Factory method to create proper TLV objects determined by their tag value
      byte[] getBytes()
      Return binary presentation of TLV object
      abstract int getLength()
      Return length of value field
      static byte[] getLengthFieldAsByteArray​(int length, boolean altFormat)
      Return length field as byte array
      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
      protected static int getLengthFieldSizeHelper​(int length, boolean altFormat)
      Helper function for getSize() and getLengthFieldSize()
      java.lang.String getName()
      Return name assigned to TLV object or null
      int getSize()
      Return the size of the TLV object in bytes
      Tag getTag()
      Returns the tag object
      byte[] getValue()
      Returns the value field
      protected static int lengthFromByteArray​(byte[] buffer, int offset, boolean altFormat)
      Helper function to decode the length field
      protected static int lengthToByteArray​(int length, byte[] buffer, int offset, boolean altFormat)
      Encode length field in byte array
      void setName​(java.lang.String name)
      Assign a name to the TLV object
      void 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 overwritten
      java.lang.String toString()
      Return Tag of TLV object as string
      void 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
    • 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 data
        offset - 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 data
        offset - 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 encoded
        buffer - Byte arrays to copy length into
        offset - Offset in byte array to which length shall be stored
        altFormat - 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 encode
        altFormat - 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 buffer
        offset - Offset in buffer
        altFormat - 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 data
        offset - Offset in buffer to start decoding
        length - 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 class java.lang.Object
        Returns:
        String containing name of TLV object