src/java.smartcardio/share/classes/javax/smartcardio/ResponseAPDU.java
author cushon
Wed, 09 Oct 2019 10:02:54 -0700
changeset 58521 55d2e10ea134
parent 47216 71c04702a3d5
permissions -rw-r--r--
8231990: Remove unnecessary else-if branch in Types.union Reviewed-by: mcimadamore Contributed-by: Brad Corso <bcorso@google.com>
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
23010
6dadb192ad81 8029235: Update copyright year to match last edit in jdk8 jdk repository for 2013
lana
parents: 18156
diff changeset
     2
 * Copyright (c) 2005, 2013, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package javax.smartcardio;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.util.Arrays;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * A response APDU as defined in ISO/IEC 7816-4. It consists of a conditional
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * body and a two byte trailer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * This class does not attempt to verify that the APDU encodes a semantically
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * valid response.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * <p>Instances of this class are immutable. Where data is passed in or out
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * via byte arrays, defensive cloning is performed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * @see CommandAPDU
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * @see CardChannel#transmit CardChannel.transmit
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * @since   1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * @author  Andreas Sterbenz
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * @author  JSR 268 Expert Group
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
public final class ResponseAPDU implements java.io.Serializable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    private static final long serialVersionUID = 6962744978375594225L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    /** @serial */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    private byte[] apdu;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
     * Constructs a ResponseAPDU from a byte array containing the complete
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
     * APDU contents (conditional body and trailed).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
     * <p>Note that the byte array is cloned to protect against subsequent
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
     * modification.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
     * @param apdu the complete response APDU
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
     * @throws NullPointerException if apdu is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
     * @throws IllegalArgumentException if apdu.length is less than 2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    public ResponseAPDU(byte[] apdu) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        apdu = apdu.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        check(apdu);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        this.apdu = apdu;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    private static void check(byte[] apdu) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        if (apdu.length < 2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
            throw new IllegalArgumentException("apdu must be at least 2 bytes long");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     * Returns the number of data bytes in the response body (Nr) or 0 if this
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * APDU has no body. This call is equivalent to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     * <code>getData().length</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * @return the number of data bytes in the response body or 0 if this APDU
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * has no body.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    public int getNr() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        return apdu.length - 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     * Returns a copy of the data bytes in the response body. If this APDU as
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     * no body, this method returns a byte array with a length of zero.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     * @return a copy of the data bytes in the response body or the empty
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     *    byte array if this APDU has no body.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    public byte[] getData() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        byte[] data = new byte[apdu.length - 2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        System.arraycopy(apdu, 0, data, 0, data.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        return data;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * Returns the value of the status byte SW1 as a value between 0 and 255.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * @return the value of the status byte SW1 as a value between 0 and 255.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    public int getSW1() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        return apdu[apdu.length - 2] & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * Returns the value of the status byte SW2 as a value between 0 and 255.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * @return the value of the status byte SW2 as a value between 0 and 255.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    public int getSW2() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        return apdu[apdu.length - 1] & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * Returns the value of the status bytes SW1 and SW2 as a single
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * status word SW.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * It is defined as
18156
edb590d448c5 8016217: More javadoc warnings
alanb
parents: 5506
diff changeset
   124
     * {@code (getSW1() << 8) | getSW2()}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * @return the value of the status word SW.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    public int getSW() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        return (getSW1() << 8) | getSW2();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * Returns a copy of the bytes in this APDU.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * @return a copy of the bytes in this APDU.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    public byte[] getBytes() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        return apdu.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * Returns a string representation of this response APDU.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * @return a String representation of this response APDU.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        return "ResponseAPDU: " + apdu.length + " bytes, SW="
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            + Integer.toHexString(getSW());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     * Compares the specified object with this response APDU for equality.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * Returns true if the given object is also a ResponseAPDU and its bytes are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * identical to the bytes in this ResponseAPDU.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * @param obj the object to be compared for equality with this response APDU
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * @return true if the specified object is equal to this response APDU
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    public boolean equals(Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        if (this == obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        if (obj instanceof ResponseAPDU == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        ResponseAPDU other = (ResponseAPDU)obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        return Arrays.equals(this.apdu, other.apdu);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     * Returns the hash code value for this response APDU.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     * @return the hash code value for this response APDU.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        return Arrays.hashCode(apdu);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    private void readObject(java.io.ObjectInputStream in)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            throws java.io.IOException, ClassNotFoundException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        apdu = (byte[])in.readUnshared();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        check(apdu);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
}