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

Print this page




  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 len 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.
 118      * @exception IOException If an I/O error has occurred.
 119      */
 120     public int read(byte b[], int off, int length) throws IOException {
 121         return read(b, off, length, impl.getTimeout());
 122     }
 123 
 124     int read(byte b[], int off, int length, int timeout) throws IOException {
 125         int n;
 126 
 127         // EOF already encountered
 128         if (eof) {
 129             return -1;
 130         }
 131 
 132         // connection reset
 133         if (impl.isConnectionReset()) {
 134             throw new SocketException("Connection reset");
 135         }


 192         return -1;
 193     }
 194 
 195     /**
 196      * Reads a single byte from the socket.
 197      */
 198     public int read() throws IOException {
 199         if (eof) {
 200             return -1;
 201         }
 202         temp = new byte[1];
 203         int n = read(temp, 0, 1);
 204         if (n <= 0) {
 205             return -1;
 206         }
 207         return temp[0] & 0xff;
 208     }
 209 
 210     /**
 211      * Skips n bytes of input.
 212      * @param n the number of bytes to skip
 213      * @return  the actual number of bytes skipped.
 214      * @exception IOException If an I/O error has occurred.
 215      */
 216     public long skip(long numbytes) throws IOException {
 217         if (numbytes <= 0) {
 218             return 0;
 219         }
 220         long n = numbytes;
 221         int buflen = (int) Math.min(1024, n);
 222         byte data[] = new byte[buflen];
 223         while (n > 0) {
 224             int r = read(data, 0, (int) Math.min((long) buflen, n));
 225             if (r < 0) {
 226                 break;
 227             }
 228             n -= r;
 229         }
 230         return numbytes - n;
 231     }
 232 




  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.
 118      * @exception IOException If an I/O error has occurred.
 119      */
 120     public int read(byte b[], int off, int length) throws IOException {
 121         return read(b, off, length, impl.getTimeout());
 122     }
 123 
 124     int read(byte b[], int off, int length, int timeout) throws IOException {
 125         int n;
 126 
 127         // EOF already encountered
 128         if (eof) {
 129             return -1;
 130         }
 131 
 132         // connection reset
 133         if (impl.isConnectionReset()) {
 134             throw new SocketException("Connection reset");
 135         }


 192         return -1;
 193     }
 194 
 195     /**
 196      * Reads a single byte from the socket.
 197      */
 198     public int read() throws IOException {
 199         if (eof) {
 200             return -1;
 201         }
 202         temp = new byte[1];
 203         int n = read(temp, 0, 1);
 204         if (n <= 0) {
 205             return -1;
 206         }
 207         return temp[0] & 0xff;
 208     }
 209 
 210     /**
 211      * Skips n bytes of input.
 212      * @param numbytes the number of bytes to skip
 213      * @return  the actual number of bytes skipped.
 214      * @exception IOException If an I/O error has occurred.
 215      */
 216     public long skip(long numbytes) throws IOException {
 217         if (numbytes <= 0) {
 218             return 0;
 219         }
 220         long n = numbytes;
 221         int buflen = (int) Math.min(1024, n);
 222         byte data[] = new byte[buflen];
 223         while (n > 0) {
 224             int r = read(data, 0, (int) Math.min((long) buflen, n));
 225             if (r < 0) {
 226                 break;
 227             }
 228             n -= r;
 229         }
 230         return numbytes - n;
 231     }
 232