-
Enhancement
-
Resolution: Unresolved
-
P4
-
None
-
8, 11, 15
-
generic
-
generic
A DESCRIPTION OF THE PROBLEM :
The methods {{CharsetEncoder.maxBytesPerChar()}} and {{CharsetDecoder.maxCharsPerByte()}} both return a result representing a maximum. For such a value one would expect it to be integer, yet (for some reason) the methods return a {{float}} instead of {{int}}.
Even worse is that for the intended use case "worst-case size of the output buffer" (as described by the documentation), {{float}} will cause precision loss for large input buffers.
The JDK itself suffers from this, having to introduce the private method {{String.scale(int, float)}}.
Ideally these methods would be replaced with ones returning an {{int}} value, though that would break backward compatibility.
Maybe for now it would be best to:
1. Deprecate the methods; or at least add a big warning that the caller should cast the result to {{int}} before performing any further calculations with it
2. Verify in the CharsetEncoder / CharsetDecoder constructor that the {{float}} value actually represents an {{int}} value ({{value == (int) value}})
3. Consider adding more useful and efficient alternative methods, see alsoJDK-8230531 and JDK-8231434
The methods {{CharsetEncoder.maxBytesPerChar()}} and {{CharsetDecoder.maxCharsPerByte()}} both return a result representing a maximum. For such a value one would expect it to be integer, yet (for some reason) the methods return a {{float}} instead of {{int}}.
Even worse is that for the intended use case "worst-case size of the output buffer" (as described by the documentation), {{float}} will cause precision loss for large input buffers.
The JDK itself suffers from this, having to introduce the private method {{String.scale(int, float)}}.
Ideally these methods would be replaced with ones returning an {{int}} value, though that would break backward compatibility.
Maybe for now it would be best to:
1. Deprecate the methods; or at least add a big warning that the caller should cast the result to {{int}} before performing any further calculations with it
2. Verify in the CharsetEncoder / CharsetDecoder constructor that the {{float}} value actually represents an {{int}} value ({{value == (int) value}})
3. Consider adding more useful and efficient alternative methods, see also
- relates to
-
JDK-4949631 String.getBytes() does not work on some strings larger than 16MB
- Resolved
-
JDK-8230531 API Doc for CharsetEncoder.maxBytesPerChar() should be clearer about BOMs
- Closed
-
JDK-8231434 Add minBytesPerSequence() to java.nio.charsets.CharsetEncoder
- Open