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