< prev index next >

src/java.base/share/classes/java/io/LineNumberInputStream.java

Print this page




 110         return c;
 111     }
 112 
 113     /**
 114      * Reads up to {@code len} bytes of data from this input stream
 115      * into an array of bytes. This method blocks until some input is available.
 116      * <p>
 117      * The {@code read} method of
 118      * {@code LineNumberInputStream} repeatedly calls the
 119      * {@code read} method of zero arguments to fill in the byte array.
 120      *
 121      * @param      b     the buffer into which the data is read.
 122      * @param      off   the start offset of the data.
 123      * @param      len   the maximum number of bytes read.
 124      * @return     the total number of bytes read into the buffer, or
 125      *             {@code -1} if there is no more data because the end of
 126      *             this stream has been reached.
 127      * @exception  IOException  if an I/O error occurs.
 128      * @see        java.io.LineNumberInputStream#read()
 129      */
 130     public int read(byte b[], int off, int len) throws IOException {
 131         if (b == null) {
 132             throw new NullPointerException();
 133         } else if ((off < 0) || (off > b.length) || (len < 0) ||
 134                    ((off + len) > b.length) || ((off + len) < 0)) {
 135             throw new IndexOutOfBoundsException();
 136         } else if (len == 0) {
 137             return 0;
 138         }
 139 
 140         int c = read();
 141         if (c == -1) {
 142             return -1;
 143         }
 144         b[off] = (byte)c;
 145 
 146         int i = 1;
 147         try {
 148             for (; i < len ; i++) {
 149                 c = read();
 150                 if (c == -1) {


 162     /**
 163      * Skips over and discards {@code n} bytes of data from this
 164      * input stream. The {@code skip} method may, for a variety of
 165      * reasons, end up skipping over some smaller number of bytes,
 166      * possibly {@code 0}. The actual number of bytes skipped is
 167      * returned.  If {@code n} is negative, no bytes are skipped.
 168      * <p>
 169      * The {@code skip} method of {@code LineNumberInputStream} creates
 170      * a byte array and then repeatedly reads into it until
 171      * {@code n} bytes have been read or the end of the stream has
 172      * been reached.
 173      *
 174      * @param      n   the number of bytes to be skipped.
 175      * @return     the actual number of bytes skipped.
 176      * @exception  IOException  if an I/O error occurs.
 177      * @see        java.io.FilterInputStream#in
 178      */
 179     public long skip(long n) throws IOException {
 180         int chunk = 2048;
 181         long remaining = n;
 182         byte data[];
 183         int nr;
 184 
 185         if (n <= 0) {
 186             return 0;
 187         }
 188 
 189         data = new byte[chunk];
 190         while (remaining > 0) {
 191             nr = read(data, 0, (int) Math.min(chunk, remaining));
 192             if (nr < 0) {
 193                 break;
 194             }
 195             remaining -= nr;
 196         }
 197 
 198         return n - remaining;
 199     }
 200 
 201     /**
 202      * Sets the line number to the specified argument.




 110         return c;
 111     }
 112 
 113     /**
 114      * Reads up to {@code len} bytes of data from this input stream
 115      * into an array of bytes. This method blocks until some input is available.
 116      * <p>
 117      * The {@code read} method of
 118      * {@code LineNumberInputStream} repeatedly calls the
 119      * {@code read} method of zero arguments to fill in the byte array.
 120      *
 121      * @param      b     the buffer into which the data is read.
 122      * @param      off   the start offset of the data.
 123      * @param      len   the maximum number of bytes read.
 124      * @return     the total number of bytes read into the buffer, or
 125      *             {@code -1} if there is no more data because the end of
 126      *             this stream has been reached.
 127      * @exception  IOException  if an I/O error occurs.
 128      * @see        java.io.LineNumberInputStream#read()
 129      */
 130     public int read(byte[] b, int off, int len) throws IOException {
 131         if (b == null) {
 132             throw new NullPointerException();
 133         } else if ((off < 0) || (off > b.length) || (len < 0) ||
 134                    ((off + len) > b.length) || ((off + len) < 0)) {
 135             throw new IndexOutOfBoundsException();
 136         } else if (len == 0) {
 137             return 0;
 138         }
 139 
 140         int c = read();
 141         if (c == -1) {
 142             return -1;
 143         }
 144         b[off] = (byte)c;
 145 
 146         int i = 1;
 147         try {
 148             for (; i < len ; i++) {
 149                 c = read();
 150                 if (c == -1) {


 162     /**
 163      * Skips over and discards {@code n} bytes of data from this
 164      * input stream. The {@code skip} method may, for a variety of
 165      * reasons, end up skipping over some smaller number of bytes,
 166      * possibly {@code 0}. The actual number of bytes skipped is
 167      * returned.  If {@code n} is negative, no bytes are skipped.
 168      * <p>
 169      * The {@code skip} method of {@code LineNumberInputStream} creates
 170      * a byte array and then repeatedly reads into it until
 171      * {@code n} bytes have been read or the end of the stream has
 172      * been reached.
 173      *
 174      * @param      n   the number of bytes to be skipped.
 175      * @return     the actual number of bytes skipped.
 176      * @exception  IOException  if an I/O error occurs.
 177      * @see        java.io.FilterInputStream#in
 178      */
 179     public long skip(long n) throws IOException {
 180         int chunk = 2048;
 181         long remaining = n;
 182         byte[] data;
 183         int nr;
 184 
 185         if (n <= 0) {
 186             return 0;
 187         }
 188 
 189         data = new byte[chunk];
 190         while (remaining > 0) {
 191             nr = read(data, 0, (int) Math.min(chunk, remaining));
 192             if (nr < 0) {
 193                 break;
 194             }
 195             remaining -= nr;
 196         }
 197 
 198         return n - remaining;
 199     }
 200 
 201     /**
 202      * Sets the line number to the specified argument.


< prev index next >