2362N/A * Copyright (c) 2005, 2006, Oracle and/or its affiliates. All rights reserved. 0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 0N/A * This code is free software; you can redistribute it and/or modify it 0N/A * under the terms of the GNU General Public License version 2 only, as 2362N/A * published by the Free Software Foundation. Oracle designates this 0N/A * particular file as subject to the "Classpath" exception as provided 2362N/A * by Oracle in the LICENSE file that accompanied this code. 0N/A * This code is distributed in the hope that it will be useful, but WITHOUT 0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 0N/A * version 2 for more details (a copy is included in the LICENSE file that 0N/A * accompanied this code). 0N/A * You should have received a copy of the GNU General Public License version 0N/A * 2 along with this work; if not, write to the Free Software Foundation, 0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 2362N/A * or visit www.oracle.com if you need additional information or have any 0N/A * A command APDU following the structure defined in ISO/IEC 7816-4. 0N/A * It consists of a four byte header and a conditional body of variable length. 0N/A * This class does not attempt to verify that the APDU encodes a semantically 0N/A * <p>Note that when the expected length of the response APDU is specified 0N/A * in the {@linkplain #CommandAPDU(int,int,int,int,int) constructors}, 0N/A * the actual length (Ne) must be specified, not its 0N/A * encoded form (Le). Similarly, {@linkplain #getNe} returns the actual 0N/A * value Ne. In other words, a value of 0 means "no data in the response APDU" 0N/A * rather than "maximum length." 0N/A * <p>This class supports both the short and extended forms of length 0N/A * encoding for Ne and Nc. However, note that not all terminals and Smart Cards 0N/A * are capable of accepting APDUs that use the extended form. 0N/A * <p>For the header bytes CLA, INS, P1, and P2 the Java type <code>int</code> 0N/A * is used to represent the 8 bit unsigned values. In the constructors, only 0N/A * the 8 lowest bits of the <code>int</code> value specified by the application 0N/A * are significant. The accessor methods always return the byte as an unsigned 0N/A * value between 0 and 255. 0N/A * <p>Instances of this class are immutable. Where data is passed in or out 0N/A * via byte arrays, defensive cloning is performed. 0N/A * @see CardChannel#transmit CardChannel.transmit 0N/A * @author Andreas Sterbenz 0N/A * @author JSR 268 Expert Group 0N/A private transient int nc;
0N/A private transient int ne;
0N/A // index of start of data within the apdu array 0N/A * Constructs a CommandAPDU from a byte array containing the complete 0N/A * APDU contents (header and body). 0N/A * <p>Note that the apdu bytes are copied to protect against 0N/A * subsequent modification. 0N/A * @param apdu the complete command APDU 0N/A * @throws NullPointerException if apdu is null 0N/A * @throws IllegalArgumentException if apdu does not contain a valid 0N/A * Constructs a CommandAPDU from a byte array containing the complete 0N/A * APDU contents (header and body). The APDU starts at the index 0N/A * <code>apduOffset</code> in the byte array and is <code>apduLength</code> 0N/A * <p>Note that the apdu bytes are copied to protect against 0N/A * subsequent modification. 0N/A * @param apdu the complete command APDU 0N/A * @param apduOffset the offset in the byte array at which the apdu 0N/A * @param apduLength the length of the APDU 0N/A * @throws NullPointerException if apdu is null 0N/A * @throws IllegalArgumentException if apduOffset or apduLength are 0N/A * negative or if apduOffset + apduLength are greater than apdu.length, 0N/A * or if the specified bytes are not a valid APDU 0N/A (
"Offset and length must not be negative");
0N/A (
"offset and length must be 0 if array is null");
0N/A (
"Offset plus length exceed array size");
0N/A * Creates a CommandAPDU from the ByteBuffer containing the complete APDU 0N/A * contents (header and body). 0N/A * The buffer's <code>position</code> must be set to the start of the APDU, 0N/A * its <code>limit</code> to the end of the APDU. Upon return, the buffer's 0N/A * <code>position</code> is equal to its limit; its limit remains unchanged. 0N/A * <p>Note that the data in the ByteBuffer is copied to protect against 0N/A * subsequent modification. 0N/A * @param apdu the ByteBuffer containing the complete APDU 0N/A * @throws NullPointerException if apdu is null 0N/A * @throws IllegalArgumentException if apdu does not contain a valid 0N/A * Constructs a CommandAPDU from the four header bytes. This is case 1 0N/A * in ISO 7816, no command body. 0N/A * @param cla the class byte CLA 0N/A * @param ins the instruction byte INS 0N/A * @param p1 the parameter byte P1 0N/A * @param p2 the parameter byte P2 0N/A * Constructs a CommandAPDU from the four header bytes and the expected 0N/A * response data length. This is case 2 in ISO 7816, empty command data 0N/A * field with Ne specified. If Ne is 0, the APDU is encoded as ISO 7816 0N/A * @param cla the class byte CLA 0N/A * @param ins the instruction byte INS 0N/A * @param p1 the parameter byte P1 0N/A * @param p2 the parameter byte P2 0N/A * @param ne the maximum number of expected data bytes in a response APDU 0N/A * @throws IllegalArgumentException if ne is negative or greater than 0N/A * Constructs a CommandAPDU from the four header bytes and command data. 0N/A * This is case 3 in ISO 7816, command data present and Ne absent. The 0N/A * value Nc is taken as data.length. If <code>data</code> is null or 0N/A * its length is 0, the APDU is encoded as ISO 7816 case 1. 0N/A * <p>Note that the data bytes are copied to protect against 0N/A * subsequent modification. 0N/A * @param cla the class byte CLA 0N/A * @param ins the instruction byte INS 0N/A * @param p1 the parameter byte P1 0N/A * @param p2 the parameter byte P2 0N/A * @param data the byte array containing the data bytes of the command body 0N/A * @throws IllegalArgumentException if data.length is greater than 65535 0N/A * Constructs a CommandAPDU from the four header bytes and command data. 0N/A * This is case 3 in ISO 7816, command data present and Ne absent. The 0N/A * value Nc is taken as dataLength. If <code>dataLength</code> 0N/A * is 0, the APDU is encoded as ISO 7816 case 1. 0N/A * <p>Note that the data bytes are copied to protect against 0N/A * subsequent modification. 0N/A * @param cla the class byte CLA 0N/A * @param ins the instruction byte INS 0N/A * @param p1 the parameter byte P1 0N/A * @param p2 the parameter byte P2 0N/A * @param data the byte array containing the data bytes of the command body 0N/A * @param dataOffset the offset in the byte array at which the data 0N/A * bytes of the command body begin 0N/A * @param dataLength the number of the data bytes in the command body 0N/A * @throws NullPointerException if data is null and dataLength is not 0 0N/A * @throws IllegalArgumentException if dataOffset or dataLength are 0N/A * negative or if dataOffset + dataLength are greater than data.length 0N/A * or if dataLength is greater than 65535 0N/A * Constructs a CommandAPDU from the four header bytes, command data, 0N/A * and expected response data length. This is case 4 in ISO 7816, 0N/A * command data and Ne present. The value Nc is taken as data.length 0N/A * if <code>data</code> is non-null and as 0 otherwise. If Ne or Nc 0N/A * are zero, the APDU is encoded as case 1, 2, or 3 per ISO 7816. 0N/A * <p>Note that the data bytes are copied to protect against 0N/A * subsequent modification. 0N/A * @param cla the class byte CLA 0N/A * @param ins the instruction byte INS 0N/A * @param p1 the parameter byte P1 0N/A * @param p2 the parameter byte P2 0N/A * @param data the byte array containing the data bytes of the command body 0N/A * @param ne the maximum number of expected data bytes in a response APDU 0N/A * @throws IllegalArgumentException if data.length is greater than 65535 0N/A * or if ne is negative or greater than 65536 0N/A * Command APDU encoding options: 0N/A * case 1: |CLA|INS|P1 |P2 | len = 4 0N/A * case 2s: |CLA|INS|P1 |P2 |LE | len = 5 0N/A * case 3s: |CLA|INS|P1 |P2 |LC |...BODY...| len = 6..260 0N/A * case 4s: |CLA|INS|P1 |P2 |LC |...BODY...|LE | len = 7..261 0N/A * case 2e: |CLA|INS|P1 |P2 |00 |LE1|LE2| len = 7 0N/A * case 3e: |CLA|INS|P1 |P2 |00 |LC1|LC2|...BODY...| len = 8..65542 0N/A * case 4e: |CLA|INS|P1 |P2 |00 |LC1|LC2|...BODY...|LE1|LE2| len =10..65544 0N/A * LE, LE1, LE2 may be 0x00. 0N/A * LC must not be 0x00 and LC1|LC2 must not be 0x00|0x00 0N/A * Constructs a CommandAPDU from the four header bytes, command data, 0N/A * and expected response data length. This is case 4 in ISO 7816, 0N/A * command data and Le present. The value Nc is taken as 0N/A * <code>dataLength</code>. 0N/A * are zero, the APDU is encoded as case 1, 2, or 3 per ISO 7816. 0N/A * <p>Note that the data bytes are copied to protect against 0N/A * subsequent modification. 0N/A * @param cla the class byte CLA 0N/A * @param ins the instruction byte INS 0N/A * @param p1 the parameter byte P1 0N/A * @param p2 the parameter byte P2 0N/A * @param data the byte array containing the data bytes of the command body 0N/A * @param dataOffset the offset in the byte array at which the data 0N/A * bytes of the command body begin 0N/A * @param dataLength the number of the data bytes in the command body 0N/A * @param ne the maximum number of expected data bytes in a response APDU 0N/A * @throws NullPointerException if data is null and dataLength is not 0 0N/A * @throws IllegalArgumentException if dataOffset or dataLength are 0N/A * negative or if dataOffset + dataLength are greater than data.length, 0N/A * or if ne is negative or greater than 65536, 0N/A * or if dataLength is greater than 65535 0N/A // 256 is encoded as 0x00 0N/A // 65536 is encoded as 0x00 0x00 0N/A }
// else le == 65536: no need to fill in, encoded as 0 0N/A * Returns the value of the class byte CLA. 0N/A * @return the value of the class byte CLA. 0N/A * Returns the value of the instruction byte INS. 0N/A * @return the value of the instruction byte INS. 0N/A * Returns the value of the parameter byte P1. 0N/A * @return the value of the parameter byte P1. 0N/A * Returns the value of the parameter byte P2. 0N/A * @return the value of the parameter byte P2. 0N/A * Returns the number of data bytes in the command body (Nc) or 0 if this 0N/A * APDU has no body. This call is equivalent to 0N/A * <code>getData().length</code>. 0N/A * @return the number of data bytes in the command body or 0 if this APDU 0N/A * Returns a copy of the data bytes in the command body. If this APDU as 0N/A * no body, this method returns a byte array with length zero. 0N/A * @return a copy of the data bytes in the command body or the empty 0N/A * byte array if this APDU has no body. 0N/A * Returns the maximum number of expected data bytes in a response 0N/A * @return the maximum number of expected data bytes in a response APDU. 0N/A * Returns a copy of the bytes in this APDU. 0N/A * @return a copy of the bytes in this APDU. 0N/A * Returns a string representation of this command APDU. 0N/A * @return a String representation of this command APDU. 0N/A * Compares the specified object with this command APDU for equality. 0N/A * Returns true if the given object is also a CommandAPDU and its bytes are 0N/A * identical to the bytes in this CommandAPDU. 0N/A * @param obj the object to be compared for equality with this command APDU 0N/A * @return true if the specified object is equal to this command APDU 0N/A * Returns the hash code value for this command APDU. 0N/A * @return the hash code value for this command APDU. 0N/A // initialize transient fields