Hi,

this is a proposal to resolve concerns that PrintWriter/BufferedWriter behave inconsistently with j.u.Formatter when setting the line.separator property to override
system-appropriate line breaks.

Instead of providing a set of new constructors, I propose to simply add a new default method j.l.Appendable#lineSeparator, which by default delegates to System.lineSeparator(). This allows classes such as PrintWriter/BufferedWriter to provide overrides which
communicate to j.u.Formatter their intended behavior.

This indirectly provides a way to control the lineSeparator by allowing users to override PrintWriter or similar in custom classes which override lineSeparator, e.g.:

 PrintWriter unixPrintWriter = new PrintWriter(out) {
     @Override
     public String lineSeparator() { return "\n"; }
 };

 PrintWriter windowsPrintWriter = new PrintWriter(out) {
     @Override
     public String lineSeparator() { return "\r\n"; }
 };

Bonus: This approach can be used to optimize j.l.Throwable to get rid of all static inner classes to wrap PrintWriter/PrintStream and instead use j.l.Appendable, which more than enough will mitigate the startup hit adding a method to Appendable will incur.

bug: https://bugs.openjdk.java.net/browse/JDK-8049847
webrev: http://cr.openjdk.java.net/~redestad/8049847/webrev.00/

I've not filed requests to change the public API just yet, rather wanted to throw this out
for preview to see if there's some concern and feedback first.

/Claes

Reply via email to