FileDocCategorySizeDatePackage
AclFileSystem.javaAPI DocphoneME MR2 API (J2ME)5698Wed May 02 18:00:38 BST 2007com.sun.satsa.acl

AclFileSystem

public class AclFileSystem extends FileSystemAbstract
This class provides interface to card file system.

Fields Summary
private static final int
CARD_BUFFER
Constant defines size of the card buffer
private static final int
P1_P2
Constant defines parameters P1, P2 in the apdu command
private byte[]
ATR
Internal buffer for ATR
private int
unitSize
variable defines the unit size in the INS_READ command
public DIRF
DIR
DIR file object
Constructors Summary
public AclFileSystem(Connection apdu)
Constructs new AclFileSystem object.

param
apdu connection to be used by this object.

                      
         
        super(apdu);
        ATR = apdu.getATR();
        /* Get FCI from previous operation */
        byte[] prevFCI = apdu.getFCI();
        /*
         * It is assumed that in case of work with application the FCI
         * contains successfull result of the selectApplication operation,
         * i.e. 0x9000. In case of work with the card file system the FCI
         * array will contain actual FCI of the master file and length of the
         * array will be > 2
         */
        if (prevFCI.length > 2) {
            parseATR();
            apdu.setUnitSize(unitSize);
            try {
                DIR = new DIRF(this);
                DIR.load();
            } catch (TLVException te) {
                throw new IOException("Wrong file TLV structure");
            }
            catch (IOException ie) {
                throw new IOException("Wrong DIR file");
            }
        } else {
            apdu.setUnitSize(unitSize);
        }
        apdu.setCLAbyte((byte)0);
    
Methods Summary
private voidparseATR()
Parses the ATR. At the moment defines a unit size only

        for (int i = ATR.length - 1; i > 0; i--) {
            if (((ATR[i] & 0xF0) == 0x70) &&
                ((ATR[i] & 0x0F) >= 2)) {
                unitSize = (1 << (ATR[i+2] & 0x07)) / 2;
                break;
            }
        }
    
public byte[]readData(int offset, int length, int fileOffset)
Reads part of selected file.

param
offset the offset into data buffer where data should be placed
param
length read data length (is not used in this class)
param
fileOffset file data offset
throws
IOException if IO error occurs
return
data byte array of the data

        Vector v = new Vector();
        int len = CARD_BUFFER;
        int readLength = 0;
        while (true) {
            byte[] result = apdu.resetCommand().
                    sendCommand(INS_READ, fileOffset/unitSize, len, false);
            int lastSW = ((result[result.length - 2] & 0xff) << 8) |
                     (result[result.length - 1] & 0xff);
            if (lastSW != 0x9000) {
               break;
            }
            v.addElement(result);
            offset += len;
            fileOffset += len;
            readLength += result.length - 2;
            if (result.length - 2 < len) {
                break;
            }
        }
        byte[] data = new byte[readLength];
        offset = 0;
        for (int i = 0; i < v.size(); i++) {
            byte[] r = (byte[])v.elementAt(i);
            System.arraycopy(r, 0, data, offset, r.length - 2);
            offset += r.length - 2;
        }
        currentFileSize = readLength;
        return data;
    
public byte[]readFile()
Reads the current EF.

return
array that contains EF body.
throws
IOException if IO error occurs

        byte[] data = readData(0, currentFileSize, 0);
        return data;
    
public voidselect(short id)
Selects file by ID.

param
id file ID
throws
IOException if IOError occurs


        byte[] data = apdu.resetCommand().
                           putShort(id).
                           sendCommand(INS_SELECT, P1_P2);

        isEFSelected = false;
        currentFileSize = 0;

        if (data.length == 2) {
            return;       // FCI is empty
        }

        /* parse FCI */
        if (Utils.getU2(data, data.length - 2) != 0x9000) {
            throw new IOException("Bad FCI");
        }

        isEFSelected = true;