Class CommandAPDU

java.lang.Object
com.dalsemi.onewire.container.CommandAPDU

public class CommandAPDU extends 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:
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    protected byte[]
    Byte array containing the entire CommandAPDU.
    protected int
    Length of this CommandAPDU currently in the apduBuffer.
    static final int
    Index for addressing CLA in this CommandAPDU apduBuffer.
    static final int
    Index for addressing INS in this CommandAPDU apduBuffer.
    static final int
    Index for addressing LC in this CommandAPDU apduBuffer.
    static final int
    Index for addressing P1 in this CommandAPDU apduBuffer.
    static final int
    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

    Modifier and Type
    Method
    Description
    final byte[]
    Gets this CommandAPDU apduBuffer.
    final byte
    getByte(int index)
    Gets the byte at the specified offset in the apduBuffer.
    final byte[]
    Gets a byte array of the buffered CommandAPDU.
    byte
    Gets the CLA byte value.
    byte
    Gets the INS byte value.
    int
    Gets the length of data field (LC).
    int
    Gets the expected length of ResponseAPDU (LE).
    final int
    Gets the length of the buffered CommandAPDU.
    byte
    Gets the first parameter (P1) byte value.
    byte
    Gets the second parameter (P2) byte value.
    final void
    setByte(int index, byte value)
    Sets the byte value at the specified offset in the apduBuffer.
    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 Details

    • CLA

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

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

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

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

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

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

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

    • 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:
    • 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:
  • Method Details

    • 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:
    • 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:
    • 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:
    • 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:
    • 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:
    • toString

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