How can I programatically determine the maximum size in bytes of a character in a specific charset?

I am getting all supported charsets by using :

Object[] Charsets = Charset.availableCharsets().keySet().toArray();

I now need to iterate through each character that can be encoded in that charset. To do this I thought about using the maximum number of bytes for each encoding and going through Byte.MIN_VALUE to Byte.MAX_VALUE for each byte. That byte array I then pass it through the String constructor that accepts a byte[] array and a specific encoding.

However can't find any clues on how I can determine the maximum length in bytes of a character representation in a specific charset.

I tried using the space character (i.e. " ") to create a string in that encoding and using .getBytes("<specific charset>").length. However I believe this only works for fixed size charsets. There are charsets in which the encoding of a character can have a variable number of bytes.

This is not a commercial software so it does not need a pretty solution. I need to create a sort of visual map of each supported charset. Each character representation is written into an image. I am also not sure on how I should select a font that can properly display all characters of a charset.

Any thoughts?

Upvotes: 1

Views: 247

Answers (1)

apangin
apangin

Reputation: 98570

charset.newEncoder().maxBytesPerChar()

Upvotes: 2

Related Questions