Search code examples
character-encodingbase64asciijava-8iso-8859-1

Why JDK8's Base64 uses ISO-8859-1?


I'm writing my own BASE64 encoder/decoder for some constrained environments.

And I found that Base64.Encoder#encodeString saying that it uses ISO-8859-1 for construct a String from those encoded bytes.

I perfectly presuming that ISO-8859-1 charset also covers all base64 alphabets.

Is there any possible reason not to use US-ASCII?


Solution

  • I suspect it's more efficient: converting from ISO-8859-1 back to text is just a matter of promoting each byte straight to a char, whereas for ASCII you'd need to check that the byte is valid ASCII. The result for base64 will always be the same, of course.

    (That's only a guess, but an educated one. You could always run benchmarks if you want to validate it...)