-1

I am working on a project that deals with security (blockchain) and it makes use of the String.getBytes() function. This function has undefined behavior based on which "platform default charset" is used.

Because of this and other reasons, I would like to test the program's continuous integration using every possible platform default charset.

The project supports Java version 11+.

How do I find the list of every possible platform default charset?

And how do I run Java in that charset (using any means possible) in order to run my tests?

William Entriken
  • 37,208
  • 23
  • 149
  • 195
  • [A quick google for the latter question about setting the default charset](https://stackoverflow.com/questions/361975/setting-the-default-java-character-encoding) – Rogue Feb 24 '21 at 04:31
  • 4
    If you don't like the fact that it uses the default charset, just pass in a specific charset! It will use that one! – Sweeper Feb 24 '21 at 04:33
  • 1
    Does [this](https://stackoverflow.com/questions/9312816/platforms-default-charset-on-different-platforms) and [this](https://stackoverflow.com/questions/361975/setting-the-default-java-character-encoding/) help? – Thiyanesh Feb 24 '21 at 05:08
  • Wrong question. Don't ask how to test all combinations of "*undefined* behavior", instead ask how to make the code have "*well-defined* behavior". --- *Hint:* Sweeper already gave [solution](https://stackoverflow.com/questions/66344716/how-do-i-find-and-use-every-java-supported-platform-default-charset#comment117291780_66344716) for that. – Andreas Feb 24 '21 at 06:19
  • Yes! Fixing the UB is desirable. And I do see the `getBytes` alternative with specified `Charset`. However, at present this is a matter of leadership, across the project I'm in as well as all its dependencies. If I can prove that a tangible problem exists (using a failed test case) then I get resources to fix the problem. – William Entriken Feb 24 '21 at 15:45

1 Answers1

0

Here is how to run Java with a different "platform default charset".

java -D"file.encoding=UTF-16" -jar ...

Here is the list of all possible platform default charsets:

https://docs.oracle.com/javase/8/docs/technotes/guides/intl/encoding.doc.html

P.S. The "platform" default charset sounds like an immutable value compiled into Java on your computer. It's not. This is something you can change at VM execution time.

William Entriken
  • 37,208
  • 23
  • 149
  • 195