jdk/src/share/classes/sun/security/ssl/ByteBufferInputStream.java
author xuelei
Sat, 24 Nov 2012 04:09:19 -0800
changeset 14664 e71aa0962e70
parent 5506 202f599c92aa
child 23010 6dadb192ad81
permissions -rw-r--r--
8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl Reviewed-by: xuelei Contributed-by: Florian Weimer <fweimer@redhat.com>
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) 2003, 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
package sun.security.ssl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.nio.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * A simple InputStream which uses ByteBuffers as it's backing store.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * The only IOException should come if the InputStream has been closed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * All other IOException should not occur because all the data is local.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * Data reads on an exhausted ByteBuffer returns a -1.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * @author  Brad Wetmore
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
class ByteBufferInputStream extends InputStream {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    ByteBuffer bb;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    ByteBufferInputStream(ByteBuffer bb) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
        this.bb = bb;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
     * Returns a byte from the ByteBuffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
     * Increments position().
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
     */
14664
e71aa0962e70 8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents: 5506
diff changeset
    53
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    public int read() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        if (bb == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
            throw new IOException("read on a closed InputStream");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        if (bb.remaining() == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        return bb.get();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     * Returns a byte array from the ByteBuffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     * Increments position().
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     */
14664
e71aa0962e70 8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents: 5506
diff changeset
    71
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    public int read(byte b[]) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        if (bb == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
            throw new IOException("read on a closed InputStream");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        return read(b, 0, b.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * Returns a byte array from the ByteBuffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * Increments position().
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     */
14664
e71aa0962e70 8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents: 5506
diff changeset
    86
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    public int read(byte b[], int off, int len) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        if (bb == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            throw new IOException("read on a closed InputStream");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        if (b == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            throw new NullPointerException();
2061
5a32855b67d4 6697270: Inputstream dosent behave correct
xuelei
parents: 2
diff changeset
    95
        } else if (off < 0 || len < 0 || len > b.length - off) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            throw new IndexOutOfBoundsException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        } else if (len == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        int length = Math.min(bb.remaining(), len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        if (length == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        bb.get(b, off, length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        return length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * Skips over and discards <code>n</code> bytes of data from this input
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     */
14664
e71aa0962e70 8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents: 5506
diff changeset
   114
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    public long skip(long n) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        if (bb == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            throw new IOException("skip on a closed InputStream");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        if (n <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
         * ByteBuffers have at most an int, so lose the upper bits.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
         * The contract allows this.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        int nInt = (int) n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        int skip = Math.min(bb.remaining(), nInt);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        bb.position(bb.position() + skip);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        return nInt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * Returns the number of bytes that can be read (or skipped over)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * from this input stream without blocking by the next caller of a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * method for this input stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     */
14664
e71aa0962e70 8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents: 5506
diff changeset
   142
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    public int available() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        if (bb == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            throw new IOException("available on a closed InputStream");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        return bb.remaining();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * Closes this input stream and releases any system resources associated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * with the stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * @exception  IOException  if an I/O error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     */
14664
e71aa0962e70 8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents: 5506
diff changeset
   158
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    public void close() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        bb = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     * Marks the current position in this input stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     */
14664
e71aa0962e70 8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents: 5506
diff changeset
   166
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    public synchronized void mark(int readlimit) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * Repositions this stream to the position at the time the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     * <code>mark</code> method was last called on this input stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     */
14664
e71aa0962e70 8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents: 5506
diff changeset
   173
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    public synchronized void reset() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        throw new IOException("mark/reset not supported");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     * Tests if this input stream supports the <code>mark</code> and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * <code>reset</code> methods.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     */
14664
e71aa0962e70 8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents: 5506
diff changeset
   182
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    public boolean markSupported() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
}