8230342: LineNumberReader.getLineNumber() returns inconsistent results after EOF
Roger Riggs
Roger.Riggs at oracle.com
Tue Sep 10 14:33:14 UTC 2019
Hi Brian,
Reconsidering...
If the current line number starts at zero and is incremented only by CR/LF
then my example program is wrong. It called getLineNumber *after* reading
the line and it should have called it *before*.
So I can support your original patch (modulo not using AtomicBoolean).
(Though my suggestion about doing all the work of keeping track of the
line number in BufferedReader
is still a valid though separate suggestion).
Roger
On 9/9/19 5:02 PM, Brian Burkhalter wrote:
> If we wanted to go the route of changing the spec to match the behavior, the additional verbiage below is one possibility.
>
> Thanks,
>
> Brian
>
> +++ b/src/java.base/share/classes/java/io/LineNumberReader.java
> @@ -35,19 +35,20 @@
> * <p> By default, line numbering begins at 0. This number increments at every
> * <a href="#lt">line terminator</a> as the data is read, and can be changed
> * with a call to {@code setLineNumber(int)}. Note however, that
> * {@code setLineNumber(int)} does not actually change the current position in
> * the stream; it only changes the value that will be returned by
> * {@code getLineNumber()}.
> *
> * <p> A line is considered to be <a id="lt">terminated</a> by any one of a
> * line feed ('\n'), a carriage return ('\r'), or a carriage return followed
> - * immediately by a linefeed.
> + * immediately by a linefeed. For {@link #readLine()} only, the end of the
> + * stream is also considered to be a line terminator.
>
>
> public class LineNumberReader extends BufferedReader {
>
> /** The current line number */
> private int lineNumber = 0;
> @@ -183,19 +184,21 @@
> }
> }
>
> return n;
> }
> }
>
> /**
> * Read a line of text. Whenever a <a href="#lt">line terminator</a> is
> - * read the current line number is incremented.
> + * read the current line number is incremented. Unlike for the other
> + * {@code read} methods of this class, reaching the end of the stream
> + * will also increment the current line number.
> *
> * @return A String containing the contents of the line, not including
> * any <a href="#lt">line termination characters</a>, or
> * {@code null} if the end of the stream has been reached
> *
> * @throws IOException
> * If an I/O error occurs
> */
> public String readLine() throws IOException {
>
>> On Sep 9, 2019, at 12:02 PM, Brian Burkhalter <brian.burkhalter at oracle.com> wrote:
>>
>>> On Sep 9, 2019, at 7:35 AM, Roger Riggs <Roger.Riggs at oracle.com <mailto:Roger.Riggs at oracle.com>> wrote:
>>>
>>> I would lean toward updating the spec to reflect the current implementation.
>> It seems strange however that if one read an entire stream using read() in one case and readLine() in another that the results would differ.
More information about the core-libs-dev
mailing list