How can I programmatically determine the maximum size in bytes of a character in a particular encoding?

I get all supported encodings using:

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

Now I need to iterate over each character that can be encoded in this encoding. For 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. This byte array I pass it through the String constructor, which takes a byte[] array and a specific encoding.

However, no clues can be found on how I can determine the maximum length in bytes of a character representation in a particular encoding.

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

This is not commercial software, so it does not need a good solution. I need to create a visual map of each supported encoding. Each representation of the character is written to the image. I'm also not sure how to choose a font that can display all encoding characters.

Any thoughts?

+1
source share
1 answer
 charset.newEncoder().maxBytesPerChar() 
+2
source

All Articles