Class ArrayLogic
- java.lang.Object
-
- javacardx.framework.util.ArrayLogic
-
public final class ArrayLogic extends Object
TheArrayLogic
class 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 inArrayLogic
class 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. TheJCSystem
class 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 byte
arrayCompareGeneric(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 short
arrayCopyRepack(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 short
arrayCopyRepackNonAtomic(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 short
arrayFillGeneric(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 short
arrayFillGenericNonAtomic(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 short
arrayFindGeneric(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
srcLen
parameter is not a multiple of 2, anUtilException
exception 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
srcLen
parameter is not a multiple of 4, anUtilException
exception 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
srcLen
parameter is not a multiple of 2, anUtilException
exception 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
srcOff
ordestOff
orsrcLen
parameter is negative anArrayIndexOutOfBoundsException
exception is thrown. - If
srcOff+srcLen
is greater thansrc.length
, the length of thesrc
array aArrayIndexOutOfBoundsException
exception is thrown and no copy is performed. - If offset into the
dest
array would become greater thandest.length
, the length of thedest
array during the copy operationArrayIndexOutOfBoundsException
exception is thrown and no copy is performed. - If
src
ordest
parameter isnull
aNullPointerException
exception is thrown. - If the
src
anddest
arguments 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 positionssrcOff
throughsrcOff+srcLen-1
were first copied to a temporary array withsrcLen
components and then the contents of the temporary array were copied into positionsdestOff
throughdestOff+srcLen-1
of 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
TransactionException
exception 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
dest
array where the last copy was performed - Throws:
ArrayIndexOutOfBoundsException
- if copying would cause access of data outside array boundsNullPointerException
- if eithersrc
ordest
isnull
TransactionException
- if copying would cause the commit capacity to be exceededUtilException
- with the following reason codes:UtilException.ILLEGAL_VALUE
ifsrc
ordest
is not an array of primitive components, or if thesrcLen
parameter 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
srcLen
parameter is not a multiple of 2, anUtilException
exception 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
srcLen
parameter is not a multiple of 4, anUtilException
exception 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
srcLen
parameter is not a multiple of 2, anUtilException
exception 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
srcOff
ordestOff
orsrcLen
parameter is negative anArrayIndexOutOfBoundsException
exception is thrown. - If
srcOff+srcLen
is greater thansrc.length
, the length of thesrc
array aArrayIndexOutOfBoundsException
exception is thrown and no copy is performed. - If offset into the
dest
array would become greater thandest.length
, the length of thedest
array during the copy operationArrayIndexOutOfBoundsException
exception is thrown and no copy is performed. - If
src
ordest
parameter isnull
aNullPointerException
exception is thrown. - If the
src
anddest
arguments 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 positionssrcOff
throughsrcOff+srcLen-1
were first copied to a temporary array withsrcLen
components and then the contents of the temporary array were copied into positionsdestOff
throughdestOff+srcLen-1
of the destination array. - Non atomic array operations on persistent integrity-sensitive arrays created using the
SensitiveArrays
API are not supported; therefore if thedest
array parameter is a persistent integrity-sensitive array aSystemException
exception 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
dest
array where the last copy was performed - Throws:
ArrayIndexOutOfBoundsException
- if copying would cause access of data outside array boundsNullPointerException
- if eithersrc
ordest
isnull
UtilException
- with the following reason codes:UtilException.ILLEGAL_VALUE
ifsrc
ordest
is not an array of primitive components, or if thesrcLen
parameter is incorrect
SystemException
- with the following reason codes:ILLEGAL_VALUE
ifdest
is 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
off
orlen
orvalOff
parameter is negative anArrayIndexOutOfBoundsException
exception is thrown. - If
off+len
is greater thantheArray.length
, the length of thetheArray
array anArrayIndexOutOfBoundsException
exception is thrown. - If
valOff
is equal to or greater thanvalArray.length
, the length of thevalArray
array anArrayIndexOutOfBoundsException
exception is thrown. - If
theArray
orvalArray
parameter isnull
aNullPointerException
exception 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
TransactionException
exception 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 thevalArray
array 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 isnull
UtilException
- with the following reason codes:UtilException.ILLEGAL_VALUE
iftheArray
orvalArray
is not an array of primitive componentsUtilException.TYPE_MISMATCHED
if thevalArray
parameter 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
off
orlen
orvalOff
parameter is negative anArrayIndexOutOfBoundsException
exception is thrown. - If
off+len
is greater thantheArray.length
, the length of thetheArray
array anArrayIndexOutOfBoundsException
exception is thrown. - If
valOff
is equal to or greater thanvalArray.length
, the length of thevalArray
array anArrayIndexOutOfBoundsException
exception is thrown. - If
theArray
orvalArray
parameter isnull
aNullPointerException
exception is thrown. - If power is lost during the copy operation and the array is persistent, a partially changed array could result.
- The
len
parameter is not constrained by the atomic commit capacity limitations. - Non atomic array operations on persistent integrity-sensitive arrays created using the
SensitiveArrays
API are not supported; therefore if thetheArray
array parameter is a persistent integrity-sensitive array aSystemException
exception 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 thevalArray
array 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 isnull
UtilException
- with the following reason codes:UtilException.ILLEGAL_VALUE
iftheArray
orvalArray
is not an array of primitive componentsUtilException.TYPE_MISMATCHED
if thevalArray
parameter is not an array of the same primitive component type as thetheArray
.
SystemException
- with the following reason codes:ILLEGAL_VALUE
iftheArray
is 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
srcOff
ordestOff
orlength
parameter is negative anArrayIndexOutOfBoundsException
exception is thrown. - If
srcOff+length
is greater thansrc.length
, the length of thesrc
array aArrayIndexOutOfBoundsException
exception is thrown. - If
destOff+length
is greater thandest.length
, the length of thedest
array anArrayIndexOutOfBoundsException
exception is thrown. - If
src
ordest
parameter isnull
aNullPointerException
exception 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:
-
0
if identical -
-1
if the first miscomparing primitive component in source array is less than that in destination array -
1
if 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 eithersrc
ordest
isnull
UtilException
- with the following reason codes:UtilException.ILLEGAL_VALUE
ifsrc
ordest
is not an array of primitive components, or if thelength
parameter is incorrectUtilException.TYPE_MISMATCHED
if thedest
parameter 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 offsetvalOff
in the byte array parametervalArray
.Note:
- If
off
orvalOff
parameter is negative anArrayIndexOutOfBoundsException
exception is thrown. - If
off
is greater thantheArray.length
, the length of thetheArray
array anArrayIndexOutOfBoundsException
exception is thrown. - If
theArray
orvalArray
parameter isnull
aNullPointerException
exception is thrown. - If the specified array is an array of byte components, then the byte
at
valOff
in thevalArray
is used as the search value. IfvalOff+1
is greater thanvalArray.length
, the length of thevalArray
array anArrayIndexOutOfBoundsException
exception is thrown. - If the specified array is an array of short components, then 2 consecutive bytes
beginning at
valOff
in thevalArray
are concatenated (high order byte component first) to form the search value. IfvalOff+2
is greater thanvalArray.length
, the length of thevalArray
array anArrayIndexOutOfBoundsException
exception is thrown. - If the specified array is an array of int components, then 4 consecutive bytes
beginning at
valOff
in thevalArray
are concatenated (high order byte component first) to form the search value. IfvalOff+4
is greater thanvalArray.length
, the length of thevalArray
array anArrayIndexOutOfBoundsException
exception 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 thevalArray
array 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
- iftheArray
isnull
UtilException
- with the following reason code:UtilException.ILLEGAL_VALUE
iftheArray
is not an array of primitive components.
- If
-
-