RFR: 8264200: java/nio/channels/DatagramChannel/SRTest.java fails intermittently

Roger Riggs rriggs at openjdk.java.net
Tue Apr 6 14:31:22 UTC 2021


On Tue, 6 Apr 2021 14:24:16 GMT, Daniel Fuchs <dfuchs at openjdk.org> wrote:

>> Fair enough. Is there any reason then why UTF-8 would be used over ISO_8859_1 then, given what you said above about character mapping? Or vice-versa
>
> The message sent by the writer is "hello" - it only contains ASCII characters, so any of US-ASCII, UTF-8, or ISO-8859-1, which are all compatible with ASCII (meaning: all these encode ASCII chars in the same way) would work.
> In fact any charset in which "hello" is mappable would work. We simply need to make sure to use the *same charset* when encoding or decoding.

Its case by case, if you don't know how the stream of characters was encoded into a byte stream then you can't reliably decode it back to characters.  For SRTest, it doesn't matter, all the strings I see are ascii.
But note that String.getBytes() uses a platform dependent encoding. 
Until the platform encoding is UTF-8, its recommended to use String.getBytes(charset).
A lot of times UTF-8 is a good guess for operations that convert strings to bytes and back.

-------------

PR: https://git.openjdk.java.net/jdk/pull/3354


More information about the nio-dev mailing list