Paul Taylor
Paul Taylor

Reputation: 13110

Can any character be encoded in UTF-16 (using Java 8)

Can any character be encoded in UTF-16 (using java) ?

I thought it could but my code that encodes as

CharsetEncoder encoder = Charset.forName("UTF-16LE").newEncoder();
ByteBuffer bb = encoder.encode(CharBuffer.wrap((String) value + '\0'));

has thrown a CharacterCodingException

Unfortunately as this only occurred for a customer not myself I dont have details of the offending character.

Upvotes: 1

Views: 231

Answers (1)

McDowell
McDowell

Reputation: 108899

There are possible values of char that are not valid UTF-16 sequences. For example:

    CharsetEncoder encoder = Charset.forName("UTF-16LE").newEncoder();
    ByteBuffer bb = encoder.encode(CharBuffer.wrap("\uDFFF"));

This code will throw an exception. U+DFFF is an unpaired surrogate.

Upvotes: 1

Related Questions