src/share/classes/java/net/SocketInputStream.java

Print this page
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


  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)
  97         throws IOException;

















  98 
  99     /**
 100      * Reads into a byte array data from the socket.
 101      * @param b the buffer into which the data is read
 102      * @return the actual number of bytes read, -1 is
 103      *          returned when the end of the stream is reached.
 104      * @exception IOException If an I/O error has occurred.
 105      */
 106     public int read(byte b[]) throws IOException {
 107         return read(b, 0, b.length);
 108     }
 109 
 110     /**
 111      * Reads into a byte array <i>b</i> at offset <i>off</i>,
 112      * <i>length</i> bytes of data.
 113      * @param b the buffer into which the data is read
 114      * @param off the start offset of the data
 115      * @param length the maximum number of bytes read
 116      * @return the actual number of bytes read, -1 is
 117      *          returned when the end of the stream is reached.


 130         }
 131 
 132         // connection reset
 133         if (impl.isConnectionReset()) {
 134             throw new SocketException("Connection reset");
 135         }
 136 
 137         // bounds check
 138         if (length <= 0 || off < 0 || off + length > b.length) {
 139             if (length == 0) {
 140                 return 0;
 141             }
 142             throw new ArrayIndexOutOfBoundsException();
 143         }
 144 
 145         boolean gotReset = false;
 146 
 147         // acquire file descriptor and do the read
 148         FileDescriptor fd = impl.acquireFD();
 149         try {
 150             n = socketRead0(fd, b, off, length, timeout);
 151             if (n > 0) {
 152                 return n;
 153             }
 154         } catch (ConnectionResetException rstExc) {
 155             gotReset = true;
 156         } finally {
 157             impl.releaseFD();
 158         }
 159 
 160         /*
 161          * We receive a "connection reset" but there may be bytes still
 162          * buffered on the socket
 163          */
 164         if (gotReset) {
 165             impl.setConnectionResetPending();
 166             impl.acquireFD();
 167             try {
 168                 n = socketRead0(fd, b, off, length, timeout);
 169                 if (n > 0) {
 170                     return n;
 171                 }
 172             } catch (ConnectionResetException rstExc) {
 173             } finally {
 174                 impl.releaseFD();
 175             }
 176         }
 177 
 178         /*
 179          * If we get here we are at EOF, the socket has been closed,
 180          * or the connection has been reset.
 181          */
 182         if (impl.isClosedOrPending()) {
 183             throw new SocketException("Socket closed");
 184         }
 185         if (impl.isConnectionResetPending()) {
 186             impl.setConnectionReset();
 187         }
 188         if (impl.isConnectionReset()) {




  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.


 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()) {