Path: blob/master/src/java.smartcardio/share/classes/javax/smartcardio/ResponseAPDU.java
41153 views
/*1* Copyright (c) 2005, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package javax.smartcardio;2627import java.util.Arrays;2829/**30* A response APDU as defined in ISO/IEC 7816-4. It consists of a conditional31* body and a two byte trailer.32* This class does not attempt to verify that the APDU encodes a semantically33* valid response.34*35* <p>Instances of this class are immutable. Where data is passed in or out36* via byte arrays, defensive cloning is performed.37*38* @see CommandAPDU39* @see CardChannel#transmit CardChannel.transmit40*41* @since 1.642* @author Andreas Sterbenz43* @author JSR 268 Expert Group44*/45public final class ResponseAPDU implements java.io.Serializable {4647private static final long serialVersionUID = 6962744978375594225L;4849/** @serial */50private byte[] apdu;5152/**53* Constructs a ResponseAPDU from a byte array containing the complete54* APDU contents (conditional body and trailed).55*56* <p>Note that the byte array is cloned to protect against subsequent57* modification.58*59* @param apdu the complete response APDU60*61* @throws NullPointerException if apdu is null62* @throws IllegalArgumentException if apdu.length is less than 263*/64public ResponseAPDU(byte[] apdu) {65apdu = apdu.clone();66check(apdu);67this.apdu = apdu;68}6970private static void check(byte[] apdu) {71if (apdu.length < 2) {72throw new IllegalArgumentException("apdu must be at least 2 bytes long");73}74}7576/**77* Returns the number of data bytes in the response body (Nr) or 0 if this78* APDU has no body. This call is equivalent to79* <code>getData().length</code>.80*81* @return the number of data bytes in the response body or 0 if this APDU82* has no body.83*/84public int getNr() {85return apdu.length - 2;86}8788/**89* Returns a copy of the data bytes in the response body. If this APDU as90* no body, this method returns a byte array with a length of zero.91*92* @return a copy of the data bytes in the response body or the empty93* byte array if this APDU has no body.94*/95public byte[] getData() {96byte[] data = new byte[apdu.length - 2];97System.arraycopy(apdu, 0, data, 0, data.length);98return data;99}100101/**102* Returns the value of the status byte SW1 as a value between 0 and 255.103*104* @return the value of the status byte SW1 as a value between 0 and 255.105*/106public int getSW1() {107return apdu[apdu.length - 2] & 0xff;108}109110/**111* Returns the value of the status byte SW2 as a value between 0 and 255.112*113* @return the value of the status byte SW2 as a value between 0 and 255.114*/115public int getSW2() {116return apdu[apdu.length - 1] & 0xff;117}118119/**120* Returns the value of the status bytes SW1 and SW2 as a single121* status word SW.122* It is defined as123* {@code (getSW1() << 8) | getSW2()}124*125* @return the value of the status word SW.126*/127public int getSW() {128return (getSW1() << 8) | getSW2();129}130131/**132* Returns a copy of the bytes in this APDU.133*134* @return a copy of the bytes in this APDU.135*/136public byte[] getBytes() {137return apdu.clone();138}139140/**141* Returns a string representation of this response APDU.142*143* @return a String representation of this response APDU.144*/145public String toString() {146return "ResponseAPDU: " + apdu.length + " bytes, SW="147+ Integer.toHexString(getSW());148}149150/**151* Compares the specified object with this response APDU for equality.152* Returns true if the given object is also a ResponseAPDU and its bytes are153* identical to the bytes in this ResponseAPDU.154*155* @param obj the object to be compared for equality with this response APDU156* @return true if the specified object is equal to this response APDU157*/158public boolean equals(Object obj) {159if (this == obj) {160return true;161}162if (obj instanceof ResponseAPDU == false) {163return false;164}165ResponseAPDU other = (ResponseAPDU)obj;166return Arrays.equals(this.apdu, other.apdu);167}168169/**170* Returns the hash code value for this response APDU.171*172* @return the hash code value for this response APDU.173*/174public int hashCode() {175return Arrays.hashCode(apdu);176}177178private void readObject(java.io.ObjectInputStream in)179throws java.io.IOException, ClassNotFoundException {180apdu = (byte[])in.readUnshared();181check(apdu);182}183184}185186187