APIdia Launch Partner
Top Description Fields Constructors Methods
javax.smartcardio

public final Class CommandAPDU

extends Object
implements Serializable
Class Inheritance
All Implemented Interfaces
java.io.Serializable
Imports
java.util.Arrays, java.nio.ByteBuffer

A command APDU following the structure defined in ISO/IEC 7816-4. It consists of a four byte header and a conditional body of variable length. This class does not attempt to verify that the APDU encodes a semantically valid command.

Note that when the expected length of the response APDU is specified in the constructors, the actual length (Ne) must be specified, not its encoded form (Le). Similarly, getNe returns the actual value Ne. In other words, a value of 0 means "no data in the response APDU" rather than "maximum length."

This class supports both the short and extended forms of length encoding for Ne and Nc. However, note that not all terminals and Smart Cards are capable of accepting APDUs that use the extended form.

For the header bytes CLA, INS, P1, and P2 the Java type int is used to represent the 8 bit unsigned values. In the constructors, only the 8 lowest bits of the int value specified by the application are significant. The accessor methods always return the byte as an unsigned value between 0 and 255.

Instances of this class are immutable. Where data is passed in or out via byte arrays, defensive cloning is performed.

Authors
Andreas Sterbenz, JSR 268 Expert Group
Since
1.6
See Also
ResponseAPDU, CardChannel.transmit

Field Summary

Modifier and TypeField and Description
private byte[]
private transient int
private static final int
private transient int
private transient int
private static final long

Constructor Summary

AccessConstructor and Description
public
CommandAPDU(byte[]
the complete command APDU
apdu
)

Constructs a CommandAPDU from a byte array containing the complete APDU contents (header and body).

public
CommandAPDU(byte[]
the complete command APDU
apdu
,
int
the offset in the byte array at which the apdu data begins
apduOffset
,
int
the length of the APDU
apduLength
)

Constructs a CommandAPDU from a byte array containing the complete APDU contents (header and body).

public
CommandAPDU(ByteBuffer
the ByteBuffer containing the complete APDU
apdu
)

Creates a CommandAPDU from the ByteBuffer containing the complete APDU contents (header and body).

public
CommandAPDU(int
the class byte CLA
cla
,
int
the instruction byte INS
ins
,
int
the parameter byte P1
p1
,
int
the parameter byte P2
p2
)

Constructs a CommandAPDU from the four header bytes.

public
CommandAPDU(int
the class byte CLA
cla
,
int
the instruction byte INS
ins
,
int
the parameter byte P1
p1
,
int
the parameter byte P2
p2
,
int
the maximum number of expected data bytes in a response APDU
ne
)

Constructs a CommandAPDU from the four header bytes and the expected response data length.

public
CommandAPDU(int
the class byte CLA
cla
,
int
the instruction byte INS
ins
,
int
the parameter byte P1
p1
,
int
the parameter byte P2
p2
,
byte[]
the byte array containing the data bytes of the command body
data
)

Constructs a CommandAPDU from the four header bytes and command data.

public
CommandAPDU(int
the class byte CLA
cla
,
int
the instruction byte INS
ins
,
int
the parameter byte P1
p1
,
int
the parameter byte P2
p2
,
byte[]
the byte array containing the data bytes of the command body
data
,
int
the offset in the byte array at which the data bytes of the command body begin
dataOffset
,
int
the number of the data bytes in the command body
dataLength
)

Constructs a CommandAPDU from the four header bytes and command data.

public
CommandAPDU(int
the class byte CLA
cla
,
int
the instruction byte INS
ins
,
int
the parameter byte P1
p1
,
int
the parameter byte P2
p2
,
byte[]
the byte array containing the data bytes of the command body
data
,
int
the maximum number of expected data bytes in a response APDU
ne
)

Constructs a CommandAPDU from the four header bytes, command data, and expected response data length.

public
CommandAPDU(int
the class byte CLA
cla
,
int
the instruction byte INS
ins
,
int
the parameter byte P1
p1
,
int
the parameter byte P2
p2
,
byte[]
the byte array containing the data bytes of the command body
data
,
int
the offset in the byte array at which the data bytes of the command body begin
dataOffset
,
int
the number of the data bytes in the command body
dataLength
,
int
the maximum number of expected data bytes in a response APDU
ne
)

Constructs a CommandAPDU from the four header bytes, command data, and expected response data length.

Method Summary

Modifier and TypeMethod and Description
private static int
arrayLength(byte[] b)

private void
checkArrayBounds(byte[] b, int ofs, int len)

public boolean

Returns:

true if the specified object is equal to this command APDU
equals
(Object
the object to be compared for equality with this command APDU
obj
)

Overrides java.lang.Object.equals.

Compares the specified object with this command APDU for equality.

public byte[]

Returns:

a copy of the bytes in this APDU.
getBytes
()

Returns a copy of the bytes in this APDU.

public int

Returns:

the value of the class byte CLA.
getCLA
()

Returns the value of the class byte CLA.

public byte[]

Returns:

a copy of the data bytes in the command body or the empty byte array if this APDU has no body.
getData
()

Returns a copy of the data bytes in the command body.

public int

Returns:

the value of the instruction byte INS.
getINS
()

Returns the value of the instruction byte INS.

public int

Returns:

the number of data bytes in the command body or 0 if this APDU has no body.
getNc
()

Returns the number of data bytes in the command body (Nc) or 0 if this APDU has no body.

public int

Returns:

the maximum number of expected data bytes in a response APDU.
getNe
()

Returns the maximum number of expected data bytes in a response APDU (Ne).

public int

Returns:

the value of the parameter byte P1.
getP1
()

Returns the value of the parameter byte P1.

public int

Returns:

the value of the parameter byte P2.
getP2
()

Returns the value of the parameter byte P2.

public int

Returns:

the hash code value for this command APDU.
hashCode
()

Overrides java.lang.Object.hashCode.

Returns the hash code value for this command APDU.

private void
parse()

Command APDU encoding options: case 1: |CLA|INS|P1 |P2 | len = 4 case 2s: |CLA|INS|P1 |P2 |LE | len = 5 case 3s: |CLA|INS|P1 |P2 |LC |...BODY...| len = 6..260 case 4s: |CLA|INS|P1 |P2 |LC |...BODY...|LE | len = 7..261 case 2e: |CLA|INS|P1 |P2 |00 |LE1|LE2| len = 7 case 3e: |CLA|INS|P1 |P2 |00 |LC1|LC2|...BODY...| len = 8..65542 case 4e: |CLA|INS|P1 |P2 |00 |LC1|LC2|...BODY...|LE1|LE2| len =10..65544 LE, LE1, LE2 may be 0x00.

private void
private void
setHeader(int cla, int ins, int p1, int p2)

public String

Returns:

a String representation of this command APDU.
toString
()

Overrides java.lang.Object.toString.

Returns a string representation of this command APDU.

Inherited from java.lang.Object:
clonefinalizegetClassnotifynotifyAllwaitwaitwait

Field Detail

apduback to summary
private byte[] apdu
dataOffsetback to summary
private transient int dataOffset
MAX_APDU_SIZEback to summary
private static final int MAX_APDU_SIZE
ncback to summary
private transient int nc
neback to summary
private transient int ne
serialVersionUIDback to summary
private static final long serialVersionUID

Constructor Detail

CommandAPDUback to summary
public CommandAPDU(byte[] apdu)

Constructs a CommandAPDU from a byte array containing the complete APDU contents (header and body).

Note that the apdu bytes are copied to protect against subsequent modification.

Parameters
apdu:byte[]

the complete command APDU

Exceptions
NullPointerException:
if apdu is null
IllegalArgumentException:
if apdu does not contain a valid command APDU
CommandAPDUback to summary
public CommandAPDU(byte[] apdu, int apduOffset, int apduLength)

Constructs a CommandAPDU from a byte array containing the complete APDU contents (header and body). The APDU starts at the index apduOffset in the byte array and is apduLength bytes long.

Note that the apdu bytes are copied to protect against subsequent modification.

Parameters
apdu:byte[]

the complete command APDU

apduOffset:int

the offset in the byte array at which the apdu data begins

apduLength:int

the length of the APDU

Exceptions
NullPointerException:
if apdu is null
IllegalArgumentException:
if apduOffset or apduLength are negative or if apduOffset + apduLength are greater than apdu.length, or if the specified bytes are not a valid APDU
CommandAPDUback to summary
public CommandAPDU(ByteBuffer apdu)

Creates a CommandAPDU from the ByteBuffer containing the complete APDU contents (header and body). The buffer's position must be set to the start of the APDU, its limit to the end of the APDU. Upon return, the buffer's position is equal to its limit; its limit remains unchanged.

Note that the data in the ByteBuffer is copied to protect against subsequent modification.

Parameters
apdu:ByteBuffer

the ByteBuffer containing the complete APDU

Exceptions
NullPointerException:
if apdu is null
IllegalArgumentException:
if apdu does not contain a valid command APDU
CommandAPDUback to summary
public CommandAPDU(int cla, int ins, int p1, int p2)

Constructs a CommandAPDU from the four header bytes. This is case 1 in ISO 7816, no command body.

Parameters
cla:int

the class byte CLA

ins:int

the instruction byte INS

p1:int

the parameter byte P1

p2:int

the parameter byte P2

CommandAPDUback to summary
public CommandAPDU(int cla, int ins, int p1, int p2, int ne)

Constructs a CommandAPDU from the four header bytes and the expected response data length. This is case 2 in ISO 7816, empty command data field with Ne specified. If Ne is 0, the APDU is encoded as ISO 7816 case 1.

Parameters
cla:int

the class byte CLA

ins:int

the instruction byte INS

p1:int

the parameter byte P1

p2:int

the parameter byte P2

ne:int

the maximum number of expected data bytes in a response APDU

Exceptions
IllegalArgumentException:
if ne is negative or greater than 65536
CommandAPDUback to summary
public CommandAPDU(int cla, int ins, int p1, int p2, byte[] data)

Constructs a CommandAPDU from the four header bytes and command data. This is case 3 in ISO 7816, command data present and Ne absent. The value Nc is taken as data.length. If data is null or its length is 0, the APDU is encoded as ISO 7816 case 1.

Note that the data bytes are copied to protect against subsequent modification.

Parameters
cla:int

the class byte CLA

ins:int

the instruction byte INS

p1:int

the parameter byte P1

p2:int

the parameter byte P2

data:byte[]

the byte array containing the data bytes of the command body

Exceptions
IllegalArgumentException:
if data.length is greater than 65535
CommandAPDUback to summary
public CommandAPDU(int cla, int ins, int p1, int p2, byte[] data, int dataOffset, int dataLength)

Constructs a CommandAPDU from the four header bytes and command data. This is case 3 in ISO 7816, command data present and Ne absent. The value Nc is taken as dataLength. If dataLength is 0, the APDU is encoded as ISO 7816 case 1.

Note that the data bytes are copied to protect against subsequent modification.

Parameters
cla:int

the class byte CLA

ins:int

the instruction byte INS

p1:int

the parameter byte P1

p2:int

the parameter byte P2

data:byte[]

the byte array containing the data bytes of the command body

dataOffset:int

the offset in the byte array at which the data bytes of the command body begin

dataLength:int

the number of the data bytes in the command body

Exceptions
NullPointerException:
if data is null and dataLength is not 0
IllegalArgumentException:
if dataOffset or dataLength are negative or if dataOffset + dataLength are greater than data.length or if dataLength is greater than 65535
CommandAPDUback to summary
public CommandAPDU(int cla, int ins, int p1, int p2, byte[] data, int ne)

Constructs a CommandAPDU from the four header bytes, command data, and expected response data length. This is case 4 in ISO 7816, command data and Ne present. The value Nc is taken as data.length if data is non-null and as 0 otherwise. If Ne or Nc are zero, the APDU is encoded as case 1, 2, or 3 per ISO 7816.

Note that the data bytes are copied to protect against subsequent modification.

Parameters
cla:int

the class byte CLA

ins:int

the instruction byte INS

p1:int

the parameter byte P1

p2:int

the parameter byte P2

data:byte[]

the byte array containing the data bytes of the command body

ne:int

the maximum number of expected data bytes in a response APDU

Exceptions
IllegalArgumentException:
if data.length is greater than 65535 or if ne is negative or greater than 65536
CommandAPDUback to summary
public CommandAPDU(int cla, int ins, int p1, int p2, byte[] data, int dataOffset, int dataLength, int ne)

Constructs a CommandAPDU from the four header bytes, command data, and expected response data length. This is case 4 in ISO 7816, command data and Le present. The value Nc is taken as dataLength. If Ne or Nc are zero, the APDU is encoded as case 1, 2, or 3 per ISO 7816.

Note that the data bytes are copied to protect against subsequent modification.

Parameters
cla:int

the class byte CLA

ins:int

the instruction byte INS

p1:int

the parameter byte P1

p2:int

the parameter byte P2

data:byte[]

the byte array containing the data bytes of the command body

dataOffset:int

the offset in the byte array at which the data bytes of the command body begin

dataLength:int

the number of the data bytes in the command body

ne:int

the maximum number of expected data bytes in a response APDU

Exceptions
NullPointerException:
if data is null and dataLength is not 0
IllegalArgumentException:
if dataOffset or dataLength are negative or if dataOffset + dataLength are greater than data.length, or if ne is negative or greater than 65536, or if dataLength is greater than 65535

Method Detail

arrayLengthback to summary
private static int arrayLength(byte[] b)
checkArrayBoundsback to summary
private void checkArrayBounds(byte[] b, int ofs, int len)
equalsback to summary
public boolean equals(Object obj)

Overrides java.lang.Object.equals.

Compares the specified object with this command APDU for equality. Returns true if the given object is also a CommandAPDU and its bytes are identical to the bytes in this CommandAPDU.

Parameters
obj:Object

the object to be compared for equality with this command APDU

Returns:boolean

true if the specified object is equal to this command APDU

getBytesback to summary
public byte[] getBytes()

Returns a copy of the bytes in this APDU.

Returns:byte[]

a copy of the bytes in this APDU.

getCLAback to summary
public int getCLA()

Returns the value of the class byte CLA.

Returns:int

the value of the class byte CLA.

getDataback to summary
public byte[] getData()

Returns a copy of the data bytes in the command body. If this APDU as no body, this method returns a byte array with length zero.

Returns:byte[]

a copy of the data bytes in the command body or the empty byte array if this APDU has no body.

getINSback to summary
public int getINS()

Returns the value of the instruction byte INS.

Returns:int

the value of the instruction byte INS.

getNcback to summary
public int getNc()

Returns the number of data bytes in the command body (Nc) or 0 if this APDU has no body. This call is equivalent to getData().length.

Returns:int

the number of data bytes in the command body or 0 if this APDU has no body.

getNeback to summary
public int getNe()

Returns the maximum number of expected data bytes in a response APDU (Ne).

Returns:int

the maximum number of expected data bytes in a response APDU.

getP1back to summary
public int getP1()

Returns the value of the parameter byte P1.

Returns:int

the value of the parameter byte P1.

getP2back to summary
public int getP2()

Returns the value of the parameter byte P2.

Returns:int

the value of the parameter byte P2.

hashCodeback to summary
public int hashCode()

Overrides java.lang.Object.hashCode.

Returns the hash code value for this command APDU.

Returns:int

the hash code value for this command APDU.

parseback to summary
private void parse()

Command APDU encoding options: case 1: |CLA|INS|P1 |P2 | len = 4 case 2s: |CLA|INS|P1 |P2 |LE | len = 5 case 3s: |CLA|INS|P1 |P2 |LC |...BODY...| len = 6..260 case 4s: |CLA|INS|P1 |P2 |LC |...BODY...|LE | len = 7..261 case 2e: |CLA|INS|P1 |P2 |00 |LE1|LE2| len = 7 case 3e: |CLA|INS|P1 |P2 |00 |LC1|LC2|...BODY...| len = 8..65542 case 4e: |CLA|INS|P1 |P2 |00 |LC1|LC2|...BODY...|LE1|LE2| len =10..65544 LE, LE1, LE2 may be 0x00. LC must not be 0x00 and LC1|LC2 must not be 0x00|0x00

readObjectback to summary
private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException
setHeaderback to summary
private void setHeader(int cla, int ins, int p1, int p2)
toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Returns a string representation of this command APDU.

Returns:String

a String representation of this command APDU.