0N/A/*
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 *
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 *
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 *
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.
0N/A *
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
2362N/A * questions.
0N/A */
0N/A
0N/Apackage javax.smartcardio;
0N/A
0N/Aimport java.util.Arrays;
0N/A
0N/A/**
0N/A * A response APDU as defined in ISO/IEC 7816-4. It consists of a conditional
0N/A * body and a two byte trailer.
0N/A * This class does not attempt to verify that the APDU encodes a semantically
0N/A * valid response.
0N/A *
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 *
0N/A * @see CommandAPDU
0N/A * @see CardChannel#transmit CardChannel.transmit
0N/A *
0N/A * @since 1.6
0N/A * @author Andreas Sterbenz
0N/A * @author JSR 268 Expert Group
0N/A */
0N/Apublic final class ResponseAPDU implements java.io.Serializable {
0N/A
0N/A private static final long serialVersionUID = 6962744978375594225L;
0N/A
0N/A /** @serial */
0N/A private byte[] apdu;
0N/A
0N/A /**
0N/A * Constructs a ResponseAPDU from a byte array containing the complete
0N/A * APDU contents (conditional body and trailed).
0N/A *
0N/A * <p>Note that the byte array is cloned to protect against subsequent
0N/A * modification.
0N/A *
0N/A * @param apdu the complete response APDU
0N/A *
0N/A * @throws NullPointerException if apdu is null
0N/A * @throws IllegalArgumentException if apdu.length is less than 2
0N/A */
0N/A public ResponseAPDU(byte[] apdu) {
0N/A apdu = apdu.clone();
0N/A check(apdu);
0N/A this.apdu = apdu;
0N/A }
0N/A
0N/A private static void check(byte[] apdu) {
0N/A if (apdu.length < 2) {
0N/A throw new IllegalArgumentException("apdu must be at least 2 bytes long");
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Returns the number of data bytes in the response body (Nr) or 0 if this
0N/A * APDU has no body. This call is equivalent to
0N/A * <code>getData().length</code>.
0N/A *
0N/A * @return the number of data bytes in the response body or 0 if this APDU
0N/A * has no body.
0N/A */
0N/A public int getNr() {
0N/A return apdu.length - 2;
0N/A }
0N/A
0N/A /**
0N/A * Returns a copy of the data bytes in the response body. If this APDU as
0N/A * no body, this method returns a byte array with a length of zero.
0N/A *
0N/A * @return a copy of the data bytes in the response body or the empty
0N/A * byte array if this APDU has no body.
0N/A */
0N/A public byte[] getData() {
0N/A byte[] data = new byte[apdu.length - 2];
0N/A System.arraycopy(apdu, 0, data, 0, data.length);
0N/A return data;
0N/A }
0N/A
0N/A /**
0N/A * Returns the value of the status byte SW1 as a value between 0 and 255.
0N/A *
0N/A * @return the value of the status byte SW1 as a value between 0 and 255.
0N/A */
0N/A public int getSW1() {
0N/A return apdu[apdu.length - 2] & 0xff;
0N/A }
0N/A
0N/A /**
0N/A * Returns the value of the status byte SW2 as a value between 0 and 255.
0N/A *
0N/A * @return the value of the status byte SW2 as a value between 0 and 255.
0N/A */
0N/A public int getSW2() {
0N/A return apdu[apdu.length - 1] & 0xff;
0N/A }
0N/A
0N/A /**
0N/A * Returns the value of the status bytes SW1 and SW2 as a single
0N/A * status word SW.
0N/A * It is defined as
0N/A * <code>(getSW1() << 8) | getSW2()</code>.
0N/A *
0N/A * @return the value of the status word SW.
0N/A */
0N/A public int getSW() {
0N/A return (getSW1() << 8) | getSW2();
0N/A }
0N/A
0N/A /**
0N/A * Returns a copy of the bytes in this APDU.
0N/A *
0N/A * @return a copy of the bytes in this APDU.
0N/A */
0N/A public byte[] getBytes() {
0N/A return apdu.clone();
0N/A }
0N/A
0N/A /**
0N/A * Returns a string representation of this response APDU.
0N/A *
0N/A * @return a String representation of this response APDU.
0N/A */
0N/A public String toString() {
0N/A return "ResponseAPDU: " + apdu.length + " bytes, SW="
0N/A + Integer.toHexString(getSW());
0N/A }
0N/A
0N/A /**
0N/A * Compares the specified object with this response APDU for equality.
0N/A * Returns true if the given object is also a ResponseAPDU and its bytes are
0N/A * identical to the bytes in this ResponseAPDU.
0N/A *
0N/A * @param obj the object to be compared for equality with this response APDU
0N/A * @return true if the specified object is equal to this response APDU
0N/A */
0N/A public boolean equals(Object obj) {
0N/A if (this == obj) {
0N/A return true;
0N/A }
0N/A if (obj instanceof ResponseAPDU == false) {
0N/A return false;
0N/A }
0N/A ResponseAPDU other = (ResponseAPDU)obj;
0N/A return Arrays.equals(this.apdu, other.apdu);
0N/A }
0N/A
0N/A /**
0N/A * Returns the hash code value for this response APDU.
0N/A *
0N/A * @return the hash code value for this response APDU.
0N/A */
0N/A public int hashCode() {
0N/A return Arrays.hashCode(apdu);
0N/A }
0N/A
0N/A private void readObject(java.io.ObjectInputStream in)
0N/A throws java.io.IOException, ClassNotFoundException {
0N/A apdu = (byte[])in.readUnshared();
0N/A check(apdu);
0N/A }
0N/A
0N/A}