com.linuxnet.jpcsc
Class Apdu

java.lang.Object
  extended bycom.linuxnet.jpcsc.Apdu

public class Apdu
extends java.lang.Object

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 bytearray 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.


Nested Class Summary
static class Apdu.Format
          The Format class described the format to use for converting APDU strings to buffers and vice versa.
 
Field Summary
private  byte[] buf
          The buffer keeping the apdu data.
private static Apdu.Format defaultFormat
          Default format for convert rotuines.
private static java.lang.String digits
          Helper string.
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
private  int len
          The length of the data in the buffer keeping the APDU.
private static int MAX_BRACE_CNT
          Number of parentheses allowed in string specifications of APDUs.
 
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 bytearray.
static java.lang.String ba2s(byte[] ba, Apdu.Format f)
          Return string representation for bytearray.
static java.lang.String ba2s(byte[] ba, int off, int len)
          Return string representation for bytearray range.
static java.lang.String ba2s(byte[] ba, int off, int len, Apdu.Format f)
          Return string representation for bytearray 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.
(package private)  byte[] getBuffer()
          Return internal buffer.
 void getBytes(byte[] ba, int off)
          Copy the currently defined apdu to given buffer.
(package private)  int getLength()
          Return length of apdu in the internal 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 bytearray.
static int s2ba(java.lang.String s, byte[] ba, int off)
          Convert string to a bytearray.
 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 froma bytearray) 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.
 
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

buf

private byte[] buf
The buffer keeping the apdu data.


len

private int len
The length of the data in the buffer keeping the APDU.


defaultFormat

private static Apdu.Format defaultFormat
Default format for convert rotuines.


digits

private static java.lang.String digits
Helper string.


MAX_BRACE_CNT

private static int MAX_BRACE_CNT
Number of parentheses allowed in string specifications of APDUs.

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 bytearray.


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 Apdu.construct().

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.
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.


getBuffer

final byte[] getBuffer()
Return internal buffer. Available to Card.Transmit().


getLength

final int getLength()
Return length of apdu in the internal buffer. Available to Card.Transmit().


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 froma bytearray) 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. See s2ba() for the possible string format. The payload has to be already defined.


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.


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 bytearray.


ba2s

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


ba2s

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


ba2s

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


s2ba

public static byte[] s2ba(java.lang.String s)
Convert string to a bytearray. 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 bytearray. 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.