rt/emul/compact/src/main/java/java/io/LineNumberReader.java
author Jaroslav Tulach <jtulach@netbeans.org>
Thu, 03 Oct 2013 15:40:35 +0200
branchjdk7-b147
changeset 1334 588d5bf7a560
permissions -rw-r--r--
Set of JDK classes needed to run javac
jtulach@1334
     1
/*
jtulach@1334
     2
 * Copyright (c) 1996, 2006, Oracle and/or its affiliates. All rights reserved.
jtulach@1334
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
jtulach@1334
     4
 *
jtulach@1334
     5
 * This code is free software; you can redistribute it and/or modify it
jtulach@1334
     6
 * under the terms of the GNU General Public License version 2 only, as
jtulach@1334
     7
 * published by the Free Software Foundation.  Oracle designates this
jtulach@1334
     8
 * particular file as subject to the "Classpath" exception as provided
jtulach@1334
     9
 * by Oracle in the LICENSE file that accompanied this code.
jtulach@1334
    10
 *
jtulach@1334
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
jtulach@1334
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
jtulach@1334
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
jtulach@1334
    14
 * version 2 for more details (a copy is included in the LICENSE file that
jtulach@1334
    15
 * accompanied this code).
jtulach@1334
    16
 *
jtulach@1334
    17
 * You should have received a copy of the GNU General Public License version
jtulach@1334
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
jtulach@1334
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
jtulach@1334
    20
 *
jtulach@1334
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
jtulach@1334
    22
 * or visit www.oracle.com if you need additional information or have any
jtulach@1334
    23
 * questions.
jtulach@1334
    24
 */
jtulach@1334
    25
jtulach@1334
    26
package java.io;
jtulach@1334
    27
jtulach@1334
    28
jtulach@1334
    29
/**
jtulach@1334
    30
 * A buffered character-input stream that keeps track of line numbers.  This
jtulach@1334
    31
 * class defines methods {@link #setLineNumber(int)} and {@link
jtulach@1334
    32
 * #getLineNumber()} for setting and getting the current line number
jtulach@1334
    33
 * respectively.
jtulach@1334
    34
 *
jtulach@1334
    35
 * <p> By default, line numbering begins at 0. This number increments at every
jtulach@1334
    36
 * <a href="#lt">line terminator</a> as the data is read, and can be changed
jtulach@1334
    37
 * with a call to <tt>setLineNumber(int)</tt>.  Note however, that
jtulach@1334
    38
 * <tt>setLineNumber(int)</tt> does not actually change the current position in
jtulach@1334
    39
 * the stream; it only changes the value that will be returned by
jtulach@1334
    40
 * <tt>getLineNumber()</tt>.
jtulach@1334
    41
 *
jtulach@1334
    42
 * <p> A line is considered to be <a name="lt">terminated</a> by any one of a
jtulach@1334
    43
 * line feed ('\n'), a carriage return ('\r'), or a carriage return followed
jtulach@1334
    44
 * immediately by a linefeed.
jtulach@1334
    45
 *
jtulach@1334
    46
 * @author      Mark Reinhold
jtulach@1334
    47
 * @since       JDK1.1
jtulach@1334
    48
 */
jtulach@1334
    49
jtulach@1334
    50
public class LineNumberReader extends BufferedReader {
jtulach@1334
    51
jtulach@1334
    52
    /** The current line number */
jtulach@1334
    53
    private int lineNumber = 0;
jtulach@1334
    54
jtulach@1334
    55
    /** The line number of the mark, if any */
jtulach@1334
    56
    private int markedLineNumber; // Defaults to 0
jtulach@1334
    57
jtulach@1334
    58
    /** If the next character is a line feed, skip it */
jtulach@1334
    59
    private boolean skipLF;
jtulach@1334
    60
jtulach@1334
    61
    /** The skipLF flag when the mark was set */
jtulach@1334
    62
    private boolean markedSkipLF;
jtulach@1334
    63
jtulach@1334
    64
    /**
jtulach@1334
    65
     * Create a new line-numbering reader, using the default input-buffer
jtulach@1334
    66
     * size.
jtulach@1334
    67
     *
jtulach@1334
    68
     * @param  in
jtulach@1334
    69
     *         A Reader object to provide the underlying stream
jtulach@1334
    70
     */
jtulach@1334
    71
    public LineNumberReader(Reader in) {
jtulach@1334
    72
        super(in);
jtulach@1334
    73
    }
jtulach@1334
    74
jtulach@1334
    75
    /**
jtulach@1334
    76
     * Create a new line-numbering reader, reading characters into a buffer of
jtulach@1334
    77
     * the given size.
jtulach@1334
    78
     *
jtulach@1334
    79
     * @param  in
jtulach@1334
    80
     *         A Reader object to provide the underlying stream
jtulach@1334
    81
     *
jtulach@1334
    82
     * @param  sz
jtulach@1334
    83
     *         An int specifying the size of the buffer
jtulach@1334
    84
     */
jtulach@1334
    85
    public LineNumberReader(Reader in, int sz) {
jtulach@1334
    86
        super(in, sz);
jtulach@1334
    87
    }
jtulach@1334
    88
jtulach@1334
    89
    /**
jtulach@1334
    90
     * Set the current line number.
jtulach@1334
    91
     *
jtulach@1334
    92
     * @param  lineNumber
jtulach@1334
    93
     *         An int specifying the line number
jtulach@1334
    94
     *
jtulach@1334
    95
     * @see #getLineNumber
jtulach@1334
    96
     */
jtulach@1334
    97
    public void setLineNumber(int lineNumber) {
jtulach@1334
    98
        this.lineNumber = lineNumber;
jtulach@1334
    99
    }
jtulach@1334
   100
jtulach@1334
   101
    /**
jtulach@1334
   102
     * Get the current line number.
jtulach@1334
   103
     *
jtulach@1334
   104
     * @return  The current line number
jtulach@1334
   105
     *
jtulach@1334
   106
     * @see #setLineNumber
jtulach@1334
   107
     */
jtulach@1334
   108
    public int getLineNumber() {
jtulach@1334
   109
        return lineNumber;
jtulach@1334
   110
    }
jtulach@1334
   111
jtulach@1334
   112
    /**
jtulach@1334
   113
     * Read a single character.  <a href="#lt">Line terminators</a> are
jtulach@1334
   114
     * compressed into single newline ('\n') characters.  Whenever a line
jtulach@1334
   115
     * terminator is read the current line number is incremented.
jtulach@1334
   116
     *
jtulach@1334
   117
     * @return  The character read, or -1 if the end of the stream has been
jtulach@1334
   118
     *          reached
jtulach@1334
   119
     *
jtulach@1334
   120
     * @throws  IOException
jtulach@1334
   121
     *          If an I/O error occurs
jtulach@1334
   122
     */
jtulach@1334
   123
    public int read() throws IOException {
jtulach@1334
   124
        synchronized (lock) {
jtulach@1334
   125
            int c = super.read();
jtulach@1334
   126
            if (skipLF) {
jtulach@1334
   127
                if (c == '\n')
jtulach@1334
   128
                    c = super.read();
jtulach@1334
   129
                skipLF = false;
jtulach@1334
   130
            }
jtulach@1334
   131
            switch (c) {
jtulach@1334
   132
            case '\r':
jtulach@1334
   133
                skipLF = true;
jtulach@1334
   134
            case '\n':          /* Fall through */
jtulach@1334
   135
                lineNumber++;
jtulach@1334
   136
                return '\n';
jtulach@1334
   137
            }
jtulach@1334
   138
            return c;
jtulach@1334
   139
        }
jtulach@1334
   140
    }
jtulach@1334
   141
jtulach@1334
   142
    /**
jtulach@1334
   143
     * Read characters into a portion of an array.  Whenever a <a
jtulach@1334
   144
     * href="#lt">line terminator</a> is read the current line number is
jtulach@1334
   145
     * incremented.
jtulach@1334
   146
     *
jtulach@1334
   147
     * @param  cbuf
jtulach@1334
   148
     *         Destination buffer
jtulach@1334
   149
     *
jtulach@1334
   150
     * @param  off
jtulach@1334
   151
     *         Offset at which to start storing characters
jtulach@1334
   152
     *
jtulach@1334
   153
     * @param  len
jtulach@1334
   154
     *         Maximum number of characters to read
jtulach@1334
   155
     *
jtulach@1334
   156
     * @return  The number of bytes read, or -1 if the end of the stream has
jtulach@1334
   157
     *          already been reached
jtulach@1334
   158
     *
jtulach@1334
   159
     * @throws  IOException
jtulach@1334
   160
     *          If an I/O error occurs
jtulach@1334
   161
     */
jtulach@1334
   162
    public int read(char cbuf[], int off, int len) throws IOException {
jtulach@1334
   163
        synchronized (lock) {
jtulach@1334
   164
            int n = super.read(cbuf, off, len);
jtulach@1334
   165
jtulach@1334
   166
            for (int i = off; i < off + n; i++) {
jtulach@1334
   167
                int c = cbuf[i];
jtulach@1334
   168
                if (skipLF) {
jtulach@1334
   169
                    skipLF = false;
jtulach@1334
   170
                    if (c == '\n')
jtulach@1334
   171
                        continue;
jtulach@1334
   172
                }
jtulach@1334
   173
                switch (c) {
jtulach@1334
   174
                case '\r':
jtulach@1334
   175
                    skipLF = true;
jtulach@1334
   176
                case '\n':      /* Fall through */
jtulach@1334
   177
                    lineNumber++;
jtulach@1334
   178
                    break;
jtulach@1334
   179
                }
jtulach@1334
   180
            }
jtulach@1334
   181
jtulach@1334
   182
            return n;
jtulach@1334
   183
        }
jtulach@1334
   184
    }
jtulach@1334
   185
jtulach@1334
   186
    /**
jtulach@1334
   187
     * Read a line of text.  Whenever a <a href="#lt">line terminator</a> is
jtulach@1334
   188
     * read the current line number is incremented.
jtulach@1334
   189
     *
jtulach@1334
   190
     * @return  A String containing the contents of the line, not including
jtulach@1334
   191
     *          any <a href="#lt">line termination characters</a>, or
jtulach@1334
   192
     *          <tt>null</tt> if the end of the stream has been reached
jtulach@1334
   193
     *
jtulach@1334
   194
     * @throws  IOException
jtulach@1334
   195
     *          If an I/O error occurs
jtulach@1334
   196
     */
jtulach@1334
   197
    public String readLine() throws IOException {
jtulach@1334
   198
        synchronized (lock) {
jtulach@1334
   199
            String l = super.readLine(skipLF);
jtulach@1334
   200
            skipLF = false;
jtulach@1334
   201
            if (l != null)
jtulach@1334
   202
                lineNumber++;
jtulach@1334
   203
            return l;
jtulach@1334
   204
        }
jtulach@1334
   205
    }
jtulach@1334
   206
jtulach@1334
   207
    /** Maximum skip-buffer size */
jtulach@1334
   208
    private static final int maxSkipBufferSize = 8192;
jtulach@1334
   209
jtulach@1334
   210
    /** Skip buffer, null until allocated */
jtulach@1334
   211
    private char skipBuffer[] = null;
jtulach@1334
   212
jtulach@1334
   213
    /**
jtulach@1334
   214
     * Skip characters.
jtulach@1334
   215
     *
jtulach@1334
   216
     * @param  n
jtulach@1334
   217
     *         The number of characters to skip
jtulach@1334
   218
     *
jtulach@1334
   219
     * @return  The number of characters actually skipped
jtulach@1334
   220
     *
jtulach@1334
   221
     * @throws  IOException
jtulach@1334
   222
     *          If an I/O error occurs
jtulach@1334
   223
     *
jtulach@1334
   224
     * @throws  IllegalArgumentException
jtulach@1334
   225
     *          If <tt>n</tt> is negative
jtulach@1334
   226
     */
jtulach@1334
   227
    public long skip(long n) throws IOException {
jtulach@1334
   228
        if (n < 0)
jtulach@1334
   229
            throw new IllegalArgumentException("skip() value is negative");
jtulach@1334
   230
        int nn = (int) Math.min(n, maxSkipBufferSize);
jtulach@1334
   231
        synchronized (lock) {
jtulach@1334
   232
            if ((skipBuffer == null) || (skipBuffer.length < nn))
jtulach@1334
   233
                skipBuffer = new char[nn];
jtulach@1334
   234
            long r = n;
jtulach@1334
   235
            while (r > 0) {
jtulach@1334
   236
                int nc = read(skipBuffer, 0, (int) Math.min(r, nn));
jtulach@1334
   237
                if (nc == -1)
jtulach@1334
   238
                    break;
jtulach@1334
   239
                r -= nc;
jtulach@1334
   240
            }
jtulach@1334
   241
            return n - r;
jtulach@1334
   242
        }
jtulach@1334
   243
    }
jtulach@1334
   244
jtulach@1334
   245
    /**
jtulach@1334
   246
     * Mark the present position in the stream.  Subsequent calls to reset()
jtulach@1334
   247
     * will attempt to reposition the stream to this point, and will also reset
jtulach@1334
   248
     * the line number appropriately.
jtulach@1334
   249
     *
jtulach@1334
   250
     * @param  readAheadLimit
jtulach@1334
   251
     *         Limit on the number of characters that may be read while still
jtulach@1334
   252
     *         preserving the mark.  After reading this many characters,
jtulach@1334
   253
     *         attempting to reset the stream may fail.
jtulach@1334
   254
     *
jtulach@1334
   255
     * @throws  IOException
jtulach@1334
   256
     *          If an I/O error occurs
jtulach@1334
   257
     */
jtulach@1334
   258
    public void mark(int readAheadLimit) throws IOException {
jtulach@1334
   259
        synchronized (lock) {
jtulach@1334
   260
            super.mark(readAheadLimit);
jtulach@1334
   261
            markedLineNumber = lineNumber;
jtulach@1334
   262
            markedSkipLF     = skipLF;
jtulach@1334
   263
        }
jtulach@1334
   264
    }
jtulach@1334
   265
jtulach@1334
   266
    /**
jtulach@1334
   267
     * Reset the stream to the most recent mark.
jtulach@1334
   268
     *
jtulach@1334
   269
     * @throws  IOException
jtulach@1334
   270
     *          If the stream has not been marked, or if the mark has been
jtulach@1334
   271
     *          invalidated
jtulach@1334
   272
     */
jtulach@1334
   273
    public void reset() throws IOException {
jtulach@1334
   274
        synchronized (lock) {
jtulach@1334
   275
            super.reset();
jtulach@1334
   276
            lineNumber = markedLineNumber;
jtulach@1334
   277
            skipLF     = markedSkipLF;
jtulach@1334
   278
        }
jtulach@1334
   279
    }
jtulach@1334
   280
jtulach@1334
   281
}