Ted
Ted

Reputation: 1690

What's the difference between java.lang.String.getBytes() and java.nio.charset.CharsetEncoder.encode()?

I was reading through the source code of java.lang.String, specifically getBytes():

 public byte[] getBytes(Charset charset) {
         if (charset == null) throw new NullPointerException();
            return StringCoding.encode(charset, value, offset, count);
 }

However, I couldn't find the StringCoding.encode() method in the Java API. I'd like to be able to compare it with java.nio.charset.CharsetEncoder.encode(), since that class/method is referenced as an alternative in the String.getBytes() javadoc. How do I find the StringCoding class, and it's source code? And what is the difference between the respective .encode() methods?

Upvotes: 1

Views: 1443

Answers (1)

fge
fge

Reputation: 121710

The difference is that with a CharsetEncoder you can choose how to fail; this is the CodingErrorAction class.

By default, String's .getBytes() uses REPLACE. Most uses of CharsetEncoder however will REPORT insead.

You can see an example of CodingErrorAction usage at the end of this page.

One such example of REPORT usage is in java.nio.file. At least on Unix systems, a pathname which you created from a String will be encoded before it is written to disks; if the encoding fails (for instance, you use ö and system charset US-ASCII), the JDK will refuse to create the file and you will be greeted with an (unchecked!) InvalidPathException.

This is unlike File which will create who knows what as a file name, and one more reason to ditch it...

Upvotes: 2

Related Questions