< prev index next >

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

Print this page




 146      * incremented.
 147      *
 148      * @param  cbuf
 149      *         Destination buffer
 150      *
 151      * @param  off
 152      *         Offset at which to start storing characters
 153      *
 154      * @param  len
 155      *         Maximum number of characters to read
 156      *
 157      * @return  The number of bytes read, or -1 if the end of the stream has
 158      *          already been reached
 159      *
 160      * @throws  IOException
 161      *          If an I/O error occurs
 162      *
 163      * @throws  IndexOutOfBoundsException {@inheritDoc}
 164      */
 165     @SuppressWarnings("fallthrough")
 166     public int read(char cbuf[], int off, int len) throws IOException {
 167         synchronized (lock) {
 168             int n = super.read(cbuf, off, len);
 169 
 170             for (int i = off; i < off + n; i++) {
 171                 int c = cbuf[i];
 172                 if (skipLF) {
 173                     skipLF = false;
 174                     if (c == '\n')
 175                         continue;
 176                 }
 177                 switch (c) {
 178                 case '\r':
 179                     skipLF = true;
 180                 case '\n':      /* Fall through */
 181                     lineNumber++;
 182                     break;
 183                 }
 184             }
 185 
 186             return n;


 195      *          any <a href="#lt">line termination characters</a>, or
 196      *          {@code null} if the end of the stream has been reached
 197      *
 198      * @throws  IOException
 199      *          If an I/O error occurs
 200      */
 201     public String readLine() throws IOException {
 202         synchronized (lock) {
 203             String l = super.readLine(skipLF);
 204             skipLF = false;
 205             if (l != null)
 206                 lineNumber++;
 207             return l;
 208         }
 209     }
 210 
 211     /** Maximum skip-buffer size */
 212     private static final int maxSkipBufferSize = 8192;
 213 
 214     /** Skip buffer, null until allocated */
 215     private char skipBuffer[] = null;
 216 
 217     /**
 218      * Skip characters.
 219      *
 220      * @param  n
 221      *         The number of characters to skip
 222      *
 223      * @return  The number of characters actually skipped
 224      *
 225      * @throws  IOException
 226      *          If an I/O error occurs
 227      *
 228      * @throws  IllegalArgumentException
 229      *          If {@code n} is negative
 230      */
 231     public long skip(long n) throws IOException {
 232         if (n < 0)
 233             throw new IllegalArgumentException("skip() value is negative");
 234         int nn = (int) Math.min(n, maxSkipBufferSize);
 235         synchronized (lock) {




 146      * incremented.
 147      *
 148      * @param  cbuf
 149      *         Destination buffer
 150      *
 151      * @param  off
 152      *         Offset at which to start storing characters
 153      *
 154      * @param  len
 155      *         Maximum number of characters to read
 156      *
 157      * @return  The number of bytes read, or -1 if the end of the stream has
 158      *          already been reached
 159      *
 160      * @throws  IOException
 161      *          If an I/O error occurs
 162      *
 163      * @throws  IndexOutOfBoundsException {@inheritDoc}
 164      */
 165     @SuppressWarnings("fallthrough")
 166     public int read(char[] cbuf, int off, int len) throws IOException {
 167         synchronized (lock) {
 168             int n = super.read(cbuf, off, len);
 169 
 170             for (int i = off; i < off + n; i++) {
 171                 int c = cbuf[i];
 172                 if (skipLF) {
 173                     skipLF = false;
 174                     if (c == '\n')
 175                         continue;
 176                 }
 177                 switch (c) {
 178                 case '\r':
 179                     skipLF = true;
 180                 case '\n':      /* Fall through */
 181                     lineNumber++;
 182                     break;
 183                 }
 184             }
 185 
 186             return n;


 195      *          any <a href="#lt">line termination characters</a>, or
 196      *          {@code null} if the end of the stream has been reached
 197      *
 198      * @throws  IOException
 199      *          If an I/O error occurs
 200      */
 201     public String readLine() throws IOException {
 202         synchronized (lock) {
 203             String l = super.readLine(skipLF);
 204             skipLF = false;
 205             if (l != null)
 206                 lineNumber++;
 207             return l;
 208         }
 209     }
 210 
 211     /** Maximum skip-buffer size */
 212     private static final int maxSkipBufferSize = 8192;
 213 
 214     /** Skip buffer, null until allocated */
 215     private char[] skipBuffer = null;
 216 
 217     /**
 218      * Skip characters.
 219      *
 220      * @param  n
 221      *         The number of characters to skip
 222      *
 223      * @return  The number of characters actually skipped
 224      *
 225      * @throws  IOException
 226      *          If an I/O error occurs
 227      *
 228      * @throws  IllegalArgumentException
 229      *          If {@code n} is negative
 230      */
 231     public long skip(long n) throws IOException {
 232         if (n < 0)
 233             throw new IllegalArgumentException("skip() value is negative");
 234         int nn = (int) Math.min(n, maxSkipBufferSize);
 235         synchronized (lock) {


< prev index next >