com.linuxnet.jpcsc
Class Apdu

java.lang.Object
  extended bycom.linuxnet.jpcsc.Apdu
All Implemented Interfaces:
java.io.Serializable

public class Apdu
extends java.lang.Object
implements java.io.Serializable

The Apdu class encapsulates an APDU. It allows to construct APDUs, convert between byte and string representations, and can be passed to Card.Transmit(). An Apdu instance uses a byte array to store the APDU header and data. This internal buffer is reused whenever a new APDU is constructed by calling one of the set(...) methods. You can incrementally add additional data bytes and the expected length by calling one of the add(...) methods. If the previously allocated APDU buffer is too small for a new APDU to create, reset() can be called to reallocate the internal buffer.

See Also:
Serialized Form

Nested Class Summary
static class Apdu.Format
          The Format class described the format to use for converting APDU strings to buffers and vice versa.
static interface Apdu.Wrapper
          ApduWrapper wraps apdu data before sending to the card.
 
Field Summary
static int HEX_COLON_HEX_FORMAT
          Output format: hex:hex:hex
static int HEX_SPACE_HEX_FORMAT
          Output format: hex space hex space hex
static int HEX_VOID_HEX_FORMAT
          Output format: hexhexhex
 
Constructor Summary
Apdu(byte[] ba)
          Construct APDU by taking given buffer.
Apdu(int len)
          Allocate len bytes for internal APDU buffer.
Apdu(int cla, int ins, int p1, int p2, int p3, byte[] in, int off, int le)
          Construct internal buffer from given data.
 
Method Summary
 void addByte(byte b)
          Add a data byte to the APDU.
 void addByte(int b)
          Add a data byte to the APDU.
 void addLe(int le)
          Add expected length of data.
 void addString(java.lang.String s)
          Add a string representing data to the APDU data area.
static java.lang.String ba2s(byte[] ba)
          Return string representation for byte array.
static java.lang.String ba2s(byte[] ba, Apdu.Format f)
          Return string representation for byte array.
static java.lang.String ba2s(byte[] ba, int off, int len)
          Return string representation for byte array range.
static java.lang.String ba2s(byte[] ba, int off, int len, Apdu.Format f)
          Return string representation for byte array range according to given format.
static byte[] construct(int cla, int ins, int p1, int p2, int p3, byte[] in, int off, int le)
          Construct APDU and return it in a newly created array.
 void getBytes(byte[] ba, int off)
          Copy the currently defined APDU to given buffer.
static void main(java.lang.String[] args)
          Test the convert methods.
 void reset()
          Reset the buffer.
 void reset(int len)
          Reset Apdu, reallocate buffer.
static byte[] s2ba(java.lang.String s)
          Convert string to a byte array.
static int s2ba(java.lang.String s, byte[] ba, int off)
          Convert string to a byte array.
 void set(byte[] ba)
          Copy the given APDU to the internal buffer.
 void set(byte[] ba, int off, int len)
          Copy the given APDU (range from a byte array) to the internal buffer.
 void set(int cla, int ins, int p1, int p2)
          Create APDU payload from given data.
 void set(int cla, int ins, int p1, int p2, int p3, byte[] in, int off, int le)
          Create an APDU from the given data.
 void set(java.lang.String s)
          Set APDU data from given string.
 java.lang.String toString()
          Return string representation of APDU.
 java.lang.String toString(Apdu.Format format)
          Return string representation of APDU according to given format specification.
 void wrapWith(Apdu.Wrapper wrapper)
          Wrap the apdu buffer.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

HEX_SPACE_HEX_FORMAT

public static final int HEX_SPACE_HEX_FORMAT
Output format: hex space hex space hex

See Also:
Constant Field Values

HEX_COLON_HEX_FORMAT

public static final int HEX_COLON_HEX_FORMAT
Output format: hex:hex:hex

See Also:
Constant Field Values

HEX_VOID_HEX_FORMAT

public static final int HEX_VOID_HEX_FORMAT
Output format: hexhexhex

See Also:
Constant Field Values
Constructor Detail

Apdu

public Apdu(byte[] ba)
Construct APDU by taking given buffer. Length of the internal buffer will be the same as the length of the given byte array.


Apdu

public Apdu(int len)
Allocate len bytes for internal APDU buffer.


Apdu

public Apdu(int cla,
            int ins,
            int p1,
            int p2,
            int p3,
            byte[] in,
            int off,
            int le)
Construct internal buffer from given data.

See Also:
construct(int, int, int, int, int, byte[], int, int)
Method Detail

construct

public static byte[] construct(int cla,
                               int ins,
                               int p1,
                               int p2,
                               int p3,
                               byte[] in,
                               int off,
                               int le)
Construct APDU and return it in a newly created array.

Parameters:
cla - CLA byte of APDU (byte #0)
ins - INS byte of APDU (byte #1)
p1 - P1 byte of APDU (byte #2)
p2 - P2 byte of APDU (byte #3)
p3 - P3 or LC byte of APDU (byte #4). This byte is omitted if parameter has value -1, otherwise it contains number of bytes in buffer in sent to card.
in - byte array containing command data of APDU. If null, p3 is omitted.
off - offset starting at which data sending is to begin
le - LE byte of APDU (appended to APDU). This byte is omitted if parameter has value -1.
Returns:
response APDU as a byte array

reset

public final void reset()
Reset the buffer.


reset

public final void reset(int len)
Reset Apdu, reallocate buffer.


getBytes

public final void getBytes(byte[] ba,
                           int off)
Copy the currently defined APDU to given buffer.


set

public final void set(byte[] ba)
Copy the given APDU to the internal buffer.


set

public final void set(byte[] ba,
                      int off,
                      int len)
Copy the given APDU (range from a byte array) to the internal buffer.


set

public final void set(int cla,
                      int ins,
                      int p1,
                      int p2,
                      int p3,
                      byte[] in,
                      int off,
                      int le)
Create an APDU from the given data.


set

public final void set(int cla,
                      int ins,
                      int p1,
                      int p2)
Create APDU payload from given data. P3 is initialized to zero.

Parameters:
cla - CLA byte of APDU (byte #0)
ins - INS byte of APDU (byte #1)
p1 - P1 byte of APDU (byte #2)
p2 - P2 byte of APDU (byte #3)

set

public final void set(java.lang.String s)
Set APDU data from given string. The APDU buffer size must have been allocated accordingly before.

See Also:
s2ba() for the possible string format

addByte

public final void addByte(int b)
Add a data byte to the APDU. The LC byte of the APDU is updated accordingly. The payload has to be already defined.


addByte

public final void addByte(byte b)
Add a data byte to the APDU. The LC byte of the APDU is updated accordingly. The payload has to be already defined.


addString

public final void addString(java.lang.String s)
Add a string representing data to the APDU data area. The payload has to be already defined.

See Also:
s2ba() for the possible string format

addLe

public final void addLe(int le)
Add expected length of data. This byte is omitted if parameter has value -1, otherwise it contains number of bytes expected from card.


wrapWith

public void wrapWith(Apdu.Wrapper wrapper)
              throws java.rmi.RemoteException
Wrap the apdu buffer.

Parameters:
wrapper -
Throws:
java.rmi.RemoteException

toString

public java.lang.String toString()
Return string representation of APDU.


toString

public java.lang.String toString(Apdu.Format format)
Return string representation of APDU according to given format specification.


ba2s

public static java.lang.String ba2s(byte[] ba)
Return string representation for byte array.


ba2s

public static java.lang.String ba2s(byte[] ba,
                                    int off,
                                    int len)
Return string representation for byte array range.


ba2s

public static java.lang.String ba2s(byte[] ba,
                                    Apdu.Format f)
Return string representation for byte array.


ba2s

public static java.lang.String ba2s(byte[] ba,
                                    int off,
                                    int len,
                                    Apdu.Format f)
Return string representation for byte array range according to given format.


s2ba

public static byte[] s2ba(java.lang.String s)
Convert string to a byte array. Notations supported are 0x00:0xb2 or 0x0:0xb2 or 00:b2 or 00:ba:|symbolic|:... or 00b2 or a0b0|symbolic|a0b0 or 0x00 0xb2 or 0x0 0xb2 or 0x0 0xb2 |symbolic| ...


s2ba

public static int s2ba(java.lang.String s,
                       byte[] ba,
                       int off)
Convert string to a byte array. Notations supported are 0x00:0xb2 or 0x0:0xb2 or 00:b2 or 00:ba for data bytes. Additionally, | can be used to insert ASCII characters into the byte stream, e.g. 01|abc|01 -> 0161626301 Another operator supported is #(...) where at the position of # the length of the byte string between ( and ) is inserted. Examples: #(5566) -> 025566, #(3344)#(55) -> 0233440155, #(11#(22)) -> 03110122, #(11#(|ZZ|)) -> 0411025A5A.


main

public static void main(java.lang.String[] args)
Test the convert methods.