-
Bug
-
Resolution: Fixed
-
P4
-
8, 9
-
b165
-
Not verified
Issue | Fix Version | Assignee | Priority | Status | Resolution | Resolved In Build |
---|---|---|---|---|---|---|
JDK-8178761 | 10 | Brian Burkhalter | P4 | Resolved | Fixed | b05 |
FULL PRODUCT VERSION :
java version "1.8.0_121"
Java(TM) SE Runtime Environment (build 1.8.0_121-b13)
Java HotSpot(TM) 64-Bit Server VM (build 25.121-b13, mixed mode)
ADDITIONAL OS VERSION INFORMATION :
Microsoft Windows [Version 10.0.14393]
Linux ubuntu-xenial 4.4.0-45-generic #66-Ubuntu SMP Wed Oct 19 14:12:37 UTC 2016 x86_64 x86_64 x86_64 GNU/Linux
A DESCRIPTION OF THE PROBLEM :
BufferedReader readLine() javadoc, https://docs.oracle.com/javase/8/docs/api/java/io/BufferedReader.html#readLine--, says:
"A line is considered to be terminated by any one of a line feed ('\n'), a carriage return ('\r'), or a carriage return followed immediately by a linefeed."
However, this is strictly not the case, as additionally EOF is also one condition when line is considered terminated by the implementation.
Quoting wikipedia: "Two ways to view newlines, both of which are self-consistent, are that newlines either separate lines or that they terminate lines. If a newline is considered a separator, there will be no newline after the last line of a file. Some programs have problems processing the last line of a file if it is not terminated by a newline. On the other hand, programs that expect newline to be used as a separator will interpret a final newline as starting a new (empty) line. Conversely, if a newline is considered a terminator, all text lines including the last are expected to be terminated by a newline. If the final character sequence in a text file is not a newline, the final line of the file may be considered to be an improper or incomplete text line, or the file may be considered to be improperly truncated."
Java implementation considers newlines as line separators. However, javadoc seems to be contradicting this.
STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
1) Create a test file with last character not being newline.
vi temp.txt
# insert lines
head -c -1 temp.txt > demo.txt
od -c demo.txt
# confirm there is no newline at the end
2) Run the given code
EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
the first line
ACTUAL -
the first line
the second line
REPRODUCIBILITY :
This bug can be reproduced always.
---------- BEGIN SOURCE ----------
/*
Precondition: demo.txt with the contents
the first line
the second line
with 'e' being last character, omitting newline.
*/
import java.io.*;
public class OutputFile {
public static void main(String args[]) throws IOException{
BufferedReader br = new BufferedReader(new FileReader("demo.txt"));
String line;
while((line=br.readLine())!=null){
System.out.println(line);
}
br.close();
}
}
---------- END SOURCE ----------
CUSTOMER SUBMITTED WORKAROUND :
To match the implementation, javadoc should be amended to say something like:
A line is considered to be terminated at the end of the file or by any one of a line feed ('\n'), a carriage return ('\r'), or a carriage return followed immediately by a linefeed.
java version "1.8.0_121"
Java(TM) SE Runtime Environment (build 1.8.0_121-b13)
Java HotSpot(TM) 64-Bit Server VM (build 25.121-b13, mixed mode)
ADDITIONAL OS VERSION INFORMATION :
Microsoft Windows [Version 10.0.14393]
Linux ubuntu-xenial 4.4.0-45-generic #66-Ubuntu SMP Wed Oct 19 14:12:37 UTC 2016 x86_64 x86_64 x86_64 GNU/Linux
A DESCRIPTION OF THE PROBLEM :
BufferedReader readLine() javadoc, https://docs.oracle.com/javase/8/docs/api/java/io/BufferedReader.html#readLine--, says:
"A line is considered to be terminated by any one of a line feed ('\n'), a carriage return ('\r'), or a carriage return followed immediately by a linefeed."
However, this is strictly not the case, as additionally EOF is also one condition when line is considered terminated by the implementation.
Quoting wikipedia: "Two ways to view newlines, both of which are self-consistent, are that newlines either separate lines or that they terminate lines. If a newline is considered a separator, there will be no newline after the last line of a file. Some programs have problems processing the last line of a file if it is not terminated by a newline. On the other hand, programs that expect newline to be used as a separator will interpret a final newline as starting a new (empty) line. Conversely, if a newline is considered a terminator, all text lines including the last are expected to be terminated by a newline. If the final character sequence in a text file is not a newline, the final line of the file may be considered to be an improper or incomplete text line, or the file may be considered to be improperly truncated."
Java implementation considers newlines as line separators. However, javadoc seems to be contradicting this.
STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
1) Create a test file with last character not being newline.
vi temp.txt
# insert lines
head -c -1 temp.txt > demo.txt
od -c demo.txt
# confirm there is no newline at the end
2) Run the given code
EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
the first line
ACTUAL -
the first line
the second line
REPRODUCIBILITY :
This bug can be reproduced always.
---------- BEGIN SOURCE ----------
/*
Precondition: demo.txt with the contents
the first line
the second line
with 'e' being last character, omitting newline.
*/
import java.io.*;
public class OutputFile {
public static void main(String args[]) throws IOException{
BufferedReader br = new BufferedReader(new FileReader("demo.txt"));
String line;
while((line=br.readLine())!=null){
System.out.println(line);
}
br.close();
}
}
---------- END SOURCE ----------
CUSTOMER SUBMITTED WORKAROUND :
To match the implementation, javadoc should be amended to say something like:
A line is considered to be terminated at the end of the file or by any one of a line feed ('\n'), a carriage return ('\r'), or a carriage return followed immediately by a linefeed.
- backported by
-
JDK-8178761 BufferedReader readLine() javadoc does not match the implementation regarding EOF
- Resolved
- links to