rev 10457 : 8054720: Modifications of I/O methods for instrumentation purposes
Summary: Wrap some native methods in Java methods and change native method names to a consistent form.
Reviewed-by: TBD

   1 /*
   2  * Copyright (c) 1995, 2013, Oracle and/or its affiliates. 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.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package java.net;
  27 
  28 import java.io.FileDescriptor;
  29 import java.io.FileInputStream;
  30 import java.io.IOException;
  31 import java.nio.channels.FileChannel;
  32 
  33 import sun.net.ConnectionResetException;
  34 
  35 /**
  36  * This stream extends FileInputStream to implement a
  37  * SocketInputStream. Note that this class should <b>NOT</b> be
  38  * public.
  39  *
  40  * @author      Jonathan Payne
  41  * @author      Arthur van Hoff
  42  */
  43 class SocketInputStream extends FileInputStream
  44 {
  45     static {
  46         init();
  47     }
  48 
  49     private boolean eof;
  50     private AbstractPlainSocketImpl impl = null;
  51     private byte temp[];
  52     private Socket socket = null;
  53 
  54     /**
  55      * Creates a new SocketInputStream. Can only be called
  56      * by a Socket. This method needs to hang on to the owner Socket so
  57      * that the fd will not be closed.
  58      * @param impl the implemented socket input stream
  59      */
  60     SocketInputStream(AbstractPlainSocketImpl impl) throws IOException {
  61         super(impl.getFileDescriptor());
  62         this.impl = impl;
  63         socket = impl.getSocket();
  64     }
  65 
  66     /**
  67      * Returns the unique {@link java.nio.channels.FileChannel FileChannel}
  68      * object associated with this file input stream.</p>
  69      *
  70      * The {@code getChannel} method of {@code SocketInputStream}
  71      * returns {@code null} since it is a socket based stream.</p>
  72      *
  73      * @return  the file channel associated with this file input stream
  74      *
  75      * @since 1.4
  76      * @spec JSR-51
  77      */
  78     public final FileChannel getChannel() {
  79         return null;
  80     }
  81 
  82     /**
  83      * Reads into an array of bytes at the specified offset using
  84      * the received socket primitive.
  85      * @param fd the FileDescriptor
  86      * @param b the buffer into which the data is read
  87      * @param off the start offset of the data
  88      * @param len the maximum number of bytes read
  89      * @param timeout the read timeout in ms
  90      * @return the actual number of bytes read, -1 is
  91      *          returned when the end of the stream is reached.
  92      * @exception IOException If an I/O error has occurred.
  93      */
  94     private native int socketRead0(FileDescriptor fd,
  95                                    byte b[], int off, int len,
  96                                    int timeout) throws IOException;
  97 
  98     /**
  99      * Reads into an array of bytes at the specified offset using
 100      * the received socket primitive.
 101      * @param fd the FileDescriptor
 102      * @param b the buffer into which the data is read
 103      * @param off the start offset of the data
 104      * @param len the maximum number of bytes read
 105      * @param timeout the read timeout in ms
 106      * @return the actual number of bytes read, -1 is
 107      *          returned when the end of the stream is reached.
 108      * @exception IOException If an I/O error has occurred.
 109      */
 110     private int socketRead(FileDescriptor fd,
 111                            byte b[], int off, int len,
 112                            int timeout) throws IOException {
 113         return socketRead0(fd, b, off, len, timeout);
 114     }
 115 
 116     /**
 117      * Reads into a byte array data from the socket.
 118      * @param b the buffer into which the data is read
 119      * @return the actual number of bytes read, -1 is
 120      *          returned when the end of the stream is reached.
 121      * @exception IOException If an I/O error has occurred.
 122      */
 123     public int read(byte b[]) throws IOException {
 124         return read(b, 0, b.length);
 125     }
 126 
 127     /**
 128      * Reads into a byte array <i>b</i> at offset <i>off</i>,
 129      * <i>length</i> bytes of data.
 130      * @param b the buffer into which the data is read
 131      * @param off the start offset of the data
 132      * @param length the maximum number of bytes read
 133      * @return the actual number of bytes read, -1 is
 134      *          returned when the end of the stream is reached.
 135      * @exception IOException If an I/O error has occurred.
 136      */
 137     public int read(byte b[], int off, int length) throws IOException {
 138         return read(b, off, length, impl.getTimeout());
 139     }
 140 
 141     int read(byte b[], int off, int length, int timeout) throws IOException {
 142         int n;
 143 
 144         // EOF already encountered
 145         if (eof) {
 146             return -1;
 147         }
 148 
 149         // connection reset
 150         if (impl.isConnectionReset()) {
 151             throw new SocketException("Connection reset");
 152         }
 153 
 154         // bounds check
 155         if (length <= 0 || off < 0 || off + length > b.length) {
 156             if (length == 0) {
 157                 return 0;
 158             }
 159             throw new ArrayIndexOutOfBoundsException();
 160         }
 161 
 162         boolean gotReset = false;
 163 
 164         // acquire file descriptor and do the read
 165         FileDescriptor fd = impl.acquireFD();
 166         try {
 167             n = socketRead(fd, b, off, length, timeout);
 168             if (n > 0) {
 169                 return n;
 170             }
 171         } catch (ConnectionResetException rstExc) {
 172             gotReset = true;
 173         } finally {
 174             impl.releaseFD();
 175         }
 176 
 177         /*
 178          * We receive a "connection reset" but there may be bytes still
 179          * buffered on the socket
 180          */
 181         if (gotReset) {
 182             impl.setConnectionResetPending();
 183             impl.acquireFD();
 184             try {
 185                 n = socketRead(fd, b, off, length, timeout);
 186                 if (n > 0) {
 187                     return n;
 188                 }
 189             } catch (ConnectionResetException rstExc) {
 190             } finally {
 191                 impl.releaseFD();
 192             }
 193         }
 194 
 195         /*
 196          * If we get here we are at EOF, the socket has been closed,
 197          * or the connection has been reset.
 198          */
 199         if (impl.isClosedOrPending()) {
 200             throw new SocketException("Socket closed");
 201         }
 202         if (impl.isConnectionResetPending()) {
 203             impl.setConnectionReset();
 204         }
 205         if (impl.isConnectionReset()) {
 206             throw new SocketException("Connection reset");
 207         }
 208         eof = true;
 209         return -1;
 210     }
 211 
 212     /**
 213      * Reads a single byte from the socket.
 214      */
 215     public int read() throws IOException {
 216         if (eof) {
 217             return -1;
 218         }
 219         temp = new byte[1];
 220         int n = read(temp, 0, 1);
 221         if (n <= 0) {
 222             return -1;
 223         }
 224         return temp[0] & 0xff;
 225     }
 226 
 227     /**
 228      * Skips n bytes of input.
 229      * @param numbytes the number of bytes to skip
 230      * @return  the actual number of bytes skipped.
 231      * @exception IOException If an I/O error has occurred.
 232      */
 233     public long skip(long numbytes) throws IOException {
 234         if (numbytes <= 0) {
 235             return 0;
 236         }
 237         long n = numbytes;
 238         int buflen = (int) Math.min(1024, n);
 239         byte data[] = new byte[buflen];
 240         while (n > 0) {
 241             int r = read(data, 0, (int) Math.min((long) buflen, n));
 242             if (r < 0) {
 243                 break;
 244             }
 245             n -= r;
 246         }
 247         return numbytes - n;
 248     }
 249 
 250     /**
 251      * Returns the number of bytes that can be read without blocking.
 252      * @return the number of immediately available bytes
 253      */
 254     public int available() throws IOException {
 255         return impl.available();
 256     }
 257 
 258     /**
 259      * Closes the stream.
 260      */
 261     private boolean closing = false;
 262     public void close() throws IOException {
 263         // Prevent recursion. See BugId 4484411
 264         if (closing)
 265             return;
 266         closing = true;
 267         if (socket != null) {
 268             if (!socket.isClosed())
 269                 socket.close();
 270         } else
 271             impl.close();
 272         closing = false;
 273     }
 274 
 275     void setEOF(boolean eof) {
 276         this.eof = eof;
 277     }
 278 
 279     /**
 280      * Overrides finalize, the fd is closed by the Socket.
 281      */
 282     protected void finalize() {}
 283 
 284     /**
 285      * Perform class load-time initializations.
 286      */
 287     private native static void init();
 288 }
--- EOF ---