[External] : Re: MemorySegment APIs for reading and writing strings with known lengths

Maurizio Cimadamore maurizio.cimadamore at oracle.com
Tue Nov 11 14:59:47 UTC 2025


I see. So, in the protobuf case you would need to:

1. encode the Java string in the desired charset
2. find out how many bytes you have
3. write the length computed in (2) and the string bytes in (1) into the 
resulting segment

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?

(I'm a bit unsure that here there's anything to be gained by the method 
you proposed?)

Do you have use cases where you don't want to decode, you just want to 
know the byte length?

Maurizio

On 11/11/2025 14:35, Liam Miller-Cushon wrote:
> On Tue, Nov 11, 2025 at 3:10 PM Maurizio Cimadamore 
> <maurizio.cimadamore at oracle.com> wrote:
>
>     Going back to this... do you have a use case where you would like to
>     know the byte length of a string encoded in a given charset?
>
>
> The main use-case is to know if a preexisting buffer is big enough to 
> write out an encoded string to.
>
> 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.
>
> (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.)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/panama-dev/attachments/20251111/a399281f/attachment.htm>


More information about the panama-dev mailing list