jdk/src/share/classes/sun/security/ssl/ByteBufferInputStream.java
changeset 2 90ce3da70b43
child 2061 5a32855b67d4
equal deleted inserted replaced
0:fd16c54261b3 2:90ce3da70b43
       
     1 /*
       
     2  * Copyright 2003-2007 Sun Microsystems, Inc.  All Rights Reserved.
       
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       
     4  *
       
     5  * This code is free software; you can redistribute it and/or modify it
       
     6  * under the terms of the GNU General Public License version 2 only, as
       
     7  * published by the Free Software Foundation.  Sun designates this
       
     8  * particular file as subject to the "Classpath" exception as provided
       
     9  * by Sun in the LICENSE file that accompanied this code.
       
    10  *
       
    11  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    14  * version 2 for more details (a copy is included in the LICENSE file that
       
    15  * accompanied this code).
       
    16  *
       
    17  * You should have received a copy of the GNU General Public License version
       
    18  * 2 along with this work; if not, write to the Free Software Foundation,
       
    19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    20  *
       
    21  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
       
    22  * CA 95054 USA or visit www.sun.com if you need additional information or
       
    23  * have any questions.
       
    24  */
       
    25 
       
    26 package sun.security.ssl;
       
    27 
       
    28 import java.io.*;
       
    29 import java.nio.*;
       
    30 
       
    31 /**
       
    32  * A simple InputStream which uses ByteBuffers as it's backing store.
       
    33  * <P>
       
    34  * The only IOException should come if the InputStream has been closed.
       
    35  * All other IOException should not occur because all the data is local.
       
    36  * Data reads on an exhausted ByteBuffer returns a -1.
       
    37  *
       
    38  * @author  Brad Wetmore
       
    39  */
       
    40 class ByteBufferInputStream extends InputStream {
       
    41 
       
    42     ByteBuffer bb;
       
    43 
       
    44     ByteBufferInputStream(ByteBuffer bb) {
       
    45         this.bb = bb;
       
    46     }
       
    47 
       
    48     /**
       
    49      * Returns a byte from the ByteBuffer.
       
    50      *
       
    51      * Increments position().
       
    52      */
       
    53     public int read() throws IOException {
       
    54 
       
    55         if (bb == null) {
       
    56             throw new IOException("read on a closed InputStream");
       
    57         }
       
    58 
       
    59         if (bb.remaining() == 0) {
       
    60             return -1;
       
    61         }
       
    62         return bb.get();
       
    63     }
       
    64 
       
    65     /**
       
    66      * Returns a byte array from the ByteBuffer.
       
    67      *
       
    68      * Increments position().
       
    69      */
       
    70     public int read(byte b[]) throws IOException {
       
    71 
       
    72         if (bb == null) {
       
    73             throw new IOException("read on a closed InputStream");
       
    74         }
       
    75 
       
    76         return read(b, 0, b.length);
       
    77     }
       
    78 
       
    79     /**
       
    80      * Returns a byte array from the ByteBuffer.
       
    81      *
       
    82      * Increments position().
       
    83      */
       
    84     public int read(byte b[], int off, int len) throws IOException {
       
    85 
       
    86         if (bb == null) {
       
    87             throw new IOException("read on a closed InputStream");
       
    88         }
       
    89 
       
    90         if (b == null) {
       
    91             throw new NullPointerException();
       
    92         } else if ((off < 0) || (off > b.length) || (len < 0) ||
       
    93                    ((off + len) > b.length) || ((off + len) < 0)) {
       
    94             throw new IndexOutOfBoundsException();
       
    95         } else if (len == 0) {
       
    96             return 0;
       
    97         }
       
    98 
       
    99         int length = Math.min(bb.remaining(), len);
       
   100         if (length == 0) {
       
   101             return -1;
       
   102         }
       
   103 
       
   104         bb.get(b, off, length);
       
   105         return length;
       
   106     }
       
   107 
       
   108     /**
       
   109      * Skips over and discards <code>n</code> bytes of data from this input
       
   110      * stream.
       
   111      */
       
   112     public long skip(long n) throws IOException {
       
   113 
       
   114         if (bb == null) {
       
   115             throw new IOException("skip on a closed InputStream");
       
   116         }
       
   117 
       
   118         if (n <= 0) {
       
   119             return 0;
       
   120         }
       
   121 
       
   122         /*
       
   123          * ByteBuffers have at most an int, so lose the upper bits.
       
   124          * The contract allows this.
       
   125          */
       
   126         int nInt = (int) n;
       
   127         int skip = Math.min(bb.remaining(), nInt);
       
   128 
       
   129         bb.position(bb.position() + skip);
       
   130 
       
   131         return nInt;
       
   132     }
       
   133 
       
   134     /**
       
   135      * Returns the number of bytes that can be read (or skipped over)
       
   136      * from this input stream without blocking by the next caller of a
       
   137      * method for this input stream.
       
   138      */
       
   139     public int available() throws IOException {
       
   140 
       
   141         if (bb == null) {
       
   142             throw new IOException("available on a closed InputStream");
       
   143         }
       
   144 
       
   145         return bb.remaining();
       
   146     }
       
   147 
       
   148     /**
       
   149      * Closes this input stream and releases any system resources associated
       
   150      * with the stream.
       
   151      *
       
   152      * @exception  IOException  if an I/O error occurs.
       
   153      */
       
   154     public void close() throws IOException {
       
   155         bb = null;
       
   156     }
       
   157 
       
   158     /**
       
   159      * Marks the current position in this input stream.
       
   160      */
       
   161     public synchronized void mark(int readlimit) {}
       
   162 
       
   163     /**
       
   164      * Repositions this stream to the position at the time the
       
   165      * <code>mark</code> method was last called on this input stream.
       
   166      */
       
   167     public synchronized void reset() throws IOException {
       
   168         throw new IOException("mark/reset not supported");
       
   169     }
       
   170 
       
   171     /**
       
   172      * Tests if this input stream supports the <code>mark</code> and
       
   173      * <code>reset</code> methods.
       
   174      */
       
   175     public boolean markSupported() {
       
   176         return false;
       
   177     }
       
   178 }