API to compute the byte length of a String encoded in a given Charset

XMLWordPrintable

    • Type: Enhancement
    • Resolution: Unresolved
    • Priority: P4
    • None
    • Affects Version/s: None
    • Component/s: core-libs

      This is a feature request for an API that returns the byte length of a String encoded in a given charset.

      This would be equivalent in behavior to `getBytes(cs).length`, but could offer better performance.

      Compared to an implementation outside the JDK, the JDK has opportunities to optimize this method using internal routines like `String#bytesCompatible` and `StringCoding#countPositives`.

      One motivating use-case is encoding multiple large strings into a single array. Without an efficient way to get the encoded length, it's necessary to encode into a temporary array and pay the cost of resizing it (potentially multiple times). There was related discussion in this panama-dev@ thread [1], and this API would dovetail with improvements discussed in [2][3]

      [1] https://mail.openjdk.org/pipermail/panama-dev/2025-November/021202.html
      [2] https://cr.openjdk.org/~mcimadamore/panama/strings_ffm.html
      [3] https://bugs.openjdk.org/browse/JDK-8369564

            Assignee:
            Liam Miller-Cushon
            Reporter:
            Liam Miller-Cushon
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated: