javacharacter-encoding

Why does CharsetEncoder.maxBytesPerChar() return a float?


This does not make sense to me why method CharsetEncoder.maxBytesPerChar() returns a float and not an integer, since this is a maximum.

Do encoding mechanisms that use partial bytes exist? Can I assume the returned float can always be casted to an integer with no data loss?


Solution

  • The method returns a float because the max. number of bytes/character could actually be fractional depending on the method of encoding.

    If future encodings had fractional bytes/character instead of say, UTF-8's 1-4 bytes/character, you may have been thankful.

    So to answer your question, yes I would assume. As long as you know the encoding scheme and whether or not fractional bytes are being toyed with, you can make the decision yourself.