<!DOCTYPE html><html><head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  </head>
  <body>
    <p>I see. So, in the protobuf case you would need to:</p>
    <p>1. encode the Java string in the desired charset<br>
      2. find out how many bytes you have<br>
      3. write the length computed in (2) and the string bytes in (1)
      into the resulting segment</p>
    <p>It seems to me that in this case encoding and length travels
      together? E.g. you need to encode anyway, at which point you also
      know the byte size?</p>
    <p>(I'm a bit unsure that here there's anything to be gained by the
      method you proposed?)</p>
    <p>Do you have use cases where you don't want to decode, you just
      want to know the byte length?</p>
    <p>Maurizio</p>
    <div class="moz-cite-prefix">On 11/11/2025 14:35, Liam Miller-Cushon
      wrote:<br>
    </div>
    <blockquote type="cite" cite="mid:CAL4Qsgu2GRug1Q_YhtNKfPUWWUd6gHCQNx+sN+_AaSdZsfD9Wg@mail.gmail.com">
      
      <div dir="ltr">
        <div dir="ltr">On Tue, Nov 11, 2025 at 3:10 PM Maurizio
          Cimadamore <<a href="mailto:maurizio.cimadamore@oracle.com" moz-do-not-send="true" class="moz-txt-link-freetext">maurizio.cimadamore@oracle.com</a>>
          wrote:</div>
        <div class="gmail_quote gmail_quote_container">
          <blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">Going
            back to this... do you have a use case where you would like
            to <br>
            know the byte length of a string encoded in a given charset?<br>
          </blockquote>
          <div><br>
          </div>
          The main use-case is to know if a preexisting buffer is big
          enough to write out an encoded string to.</div>
        <div class="gmail_quote gmail_quote_container"><br>
        </div>
        <div class="gmail_quote gmail_quote_container">As well, for the
          protobuf example, it's writing a varint length as a prefix
          before the string data, so it needs to know what the encoded
          length is to know how many bytes to reserve to encode the
          length itself.</div>
        <div class="gmail_quote gmail_quote_container"><br>
        </div>
        <div class="gmail_quote gmail_quote_container">(I'm happy to
          separate out the discussion of string.getBytesLength(Charset),
          or not, if you think it could safely be pursued separately
          without risk of dead ends. I mentioned it here because the
          use-case is at least somewhat related, and in case you saw
          connections I'd missed.)</div>
      </div>
    </blockquote>
  </body>
</html>