Inconsistencies when creating a Reader from a Path

Norbert Kiesel nkiesel at
Thu Feb 27 19:33:16 UTC 2020

 The following 2 ways to construct a `Reader` for a `Path` look very
similar (with a slight edge for the first one because it is shorter):

Reader reader1 = Files.newBufferedReader(path, StandardcharSets.UTF_8);
Reader reader2 = new BufferedReader(new
InputStreamReader(Files.newInputStream(path), StandardCharsets.UTF_8));

Both readers ultimately create a `StreamDecoder` that is used to read and
decode the input.  However, they use different constructors:

 - `reader1` calls the constructor with a `CharsetDecoder` created from the
`Charset` using `newDecoder()`
 - `reader2` calls the constructor with the `Charset` which `StreamDecoder`
then converts
   into a `CharsetDecoder` using

The result is that `reader1` will throw an Exception when facing invalid
input while `reader2` will silently "fix" that invalid input using
"replacement chars".

Which brings me to my questions:

 1. Should the 2 approaches not behave identical by default?
 2. Is there a way to use the first approach but end up with the same error
behavior as the second approach? One possible way would be to create an
    overloaded `Files.newBufferedReader` which takes a `CharsetDecoder` as
second parameter.

More information about the jdk-dev mailing list