Class ArrayLogic
- java.lang.Object
-
- javacardx.framework.util.ArrayLogic
-
public final class ArrayLogic extends Object
TheArrayLogicclass contains common utility functions for manipulating arrays of primitive components - byte, short or int. Some of the methods may be implemented as native functions for performance reasons. All the methods inArrayLogicclass are static methods.Some methods of
ArrayLogic, namelyarrayCopyRepack(),arrayCopyRepackNonAtomic()andarrayFillGenericNonAtomic(), refer to the persistence of array objects. The term persistent means that arrays and their values persist from one CAD session to the next, indefinitely. TheJCSystemclass is used to control the persistence and transience of objects.- Since:
- 2.2.2
- See Also:
javacard.framework.JCSystem
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static bytearrayCompareGeneric(Object src, short srcOff, Object dest, short destOff, short length)Compares an array from the specified source array, beginning at the specified position, with the specified position of the destination array from left to right.static shortarrayCopyRepack(Object src, short srcOff, short srcLen, Object dest, short destOff)Copies data from the specified source array, beginning at the specified position, to the specified position of the destination array.static shortarrayCopyRepackNonAtomic(Object src, short srcOff, short srcLen, Object dest, short destOff)Non-atomically copies data from the specified source array, beginning at the specified position, to the specified position of the destination array.static shortarrayFillGeneric(Object theArray, short off, short len, Object valArray, short valOff)Fills the array of primitive components beginning at the specified position, for the specified length with the specified value.static shortarrayFillGenericNonAtomic(Object theArray, short off, short len, Object valArray, short valOff)Fills the array of primitive components(non-atomically) beginning at the specified position, for the specified length with the specified value.static shortarrayFindGeneric(Object theArray, short off, byte[] valArray, short valOff)Finds the first occurrence of the specified value within the specified array.
-
-
-
Method Detail
-
arrayCopyRepack
public static final short arrayCopyRepack(Object src, short srcOff, short srcLen, Object dest, short destOff) throws ArrayIndexOutOfBoundsException, NullPointerException, TransactionException, UtilException
Copies data from the specified source array, beginning at the specified position, to the specified position of the destination array. Note that this method may be used to copy from an array of any primitive component - byte, short or int to another (or same) array of any primitive component - byte, short or int. If the source array primitive component size is smaller than that of the destination array, a packing conversion is performed; if the source array primitive component size is larger than that of the destination array, an unpacking operation is performed; if the source and destination arrays are of the same component type, simple copy without any repacking is performed.Note:
- If the source array is a byte array and the destination is a short
array, then pairs of source array bytes are concatenated (high order byte
component first) to form short components
before being written to the destination short array. If the
srcLenparameter is not a multiple of 2, anUtilExceptionexception is thrown. - If the source array is a byte array and the destination is an int
array, 4 bytes of the source array are concatenated at a time (high order byte
component first) to form int components
before being written to the destination int array. If the
srcLenparameter is not a multiple of 4, anUtilExceptionexception is thrown. - If the source array is a short array and the destination is an int
array, then pairs of source array bytes are concatenated (high order short
component first) to form int components
before being written to the destination int array. If the
srcLenparameter is not a multiple of 2, anUtilExceptionexception is thrown. - If the source array is a short array and the destination is a byte array, then each short component is split into 2 bytes (high order byte component first) before being written sequentially to the destination byte array.
- If the source array is a int array and the destination is a short array, then each int component is split into 2 shorts (high order short component first) before being written sequentially to the destination short array.
- If the source array is a int array and the destination is a byte array, then each int component is split into 4 bytes (high order byte component first) before being written sequentially to the destination byte array.
- If
srcOffordestOfforsrcLenparameter is negative anArrayIndexOutOfBoundsExceptionexception is thrown. - If
srcOff+srcLenis greater thansrc.length, the length of thesrcarray aArrayIndexOutOfBoundsExceptionexception is thrown and no copy is performed. - If offset into the
destarray would become greater thandest.length, the length of thedestarray during the copy operationArrayIndexOutOfBoundsExceptionexception is thrown and no copy is performed. - If
srcordestparameter isnullaNullPointerExceptionexception is thrown. - If the
srcanddestarguments refer to the same array object, or if any of these arguments refer to an array view sharing components with the other argument, then the copying is performed as if the components at positionssrcOffthroughsrcOff+srcLen-1were first copied to a temporary array withsrcLencomponents and then the contents of the temporary array were copied into positionsdestOffthroughdestOff+srcLen-1of the destination array. - If the destination array is persistent, the entire copy is performed atomically.
- The copy operation is subject to atomic commit capacity limitations.
If the commit capacity is exceeded, no copy is performed and a
TransactionExceptionexception is thrown.
- Parameters:
src- source array objectsrcOff- offset within source array to start copy fromsrcLen- number of source component values to be copied from the source arraydest- destination array objectdestOff- offset within destination array to start copy into- Returns:
- a value of one more than the offset within the
destarray where the last copy was performed - Throws:
ArrayIndexOutOfBoundsException- if copying would cause access of data outside array boundsNullPointerException- if eithersrcordestisnullTransactionException- if copying would cause the commit capacity to be exceededUtilException- with the following reason codes:UtilException.ILLEGAL_VALUEifsrcordestis not an array of primitive components, or if thesrcLenparameter is incorrect
- See Also:
javacard.framework.JCSystem.getUnusedCommitCapacity()
- If the source array is a byte array and the destination is a short
array, then pairs of source array bytes are concatenated (high order byte
component first) to form short components
before being written to the destination short array. If the
-
arrayCopyRepackNonAtomic
public static final short arrayCopyRepackNonAtomic(Object src, short srcOff, short srcLen, Object dest, short destOff) throws ArrayIndexOutOfBoundsException, NullPointerException, UtilException, SystemException
Non-atomically copies data from the specified source array, beginning at the specified position, to the specified position of the destination array. Note that this method may be used to copy from an array of any primitive component - byte, short or int to another (or same) array of any primitive component - byte, short or int. If the source array primitive component size is smaller than that of the destination array, a packing conversion is performed; if the source array primitive component size is larger than that of the destination array, an unpacking operation is performed; if the source and destination arrays are of the same component type, simple copy without any repacking is performed.This method does not use the transaction facility during the copy operation even if a transaction is in progress. Thus, this method is suitable for use only when the contents of the destination array can be left in a partially modified state in the event of a power loss in the middle of the copy operation.
Note:
- If the source array is a byte array and the destination is a short
array, then pairs of source array bytes are concatenated (high order byte
component first) to form short components
before being written to the destination short array. If the
srcLenparameter is not a multiple of 2, anUtilExceptionexception is thrown. - If the source array is a byte array and the destination is an int
array, 4 bytes of the source array are concatenated at a time (high order byte
component first) to form int components
before being written to the destination int array. If the
srcLenparameter is not a multiple of 4, anUtilExceptionexception is thrown. - If the source array is a short array and the destination is an int
array, then pairs of source array bytes are concatenated (high order short
component first) to form int components
before being written to the destination int array. If the
srcLenparameter is not a multiple of 2, anUtilExceptionexception is thrown. - If the source array is a short array and the destination is a byte array, then each short component is split into 2 bytes (high order byte component first) before being written sequentially to the destination byte array.
- If the source array is a int array and the destination is a short array, then each int component is split into 2 shorts (high order short component first) before being written sequentially to the destination short array.
- If the source array is a int array and the destination is a byte array, then each int component is split into 4 bytes (high order byte component first) before being written sequentially to the destination byte array.
- If
srcOffordestOfforsrcLenparameter is negative anArrayIndexOutOfBoundsExceptionexception is thrown. - If
srcOff+srcLenis greater thansrc.length, the length of thesrcarray aArrayIndexOutOfBoundsExceptionexception is thrown and no copy is performed. - If offset into the
destarray would become greater thandest.length, the length of thedestarray during the copy operationArrayIndexOutOfBoundsExceptionexception is thrown and no copy is performed. - If
srcordestparameter isnullaNullPointerExceptionexception is thrown. - If the
srcanddestarguments refer to the same array object, or if any of these arguments refer to an array view sharing components with the other argument, then the copying is performed as if the components at positionssrcOffthroughsrcOff+srcLen-1were first copied to a temporary array withsrcLencomponents and then the contents of the temporary array were copied into positionsdestOffthroughdestOff+srcLen-1of the destination array. - Non atomic array operations on persistent integrity-sensitive arrays created using the
SensitiveArraysAPI are not supported; therefore if thedestarray parameter is a persistent integrity-sensitive array aSystemExceptionexception is thrown.
- Parameters:
src- source array objectsrcOff- offset within source array to start copy fromsrcLen- number of source component values to be copied from the source arraydest- destination array objectdestOff- offset within destination array to start copy into- Returns:
- a value of one more than the offset within the
destarray where the last copy was performed - Throws:
ArrayIndexOutOfBoundsException- if copying would cause access of data outside array boundsNullPointerException- if eithersrcordestisnullUtilException- with the following reason codes:UtilException.ILLEGAL_VALUEifsrcordestis not an array of primitive components, or if thesrcLenparameter is incorrect
SystemException- with the following reason codes:ILLEGAL_VALUEifdestis a persistent integrity-sensitive array object.
- If the source array is a byte array and the destination is a short
array, then pairs of source array bytes are concatenated (high order byte
component first) to form short components
before being written to the destination short array. If the
-
arrayFillGeneric
public static final short arrayFillGeneric(Object theArray, short off, short len, Object valArray, short valOff) throws ArrayIndexOutOfBoundsException, NullPointerException, UtilException, TransactionException
Fills the array of primitive components beginning at the specified position, for the specified length with the specified value. Note that this method may be used to fill an array of any primitive component type - byte, short or int. The value used for the fill operation is itself specified using an array (valArray) of the same primitive component type at offsetvalOff.The following code snippet shows how this method is typically used:
public short[] myArray = new short[10]; .. // Fill the entire array myArray of 10 short components with the value 0x1234 myArray[0] = (short)0x1234; ArrayLogic.arrayFillGeneric(myArray, (short)0, (short)10, myArray, (short)0); ..
Note:
- If
offorlenorvalOffparameter is negative anArrayIndexOutOfBoundsExceptionexception is thrown. - If
off+lenis greater thantheArray.length, the length of thetheArrayarray anArrayIndexOutOfBoundsExceptionexception is thrown. - If
valOffis equal to or greater thanvalArray.length, the length of thevalArrayarray anArrayIndexOutOfBoundsExceptionexception is thrown. - If
theArrayorvalArrayparameter isnullaNullPointerExceptionexception is thrown. - If the destination array is persistent, the entire copy is performed atomically.
- The fill operation is subject to atomic commit capacity limitations.
If the commit capacity is exceeded, no filling is performed and a
TransactionExceptionexception is thrown.
- Parameters:
theArray- the array objectoff- offset within array to start filling the specified valuelen- the number of component values to be filledvalArray- the array object containing the fill valuevalOff- the offset within thevalArrayarray containing the fill value- Returns:
off+len- Throws:
ArrayIndexOutOfBoundsException- if the fill operation would cause access of data outside array boundsNullPointerException- if theArray or valArray isnullUtilException- with the following reason codes:UtilException.ILLEGAL_VALUEiftheArrayorvalArrayis not an array of primitive componentsUtilException.TYPE_MISMATCHEDif thevalArrayparameter is not an array of the same primitive component type as thetheArray.
TransactionException- if filling would cause the commit capacity to be exceeded- Since:
- 3.0.5
- If
-
arrayFillGenericNonAtomic
public static final short arrayFillGenericNonAtomic(Object theArray, short off, short len, Object valArray, short valOff) throws ArrayIndexOutOfBoundsException, NullPointerException, UtilException, SystemException
Fills the array of primitive components(non-atomically) beginning at the specified position, for the specified length with the specified value. Note that this method may be used to fill an array of any primitive component type - byte, short or int. The value used for the fill operation is itself specified using an array (valArray) of the same primitive component type at offsetvalOff.This method does not use the transaction facility during the fill operation even if a transaction is in progress. Thus, this method is suitable for use only when the contents of the array can be left in a partially filled state in the event of a power loss in the middle of the fill operation.
The following code snippet shows how this method is typically used:
public short[] myArray = new short[10]; .. // Fill the entire array myArray of 10 short components with the value 0x1234 myArray[0] = (short)0x1234; ArrayLogic.arrayFillGenericNonAtomic(myArray, (short)0, (short)10, myArray, (short)0); ..
Note:
- If
offorlenorvalOffparameter is negative anArrayIndexOutOfBoundsExceptionexception is thrown. - If
off+lenis greater thantheArray.length, the length of thetheArrayarray anArrayIndexOutOfBoundsExceptionexception is thrown. - If
valOffis equal to or greater thanvalArray.length, the length of thevalArrayarray anArrayIndexOutOfBoundsExceptionexception is thrown. - If
theArrayorvalArrayparameter isnullaNullPointerExceptionexception is thrown. - If power is lost during the copy operation and the array is persistent, a partially changed array could result.
- The
lenparameter is not constrained by the atomic commit capacity limitations. - Non atomic array operations on persistent integrity-sensitive arrays created using the
SensitiveArraysAPI are not supported; therefore if thetheArrayarray parameter is a persistent integrity-sensitive array aSystemExceptionexception is thrown.
- Parameters:
theArray- the array objectoff- offset within array to start filling the specified valuelen- the number of component values to be filledvalArray- the array object containing the fill valuevalOff- the offset within thevalArrayarray containing the fill value- Returns:
off+len- Throws:
ArrayIndexOutOfBoundsException- if the fill operation would cause access of data outside array boundsNullPointerException- if theArray or valArray isnullUtilException- with the following reason codes:UtilException.ILLEGAL_VALUEiftheArrayorvalArrayis not an array of primitive componentsUtilException.TYPE_MISMATCHEDif thevalArrayparameter is not an array of the same primitive component type as thetheArray.
SystemException- with the following reason codes:ILLEGAL_VALUEiftheArrayis a persistent integrity-sensitive array object.
- If
-
arrayCompareGeneric
public static final byte arrayCompareGeneric(Object src, short srcOff, Object dest, short destOff, short length) throws ArrayIndexOutOfBoundsException, NullPointerException, UtilException
Compares an array from the specified source array, beginning at the specified position, with the specified position of the destination array from left to right. Note that this method may be used to compare any two arrays of the same primitive component type - byte, short or int. Returns the ternary result of the comparison : less than(-1), equal(0) or greater than(1).Note:
- If
srcOffordestOfforlengthparameter is negative anArrayIndexOutOfBoundsExceptionexception is thrown. - If
srcOff+lengthis greater thansrc.length, the length of thesrcarray aArrayIndexOutOfBoundsExceptionexception is thrown. - If
destOff+lengthis greater thandest.length, the length of thedestarray anArrayIndexOutOfBoundsExceptionexception is thrown. - If
srcordestparameter isnullaNullPointerExceptionexception is thrown.
- Parameters:
src- source array objectsrcOff- offset within source array to start comparedest- destination array objectdestOff- offset within destination array to start comparelength- length to be compared- Returns:
- the result of the comparison as follows:
-
0if identical -
-1if the first miscomparing primitive component in source array is less than that in destination array -
1if the first miscomparing primitive component in source array is greater than that in destination array
-
- Throws:
ArrayIndexOutOfBoundsException- if comparing all the components would cause access of data outside array boundsNullPointerException- if eithersrcordestisnullUtilException- with the following reason codes:UtilException.ILLEGAL_VALUEifsrcordestis not an array of primitive components, or if thelengthparameter is incorrectUtilException.TYPE_MISMATCHEDif thedestparameter is not an array of the same primitive component type.
- If
-
arrayFindGeneric
public static final short arrayFindGeneric(Object theArray, short off, byte[] valArray, short valOff) throws ArrayIndexOutOfBoundsException, NullPointerException, UtilException
Finds the first occurrence of the specified value within the specified array. The search begins at the specified position and proceeds until the end of the array. Note that this method may be used to search an array of any primitive component type - byte, short or int. The value used in the search operation is itself specified by the appropriate number of consecutive bytes at offsetvalOffin the byte array parametervalArray.Note:
- If
offorvalOffparameter is negative anArrayIndexOutOfBoundsExceptionexception is thrown. - If
offis greater thantheArray.length, the length of thetheArrayarray anArrayIndexOutOfBoundsExceptionexception is thrown. - If
theArrayorvalArrayparameter isnullaNullPointerExceptionexception is thrown. - If the specified array is an array of byte components, then the byte
at
valOffin thevalArrayis used as the search value. IfvalOff+1is greater thanvalArray.length, the length of thevalArrayarray anArrayIndexOutOfBoundsExceptionexception is thrown. - If the specified array is an array of short components, then 2 consecutive bytes
beginning at
valOffin thevalArrayare concatenated (high order byte component first) to form the search value. IfvalOff+2is greater thanvalArray.length, the length of thevalArrayarray anArrayIndexOutOfBoundsExceptionexception is thrown. - If the specified array is an array of int components, then 4 consecutive bytes
beginning at
valOffin thevalArrayare concatenated (high order byte component first) to form the search value. IfvalOff+4is greater thanvalArray.length, the length of thevalArrayarray anArrayIndexOutOfBoundsExceptionexception is thrown.
- Parameters:
theArray- the array object to searchoff- offset within the array to start serching for the specified valuevalArray- the array object containing the search valuevalOff- the offset within thevalArrayarray containing the search value- Returns:
- the offset into the specified array where the first occurrence of specified value was found or -1 if the specified value does not occur in the specified portion of the array
- Throws:
ArrayIndexOutOfBoundsException- if the search operation would cause access of data outside array boundsNullPointerException- iftheArrayisnullUtilException- with the following reason code:UtilException.ILLEGAL_VALUEiftheArrayis not an array of primitive components.
- If
-
-