jdk/src/share/classes/sun/security/ssl/AppInputStream.java
author xuelei
Mon, 14 Nov 2011 01:21:20 -0800
changeset 11018 be74e8b8f3eb
parent 5506 202f599c92aa
child 14664 e71aa0962e70
permissions -rw-r--r--
7111548: unexpected debug log message Reviewed-by: wetmore
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2061
diff changeset
     2
 * Copyright (c) 1996, 2009, 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: 2061
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: 2061
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: 2061
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2061
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2061
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
package sun.security.ssl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * InputStream for application data as returned by SSLSocket.getInputStream().
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * It uses an InputRecord as internal buffer that is refilled on demand
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * whenever it runs out of data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * @author David Brownell
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
class AppInputStream extends InputStream {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
    // static dummy array we use to implement skip()
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    private final static byte[] SKIP_ARRAY = new byte[1024];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    private SSLSocketImpl c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    InputRecord r;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    // One element array used to implement the single byte read() method
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    private final byte[] oneByte = new byte[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    AppInputStream(SSLSocketImpl conn) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
        r = new InputRecord();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        c = conn;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
     * Return the minimum number of bytes that can be read without blocking.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
     * Currently not synchronized.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    public int available() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        if (c.checkEOF() || (r.isAppDataValid() == false)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        return r.available();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     * Read a single byte, returning -1 on non-fault EOF status.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    public synchronized int read() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        int n = read(oneByte, 0, 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        if (n <= 0) { // EOF
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        return oneByte[0] & 0xff;
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
     * Read up to "len" bytes into this buffer, starting at "off".
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     * If the layer above needs more data, it asks for more, so we
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * are responsible only for blocking to fill at most one buffer,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     * and returning "-1" on non-fault EOF status.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    public synchronized int read(byte b[], int off, int len)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            throws IOException {
2061
5a32855b67d4 6697270: Inputstream dosent behave correct
xuelei
parents: 2
diff changeset
    84
        if (b == null) {
5a32855b67d4 6697270: Inputstream dosent behave correct
xuelei
parents: 2
diff changeset
    85
            throw new NullPointerException();
5a32855b67d4 6697270: Inputstream dosent behave correct
xuelei
parents: 2
diff changeset
    86
        } else if (off < 0 || len < 0 || len > b.length - off) {
5a32855b67d4 6697270: Inputstream dosent behave correct
xuelei
parents: 2
diff changeset
    87
            throw new IndexOutOfBoundsException();
5a32855b67d4 6697270: Inputstream dosent behave correct
xuelei
parents: 2
diff changeset
    88
        } else if (len == 0) {
5a32855b67d4 6697270: Inputstream dosent behave correct
xuelei
parents: 2
diff changeset
    89
            return 0;
5a32855b67d4 6697270: Inputstream dosent behave correct
xuelei
parents: 2
diff changeset
    90
        }
5a32855b67d4 6697270: Inputstream dosent behave correct
xuelei
parents: 2
diff changeset
    91
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        if (c.checkEOF()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
             * Read data if needed ... notice that the connection guarantees
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
             * that handshake, alert, and change cipher spec data streams are
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
             * handled as they arrive, so we never see them here.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            while (r.available() == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
                c.readDataRecord(r);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                if (c.checkEOF()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                    return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            int howmany = Math.min(len, r.available());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            howmany = r.read(b, off, howmany);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            return howmany;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            // shutdown and rethrow (wrapped) exception as appropriate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            c.handleException(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            // dummy for compiler
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    }
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
     * Skip n bytes. This implementation is somewhat less efficient
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * than possible, but not badly so (redundant copy). We reuse
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * the read() code to keep things simpler. Note that SKIP_ARRAY
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * is static and may garbled by concurrent use, but we are not interested
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * in the data anyway.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    public synchronized long skip(long n) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        long skipped = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        while (n > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            int len = (int)Math.min(n, SKIP_ARRAY.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            int r = read(SKIP_ARRAY, 0, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            if (r <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            n -= r;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            skipped += r;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        return skipped;
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
     * Socket close is already synchronized, no need to block here.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    public void close() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        c.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    // inherit default mark/reset behavior (throw Exceptions) from InputStream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
}