JDK-8254653 : PrintStream and PrintWriter platform encoding mention is incorrect
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.io
  • Affected Version: 15
  • Priority: P4
  • Status: Open
  • Resolution: Unresolved
  • OS: generic
  • CPU: generic
  • Submitted: 2020-10-11
  • Updated: 2023-08-10
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
Other
tbdUnresolved
Related Reports
Relates :  
Relates :  
Relates :  
Relates :  
Description
A DESCRIPTION OF THE PROBLEM :
Follow-up for https://bugs.openjdk.java.net/browse/JDK-8131664
Related to:
- https://bugs.openjdk.java.net/browse/JDK-4833833
- https://bugs.openjdk.java.net/browse/JDK-4916565
- https://bugs.openjdk.java.net/browse/JDK-5068812

PrintStream and PrintWriter both support specifying a custom character encoding (Charset), or in the case of PrintWriter even support specifying a Writer and therefore the PrintWriter does not perform any encoding at all.

Yet some methods only mention that characters will be encoded using the "platform encoding", which is incorrect:
- PrintStream:
  - write​(int)
  - write​(byte[], int, int)
    Note: This method should probably also refer to `print(char[])` (taking an array parameter) instead of the overloads with a single `char` parameter, similar to `write(byte[])` which also refers to `print(char[])`
  - write(byte[])
  - writeBytes​(byte[])
  - print(boolean)
  - print(int)
  - print(long)
  - print(float)
  - print(double)
  - print(Object)
- PrintWriter
  - print(boolean)
  - print(char)
  - print(int)
  - print(long)
  - print(float)
  - print(double)
  - print(char[])
  - print(String)
    Note: The javadoc also refers to `write(int)` for writing bytes; this appears to be copied from PrintStream but is incorrect here because PrintWriter.write(int) writes **chars** not bytes.
  - print(Object)
    Note: The javadoc also refers to `write(int)` for writing bytes; this appears to be copied from PrintStream but is incorrect here because PrintWriter.write(int) writes **chars** not bytes.

It might be best to just completely omit these notes about encoding in the methods and mention it **once** in the class documentation. Currently it just causes a lot of redundance.

Also some side notes which should ideally be fixed as well (feel free to create separate Jira issues):
1. The PrintStream class javadoc should use a link instead of only {@code ...} for the reference to `checkError`.
2. It also might be good to update the javadoc of `PrintStream​(OutputStream, boolean, String)`: All other constructors (even PrintWriter ones) which take a String charset use the parameter name `csn` and link to Charset, yet that constructor calls it `encoding` and links to the package doc.
3. `PrintWriter.write(char[], int, int)` unnecessarily has "A" and "Portion" capitalized in the javadoc:
> Writes A Portion of ...
4. `PrintWriter.write​(char[])` and `write(String)` javadoc contains implementation details which are irrelevant for the user:
> This method cannot be inherited from the Writer class because it must suppress I/O exceptions.
5. PrintStream.append(CharSequence) and PrintWriter.append(CharSequence) should link to java.nio.CharBuffer instead of only using the term "character buffer" because not every user might understand which class is meant by this.
6. `PrintStream.write​(byte[])` `@throws` javadoc is incorrect, it should say
> @throws IOException Never
7. PrintWriter.write(int) should explain as detailed as Write.write(int) that the high order 16-bits are ignored. This is currently not clear.



Comments
Some modifications in the javadocs are needed as per the followup of https://bugs.openjdk.java.net/browse/JDK-8131664 1. javadocs of few methods of PrintStream and PrintWriter still use platform encoding whereas custom charset can be used. 2. write(byte[], int, int) should reference to print(char[]) in its description rather than print(char) and println(char) 3. print(String) method refers to write(int) for writing bytes which is incorrect because PrintWriter.write(int) writes chars not bytes. 4. The PrintStream class javadoc should use a link instead of only {@code ...} for the reference to `checkError`. 5. It also might be good to update the javadoc of `PrintStream​(OutputStream, boolean, String)`: All other constructors (even PrintWriter ones) which take a String charset use the parameter name `csn` and link to Charset, yet that constructor calls it `encoding` and links to the package doc. 6. `PrintWriter.write(char[], int, int)` unnecessarily has "A" and "Portion" capitalized in the javadoc: > Writes A Portion of ... 7. `PrintWriter.write(char[])` and `write(String)` javadoc contains implementation details which are irrelevant for the user: > This method cannot be inherited from the Writer class because it must suppress I/O exceptions. 8. PrintStream.append(CharSequence) and PrintWriter.append(CharSequence) should link to java.nio.CharBuffer instead of only using the term "character buffer" because not every user might understand which class is meant by this. 9. `PrintStream.write(byte[])` `@throws` javadoc is incorrect, it should say > @throws IOException Never 7. PrintWriter.write(int) should explain as detailed as Write.write(int) that the high order 16-bits are ignored. This is currently not clear. Following link can be referred for the issues: For example: https://docs.oracle.com/en/java/javase/15/docs/api/java.base/java/io/PrintStream.html#write(byte%5B%5D)
13-10-2020