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

Print this page




 142 
 143     /**
 144      * Read characters into a portion of an array.  Whenever a <a
 145      * href="#lt">line terminator</a> is read the current line number is
 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     @SuppressWarnings("fallthrough")
 164     public int read(char cbuf[], int off, int len) throws IOException {
 165         synchronized (lock) {
 166             int n = super.read(cbuf, off, len);
 167 
 168             for (int i = off; i < off + n; i++) {
 169                 int c = cbuf[i];
 170                 if (skipLF) {
 171                     skipLF = false;
 172                     if (c == '\n')
 173                         continue;
 174                 }
 175                 switch (c) {
 176                 case '\r':
 177                     skipLF = true;
 178                 case '\n':      /* Fall through */
 179                     lineNumber++;
 180                     break;
 181                 }




 142 
 143     /**
 144      * Read characters into a portion of an array.  Whenever a <a
 145      * href="#lt">line terminator</a> is read the current line number is
 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                 }