Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8262187

CharsetEncoder.maxBytesPerChar() and CharsetDecoder.maxCharsPerByte() return float instead of int

XMLWordPrintable

      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 also JDK-8230531 and JDK-8231434



            Unassigned Unassigned
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            5 Start watching this issue

              Created:
              Updated: