Class CommandAPDU


  • public class CommandAPDU
    extends java.lang.Object
    A CommandAPDU represents an ISO 7816-4 specified Application Protocol Data Unit (APDU) sent to a smart card. A response from the smart card is in turn represented by a ResponseAPDU.

    According to ISO 7816-4, a CommandAPDU has the following format:
                      HEADER         |           BODY
             CLA    INS    P1    P2  |  [LC]    [DATA]    [LE]
     
    where
    • CLA is the class byte
    • INS is the instruction byte
    • P1 is the first parameter byte
    • P2 is the second parameter byte
    • LC is the number of bytes present in the data block
    • DATA is an byte array of data to be sent
    • LE is the maximum number of bytes expected in the ResponseAPDU
    • [ ] denotes optional fields

    Usage

       byte[] buffer = { (byte) 0x90, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x01, (byte) 0x02, (byte) 0x03 };
       CommandAPDU capdu = new CommandAPDU(buffer);
       
       CommandAPDU capdu = new CommandAPDU((byte) 0x90, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x01, (byte) 0x02,
                      (byte) 0x03);
       

    Additional information

    http://www.opencard.org
    See Also:
    ResponseAPDU
    • Field Summary

      Fields 
      Modifier and Type Field Description
      protected byte[] apduBuffer
      Byte array containing the entire CommandAPDU.
      protected int apduLength
      Length of this CommandAPDU currently in the apduBuffer.
      static int CLA
      Index for addressing CLA in this CommandAPDU apduBuffer.
      static int INS
      Index for addressing INS in this CommandAPDU apduBuffer.
      static int LC
      Index for addressing LC in this CommandAPDU apduBuffer.
      static int P1
      Index for addressing P1 in this CommandAPDU apduBuffer.
      static int P2
      Index for addressing P2 in this CommandAPDU apduBuffer.
    • Constructor Summary

      Constructors 
      Constructor Description
      CommandAPDU​(byte[] buffer)
      Constructs a new ISO 7816-4 CommandAPDU.
      CommandAPDU​(byte cla, byte ins, byte p1, byte p2)
      Constructs a new ISO 7816-4 CASE 1 CommandAPDU.
      CommandAPDU​(byte cla, byte ins, byte p1, byte p2, byte[] data)
      Constructs a new ISO 7816-4 CASE 3 CommandAPDU.
      CommandAPDU​(byte cla, byte ins, byte p1, byte p2, byte[] data, int le)
      Constructs a new ISO 7816-4 CASE 4 CommandAPDU.
      CommandAPDU​(byte cla, byte ins, byte p1, byte p2, int le)
      Constructs a new ISO 7816-4 CASE 2 CommandAPDU.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      byte[] getBuffer()
      Gets this CommandAPDU apduBuffer.
      byte getByte​(int index)
      Gets the byte at the specified offset in the apduBuffer.
      byte[] getBytes()
      Gets a byte array of the buffered CommandAPDU.
      byte getCLA()
      Gets the CLA byte value.
      byte getINS()
      Gets the INS byte value.
      int getLC()
      Gets the length of data field (LC).
      int getLE()
      Gets the expected length of ResponseAPDU (LE).
      int getLength()
      Gets the length of the buffered CommandAPDU.
      byte getP1()
      Gets the first parameter (P1) byte value.
      byte getP2()
      Gets the second parameter (P2) byte value.
      void setByte​(int index, byte value)
      Sets the byte value at the specified offset in the apduBuffer.
      java.lang.String toString()
      Gets a string representation of this CommandAPDU.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
    • Field Detail

      • CLA

        public static final int CLA
        Index for addressing CLA in this CommandAPDU apduBuffer.
        See Also:
        Constant Field Values
      • INS

        public static final int INS
        Index for addressing INS in this CommandAPDU apduBuffer.
        See Also:
        Constant Field Values
      • P1

        public static final int P1
        Index for addressing P1 in this CommandAPDU apduBuffer.
        See Also:
        Constant Field Values
      • P2

        public static final int P2
        Index for addressing P2 in this CommandAPDU apduBuffer.
        See Also:
        Constant Field Values
      • LC

        public static final int LC
        Index for addressing LC in this CommandAPDU apduBuffer.
        See Also:
        Constant Field Values
      • apduBuffer

        protected byte[] apduBuffer
        Byte array containing the entire CommandAPDU.
      • apduLength

        protected int apduLength
        Length of this CommandAPDU currently in the apduBuffer.
    • Constructor Detail

      • CommandAPDU

        public CommandAPDU​(byte[] buffer)
        Constructs a new ISO 7816-4 CommandAPDU.
        Parameters:
        buffer - the entire CommandAPDU as a byte array
      • CommandAPDU

        public CommandAPDU​(byte cla,
                           byte ins,
                           byte p1,
                           byte p2)
        Constructs a new ISO 7816-4 CASE 1 CommandAPDU.
        Parameters:
        cla - CLA byte
        ins - INS byte
        p1 - parameter byte P1
        p2 - parameter byte P2
      • CommandAPDU

        public CommandAPDU​(byte cla,
                           byte ins,
                           byte p1,
                           byte p2,
                           int le)
        Constructs a new ISO 7816-4 CASE 2 CommandAPDU.
        Parameters:
        cla - CLA byte
        ins - INS byte
        p1 - parameter byte P1
        p2 - parameter byte P2
        le - length of expected ResponseAPDU, ranges from -1 to 255, where -1 is no length and 0 is the maximum length supported
        See Also:
        ResponseAPDU
      • CommandAPDU

        public CommandAPDU​(byte cla,
                           byte ins,
                           byte p1,
                           byte p2,
                           byte[] data)
        Constructs a new ISO 7816-4 CASE 3 CommandAPDU.
        Parameters:
        cla - CLA byte
        ins - INS byte
        p1 - parameter byte P1
        p2 - parameter byte P2
        data - this CommandAPDU data as a byte array, LC is derived from this data array length
      • CommandAPDU

        public CommandAPDU​(byte cla,
                           byte ins,
                           byte p1,
                           byte p2,
                           byte[] data,
                           int le)
        Constructs a new ISO 7816-4 CASE 4 CommandAPDU.
        Parameters:
        cla - CLA byte
        ins - INS byte
        p1 - parameter byte P1
        p2 - parameter byte P2
        data - CommandAPDU data as a byte array, LC is derived from this data array length
        le - length of expected ResponseAPDU, ranges from -1 to 255, where -1 is no length and 0 is the maximum length supported
        See Also:
        ResponseAPDU
    • Method Detail

      • getCLA

        public byte getCLA()
        Gets the CLA byte value.
        Returns:
        CLA byte of this CommandAPDU
      • getINS

        public byte getINS()
        Gets the INS byte value.
        Returns:
        INS byte of this CommandAPDU
      • getP1

        public byte getP1()
        Gets the first parameter (P1) byte value.
        Returns:
        P1 byte of this CommandAPDU
      • getP2

        public byte getP2()
        Gets the second parameter (P2) byte value.
        Returns:
        P2 byte of this CommandAPDU
      • getLC

        public int getLC()
        Gets the length of data field (LC).
        Returns:
        the number of bytes present in the data field of this CommandAPDU, 0 indicates that there is no body
      • getLE

        public int getLE()
        Gets the expected length of ResponseAPDU (LE).
        Returns:
        the maximum number of bytes expected in the data field of the ResponseAPDU to this CommandAPDU, -1 indicates that no value is specified
        See Also:
        ResponseAPDU
      • getBuffer

        public final byte[] getBuffer()
        Gets this CommandAPDU apduBuffer. This method allows user to manipulate the buffered CommandAPDU.
        Returns:
        apduBuffer that holds the current CommandAPDU
        See Also:
        getBytes()
      • getByte

        public final byte getByte​(int index)
        Gets the byte at the specified offset in the apduBuffer. This method can only be used to access the CommandAPDU currently stored. It is not possible to read beyond the end of the CommandAPDU.
        Parameters:
        index - the offset in the apduBuffer
        Returns:
        the value at the given offset, or -1 if the offset is invalid
        See Also:
        setByte(int, byte), getLength()
      • getBytes

        public final byte[] getBytes()
        Gets a byte array of the buffered CommandAPDU. The byte array returned gets allocated with the exact size of the buffered CommandAPDU. To get direct access to the internal apduBuffer, use getBuffer().
        Returns:
        the buffered CommandAPDU copied into a new array
        See Also:
        getBuffer()
      • getLength

        public final int getLength()
        Gets the length of the buffered CommandAPDU.
        Returns:
        the length of the CommandAPDU currently stored
      • setByte

        public final void setByte​(int index,
                                  byte value)
        Sets the byte value at the specified offset in the apduBuffer. This method can only be used to modify a CommandAPDU already stored. It is not possible to set bytes beyond the end of the current CommandAPDU.
        Parameters:
        index - the offset in the apduBuffer
        value - the new byte value to store
        See Also:
        getByte(int), getLength()
      • toString

        public java.lang.String toString()
        Gets a string representation of this CommandAPDU.
        Overrides:
        toString in class java.lang.Object
        Returns:
        a string describing this CommandAPDU