FileDocCategorySizeDatePackage
ViewHandler.javaAPI DocphoneME MR2 API (J2ME)33005Wed May 02 18:00:40 BST 2007sim.toolkit

ViewHandler

public abstract class ViewHandler extends Object
The ViewHandler class offers basic services and contains basic methods to handle a Simple TLV List, such as in a Terminal Response data field or in a BER-TLV element (Envelope data field or Proactive command). The byte at offset 0 of a handler is the tag of the first Simple TLV.
version
8.3.0
see
ToolkitException

Fields Summary
protected static final byte
BER_TLV_TAG_OFFSET
Offset of BER_TLV_TAG.
protected static final byte
OFFSET_LC
Offset of Lc in the APDU buffer.
protected static final byte
TPUD_OFFSET
Offset of TPDUD.
private static final byte
DEVICE_ID_TAG
Tag of DEVICE_ID field.
private static final byte
DEVICE_ID_LENGTH
Length of DEVICE_ID field.
private static final byte
ADDRESS_TAG
Tag of ADDRESS field.
private static final byte
SMS_TPDU_TAG
Tag of SMS_TPDU.
short
currentTLVOffset
Offset of first occurrence of TLV.
short
firstTLVOffset
Offset of first occurrence of TLV.
public static AccessSAT
SATAccessor
Reference to SAT Accessor.
public static ToolkitInterface
currentTI
Reference to TI which is now serviced.
Constructors Summary
ViewHandler()
Constructor.

    
          
     
ViewHandler(byte[] buffer, short offset, short length)
Builds a new ViewHandler object.

param
buffer a reference to the TLV buffer
param
offset the position in the TLV buffer
param
length the length of the TLV buffer
exception
NullPointerException if buffer is null
exception
ArrayIndexOutOfBoundsException if offset or length or both would cause access outside array bounds

    
Methods Summary
public bytecompareValue(short valueOffset, byte[] compareBuffer, short compareOffset, short compareLength)
Compares the last found TLV element with a buffer.

Notes:

  • If compareOffset or compareLength parameter is negative an ArrayIndexOutOfBoundsException exception is thrown and no compare is performed.
  • If compareOffset+compareLengthis greater than compareBuffer.length, the length of the compareBuffer array an ArrayIndexOutOfBoundsException exception is thrown and no compare is performed.

param
valueOffset the offset of the first byte to compare in the TLV element
param
compareBuffer a reference to the comparison buffer
param
compareOffset the position in the comparison buffer
param
compareLength the length to be compared
return
the result of the comparison as follows:
  • 0 if identical
  • -1 if the first miscomparing byte in simple TLV List is less than that in compareBuffer,
  • 1 if the first miscomparing byte in simple TLV List is greater than that in compareBuffer.
exception
NullPointerException if compareBuffer is null
exception
ArrayIndexOutOfBoundsException if compareValue would cause access of data outside array bounds.
exception
ToolkitException with the following reason codes:
  • HANDLER_NOT_AVAILABLE if the handler is busy
  • UNAVAILABLE_ELEMENT in case of unavailable TLV element
  • OUT_OF_TLV_BOUNDARIES if:
    • valueOffset parameter is negative or
    • valueOffset + compareLength is greater than the length of the current TLV

        byte[] buffer = getAPDUBuffer();
        short Lc = (short)(buffer[OFFSET_LC] & 0xFF);
        short TLVOffset = getLastTLVOffset(buffer, Lc);
        if (TLVOffset >= Lc) {
            ToolkitException.throwIt(ToolkitException.UNAVAILABLE_ELEMENT);
        }
        short TLVLength = (short)(buffer[(short)(TLVOffset + 1)] & 0xFF);
        if (valueOffset > TLVLength || valueOffset < 0) {
            ToolkitException.throwIt(ToolkitException.OUT_OF_TLV_BOUNDARIES);
        }
        return Util.arrayCompare(buffer, 
                                (short)(currentTLVOffset + 2 + valueOffset), 
                                compareBuffer, compareOffset, compareLength);
    
public shortcopy(byte[] dstBuffer, short dstOffset, short dstLength)
Copies the simple TLV list contained in the handler to the destination byte array.

Notes:

  • If dstOffset or dstLength parameter is negative an ArrayIndexOutOfBoundsException exception is thrown and no copy is performed.
  • If dstOffset+dstLength is greater than dstBuffer.length, the length of the dstBuffer array an ArrayIndexOutOfBoundsException exception is thrown and no copy is performed.

param
dstBuffer a reference to the destination buffer
param
dstOffset the position in the destination buffer
param
dstLength the data length to be copied
return
dstOffset+dstLength
exception
NullPointerException if dstBuffer is null
exception
ArrayIndexOutOfBoundsException if copy would cause access of data outside array bounds.
exception
ToolkitException with the following reason codes:
  • HANDLER_NOT_AVAILABLE if the handler is busy
  • OUT_OF_TLV_BOUNDARIES if dstLength is grater than the length of the simple TLV List.

        byte[] buffer = getAPDUBuffer();
        short TLVLength = 
            (short)(buffer[(short)(currentTLVOffset + 1)] & 0xFF);
        if (TLVLength < dstLength) {
            // if length is greater than the TLV length itself, then 
            // throw exception
            ToolkitException.throwIt(ToolkitException.OUT_OF_TLV_BOUNDARIES);
        }
        // copy the current TLV list
        Util.arrayCopy(buffer, (short)(currentTLVOffset+2), dstBuffer, 
                        dstOffset, dstLength);
        return (short)(dstOffset + dstLength);
    
public shortcopyValue(short valueOffset, byte[] dstBuffer, short dstOffset, short dstLength)
Copies a part of the last TLV element which has been found, into a destination buffer.

Notes:

  • If dstOffset or dstLength parameter is negative an ArrayIndexOutOfBoundsException exception is thrown and no copy is performed.
  • If dstOffset+dstLength is greater than dstBuffer.length, the length of the dstBuffer array an ArrayIndexOutOfBoundsException exception is thrown and no copy is performed.

param
valueOffset the offset of the first byte in the source TLV element
param
dstBuffer a reference to the destination buffer
param
dstOffset the position in the destination buffer
param
dstLength the data length to be copied
return
dstOffset+dstLength
exception
NullPointerException if dstBuffer is null
exception
ArrayIndexOutOfBoundsException if copyValue would cause access of data outside array bounds.
exception
ToolkitException with the following reason codes:
  • HANDLER_NOT_AVAILABLE if the handler is busy
  • UNAVAILABLE_ELEMENT in case of unavailable TLV element
  • OUT_OF_TLV_BOUNDARIES if:
    • valueOffset parameter is negative or
    • valueOffset + dstLength is greater than the length of the current TLV

        byte[] buffer = getAPDUBuffer();
        short Lc = (short)(buffer[OFFSET_LC] & 0xFF);
        short TLVOffset = getLastTLVOffset(buffer, Lc);
        if (TLVOffset >= Lc) {
            ToolkitException.throwIt(ToolkitException.UNAVAILABLE_ELEMENT);
        }
        short TLVLength = (short)(buffer[(short)(TLVOffset + 1)] & 0xFF);
        if (valueOffset > TLVLength || valueOffset < 0) {
            ToolkitException.throwIt(ToolkitException.OUT_OF_TLV_BOUNDARIES);
        }
        Util.arrayCopy(buffer, 
                        (short)(currentTLVOffset + 2 + valueOffset), 
                        dstBuffer, dstOffset, dstLength);
        return buffer[(short)(TLVOffset + 2 + valueOffset)]; 
    
public bytefindAndCompareValue(byte tag, byte[] compareBuffer, short compareOffset)
Looks for the first occurrence of a TLV element from beginning of a TLV list and compare its value with a buffer. If no TLV element is found, the UNAVAILABLE_ELEMENT exception is thrown. If the method is successful then the corresponding TLV becomes current, else no TLV is selected. This search method is Comprehension Required flag independent.

Notes:

  • If compareOffset parameter is negative or compareOffset is greater than compareBuffer.length, the length of the compareBuffer array an ArrayIndexOutOfBoundsException exception is thrown and no find is performed.

param
tag the tag of the TLV element to search
param
compareBuffer a reference to the comparison buffer
param
compareOffset the position in the comparison buffer
return
the result of the comparison as follows:
  • 0 if identical
  • -1 if the first miscomparing byte in simple TLV is less than that in compareBuffer,
  • 1 if the first miscomparing byte in simple TLV is greater than that in compareBuffer.
exception
NullPointerException if compareBuffer is null
exception
ArrayIndexOutOfBoundsException if findAndCompareValue would cause access of data outside array bounds.
exception
ToolkitException with the following reason codes:
  • HANDLER_NOT_AVAILABLE if the handler is busy
  • UNAVAILABLE_ELEMENT in case of unavailable TLV element

        byte[] buffer = getAPDUBuffer();
        short Lc = (short)(buffer[OFFSET_LC] & 0xFF);
        short TLVOffset = getTLVOffset(buffer, tag, Lc, (byte)1);
        
        if (TLVOffset >= Lc) {
            // TLV not found
            ToolkitException.throwIt(ToolkitException.UNAVAILABLE_ELEMENT);
        }
        // this is the current TLV
        currentTLVOffset = TLVOffset;
        short length = buffer[(short)(TLVOffset+1)];
        return Util.arrayCompare(buffer, (short)(TLVOffset+2), compareBuffer,
                                compareOffset, length);
    
public bytefindAndCompareValue(byte tag, byte occurrence, short valueOffset, byte[] compareBuffer, short compareOffset, short compareLength)
Looks for the indicated occurrence of a TLV element from the beginning of a TLV list and compare its value with a buffer. If no TLV element is found, the UNAVAILABLE_ELEMENT exception is thrown. If the method is successful then the corresponding TLV becomes current, else no TLV is selected. This search method is Comprehension Required flag independent.

Notes:

  • If compareOffset or compareLength parameter is negative an ArrayIndexOutOfBoundsException exception is thrown and no find and compare is performed.
  • If compareOffset+compareLength is greater than compareBuffer.length, the length of the compareBuffer array an ArrayIndexOutOfBoundsException exception is thrown and no find and compare is performed.

param
tag the tag of the TLV element to search
param
occurrence the occurrence number of the TLV element (1 for the first, 2 for the second...)
param
valueOffset the offset of the first byte in the source TLV element
param
compareBuffer a reference to the comparison buffer
param
compareOffset the position in the comparison buffer
param
compareLength the length to be compared
return
the result of the comparison as follows:
  • 0 if identical
  • -1 if the first miscomparing byte in simple TLV is less than that in compareBuffer,
  • 1 if the first miscomparing byte in simple TLV is greater than that in compareBuffer.
exception
NullPointerException if compareBuffer is null
exception
ArrayIndexOutOfBoundsException if findAndCompareValue would cause access of data outside array bounds.
exception
ToolkitException with the following reason codes:
  • HANDLER_NOT_AVAILABLE if the handler is busy
  • UNAVAILABLE_ELEMENT in case of unavailable TLV element
  • OUT_OF_TLV_BOUNDARIES if:
    • valueOffset parameter is negative or
    • valueOffset + compareLength is greater than the length of the current TLV
  • BAD_INPUT_PARAMETER if an input parameter is not valid (e.g. occurrence = 0)

        byte[] buffer = getAPDUBuffer();
        short Lc = (short)(buffer[OFFSET_LC] & 0xFF);
        short TLVOffset = getTLVOffset(buffer, tag, Lc, occurrence);
        
        if (TLVOffset >= Lc) {
            // TLV not found
            ToolkitException.throwIt(ToolkitException.UNAVAILABLE_ELEMENT);
        }
        // this is the current TLV
        currentTLVOffset = TLVOffset;
        short length = buffer[(short)(TLVOffset+1)];
        if ((valueOffset < 0) || (short)
	    (valueOffset + compareLength) > length) {
            ToolkitException.throwIt(ToolkitException.OUT_OF_TLV_BOUNDARIES);
        }
        return Util.arrayCompare(buffer, (short)(TLVOffset+2+valueOffset), 
                                compareBuffer, compareOffset, compareLength);
    
public shortfindAndCopyValue(byte tag, byte[] dstBuffer, short dstOffset)
Looks for the first occurrence of a TLV element from the beginning of a TLV list and copy its value into a destination buffer. If no TLV element is found, the UNAVAILABLE_ELEMENT exception is thrown. If the method is successful then the corresponding TLV becomes current, else no TLV is selected. This search method is Comprehension Required flag independent.

Notes:

  • If dstOffset parameter is negative or dstOffset is greater than dstBuffer.length, the length of the dstBuffer array an ArrayIndexOutOfBoundsException exception is thrown and no find is performed.

param
tag the tag of the TLV element to search
param
dstBuffer a reference to the destination buffer
param
dstOffset the position in the destination buffer
return
dstOffset + length of the copied value
exception
NullPointerException if dstBuffer is null
exception
ArrayIndexOutOfBoundsException if findAndCopyValue would cause access of data outside array bounds.
exception
ToolkitException with the following reason codes:
  • HANDLER_NOT_AVAILABLE if the handler is busy
  • UNAVAILABLE_ELEMENT in case of unavailable TLV element

        // this method could potentialy use the other 
        // findAndCopyValue() method. The only problem is the length 
        // parameter required by that method
        
        byte[] buffer = getAPDUBuffer();
        short Lc = (short)(buffer[OFFSET_LC] & 0xFF);
        short TLVOffset = getTLVOffset(buffer, tag, Lc, (short)1);
        
        if (TLVOffset >= Lc) {
            // TLV not found
            ToolkitException.throwIt(ToolkitException.UNAVAILABLE_ELEMENT);
        }
        currentTLVOffset = TLVOffset;
        short length = buffer[(short)(TLVOffset+1)];
        Util.arrayCopy(buffer, (short)(TLVOffset+2), dstBuffer, 
                        dstOffset, length);
        return (short)(dstOffset + length);
    
public shortfindAndCopyValue(byte tag, byte occurrence, short valueOffset, byte[] dstBuffer, short dstOffset, short dstLength)
Looks for the indicated occurrence of a TLV element from the beginning of a TLV list and copy its value into a destination buffer. If no TLV element is found, the UNAVAILABLE_ELEMENT exception is thrown. If the method is successful then the corresponding TLV becomes current, else no TLV is selected. This search method is Comprehension Required flag independent.

Notes:

  • If dstOffset or dstLength parameter is negative an ArrayIndexOutOfBoundsException exception is thrown and no copy is performed.
  • If dstOffset+dstLengthis greater than dstBuffer.length, the length of the dstBuffer array an ArrayIndexOutOfBoundsException exception is thrown and no copy is performed.

param
tag the tag of the TLV element to search
param
occurrence the occurrence number of the TLV element (1 for the first, 2 for the second...)
param
valueOffset the offset of the first byte in the source TLV element
param
dstBuffer a reference to the destination buffer
param
dstOffset the position in the destination buffer
param
dstLength the data length to be copied
return
dstOffset + dstLength
exception
NullPointerException if dstBuffer is null
exception
ArrayIndexOutOfBoundsException if findAndCopyValue would cause access of data outside array bounds.
exception
ToolkitException with the following reason codes:
  • HANDLER_NOT_AVAILABLE if the handler is busy
  • UNAVAILABLE_ELEMENT in case of unavailable TLV element
  • OUT_OF_TLV_BOUNDARIES if:
    • valueOffset parameter is negative or
    • valueOffset + dstLength is greater than the length of the current TLV
  • BAD_INPUT_PARAMETER if an input parameter is not valid (e.g. occurrence = 0)

                                                			  
        byte[] buffer = getAPDUBuffer();
        short Lc = (short)(buffer[OFFSET_LC] & 0xFF);
        short TLVOffset = getTLVOffset(buffer, tag, Lc, occurrence);
        
        if (TLVOffset >= Lc) {
            // TLV not found
            ToolkitException.throwIt(ToolkitException.UNAVAILABLE_ELEMENT);
        }
        // this is the current TLV
        currentTLVOffset = TLVOffset;
        short length = buffer[(short)(TLVOffset+1)];
        if ((valueOffset < 0) || (short)(valueOffset + dstLength) > length) {
            ToolkitException.throwIt(ToolkitException.OUT_OF_TLV_BOUNDARIES);
        }
        Util.arrayCopy(buffer, (short)(TLVOffset+2+valueOffset), 
                        dstBuffer, dstOffset, dstLength);
        return (short)(dstOffset + length);

    
public bytefindTLV(byte tag, byte occurrence)
Looks for the indicated occurrence of a TLV element from the beginning of the TLV list (handler buffer). If the method is successful then the corresponding TLV becomes current, else no TLV is selected. This search method is Comprehension Required flag independent.

param
tag the tag of the TLV element to search
param
occurrence the occurrence number of the TLV element (1 for the first, 2 for the second...)
return
result of the method:
  • TLV_NOT_FOUND if the required occurrence of the TLV element does not exist
  • TLV_FOUND_CR_SET if the required occurrence exists and Comprehension Required flag is set
  • TLV_FOUND_CR_NOT_SET if the required occurrence exists and Comprehension Required flag is not set
exception
ToolkitException with the following reason codes:
  • HANDLER_NOT_AVAILABLE if the handler is busy
  • BAD_INPUT_PARAMETER if an input parameter is not valid (e.g. occurrence = 0)

        byte count = 0; // count of occurances
        byte[] buffer = getAPDUBuffer();
        short Lc = (short)(buffer[OFFSET_LC] & 0xFF);
        short TLVOffset = getTLVOffset(buffer, tag, Lc, occurrence);
        if (TLVOffset >= Lc)
            return ToolkitConstants.TLV_NOT_FOUND; // not found
        
        currentTLVOffset = TLVOffset;
        if ((byte)(buffer[TLVOffset] & ToolkitConstants.TAG_SET_CR) == 
                    ToolkitConstants.TAG_SET_CR)
            return ToolkitConstants.TLV_FOUND_CR_SET;
            
        return ToolkitConstants.TLV_FOUND_CR_NOT_SET;
    
public static byte[]getAPDUBuffer(byte[] buffer)
Copies the APDUBuffer content into provided buffer.

param
buffer The buffer
return
Provided buffer filled with APDUBuffer content

        short len = SATAccessor.getAPDUBufferLength();
        
        for (short i = 0; i < len; i++) {
            buffer[i] = SATAccessor.getAPDUBufferByte(i);
        }
        return buffer;
    
public static byte[]getAPDUBuffer()
Copies the APDUBuffer content into current buffer.

return
apdu buffer

        return getAPDUBuffer(currentTI.getAPDUBuffer());
    
protected shortgetLastTLVOffset(byte[] buffer, short Lc)
Helper method for getValue... and copyValue....

param
buffer APDU buffer
param
Lc Length of command data
return
Offset of the tag

        short offset = firstTLVOffset;
        short lastTLVOffset;
        do {
            lastTLVOffset = offset;
            // advance to next TLV
            offset++;
            short length = buffer[offset];
            offset = (short)(offset + length + 1);
        } while (offset < Lc);
        return lastTLVOffset;
    
public shortgetLength()
Returns the length of the TLV list.

return
length in bytes
exception
ToolkitException with the following reason codes:
  • HANDLER_NOT_AVAILABLE if the handler is busy

        byte[] buffer = getAPDUBuffer();
        short length = (short)(buffer[(short)(currentTLVOffset + 1)] & 0xFF);
        return length;
    
protected shortgetTLVOffset(byte[] buffer, byte tag, short Lc, short occurrence)
Helper method for findAndCompareValue.

param
buffer APDU buffer
param
tag What tag we are finding
param
Lc Length of command data
param
occurrence The occurrence number of the TLV element
return
Offset of the tag

        byte count = 0; // count of occurances
        short offset = firstTLVOffset;
        tag = (byte)(tag & 0x7F);
        while (offset < Lc) {
            if ((byte)(buffer[offset] & 0x7F) == tag) {
                count++;
                if (count != occurrence) {
                    continue;
                }
                return offset;
            } else {
                // advance to next TLV
                offset++;
                short length = buffer[offset];
                offset = (short)(offset + length + 1);
            }
        }
        return offset; // not found
    
public bytegetValueByte(short valueOffset)
Gets a byte from the last TLV element which has been found in the handler.

param
valueOffset the offset of the byte to return in the TLV element
return
element value (1 byte)
exception
ToolkitException with the following reason codes:
  • HANDLER_NOT_AVAILABLE if the handler is busy
  • UNAVAILABLE_ELEMENT in case of unavailable TLV element
  • OUT_OF_TLV_BOUNDARIES if valueOffset is out of the current TLV

        byte[] buffer = getAPDUBuffer();
        short Lc = (short)(buffer[OFFSET_LC] & 0xFF);
        short TLVOffset = getLastTLVOffset(buffer, Lc);
        if (TLVOffset >= Lc) {
            ToolkitException.throwIt(ToolkitException.UNAVAILABLE_ELEMENT);
        }
        short TLVLength = (short)(buffer[(short)(TLVOffset + 1)] & 0xFF);
        if (valueOffset > TLVLength) {
            ToolkitException.throwIt(ToolkitException.OUT_OF_TLV_BOUNDARIES);
        }
        // return the byte at offset
        return buffer[(short)(TLVOffset + 2 + valueOffset)]; 
    
public shortgetValueLength()
Gets the binary length of the value field for the last TLV element which has been found in the handler.

return
length of the value field
exception
ToolkitException with the following reason codes:
  • HANDLER_NOT_AVAILABLE if the handler is busy
  • UNAVAILABLE_ELEMENT in case of unavailable TLV element

        byte[] buffer = getAPDUBuffer();
        short Lc = (short)(buffer[OFFSET_LC] & 0xFF);
        short TLVOffset = getLastTLVOffset(buffer, Lc);
        if (TLVOffset >= Lc) {
            ToolkitException.throwIt(ToolkitException.UNAVAILABLE_ELEMENT);
        }
        return (short)(buffer[(short)
			     (TLVOffset + 1)] & 0xFF); // return the length
    
public static voidsetAPDUBuffer(byte[] buffer, short length)
Copies content of provided buffer into the APDUBuffer.

param
buffer The buffer
param
length Length of the buffer

        
        for (short i = 0; i < length; i++) {
            SATAccessor.setAPDUBufferByte(i, buffer[i]);
        }
    
public static voidsetAPDUBuffer(short length)
Copies content of current buffer into the APDUBuffer.

param
length Length of the current buffer

        setAPDUBuffer(currentTI.getAPDUBuffer(), length);
    
public static voidsetOutBufferData(short length)
Sets the data in the out buffer.

param
length Length of data

        setAPDUBuffer(length);
        SATAccessor.setOutBufferData(length);