pre-RFR (s): 8049847: Enhance PrintWriter line.separator handling

Claes Redestad claes.redestad at
Fri Jan 2 15:38:44 UTC 2015


  this is a proposal to resolve concerns that PrintWriter/BufferedWriter 
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 
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) {
      public String lineSeparator() { return "\n"; }

  PrintWriter windowsPrintWriter = new PrintWriter(out) {
      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.


  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.


More information about the core-libs-dev mailing list