RFR: 8369564: Provide a MemorySegment API to read strings with known lengths [v8]
Liam Miller-Cushon
cushon at openjdk.org
Tue Nov 25 19:10:29 UTC 2025
On Tue, 25 Nov 2025 14:13:09 GMT, Maurizio Cimadamore <mcimadamore at openjdk.org> wrote:
>> Done, and I added a test to cover the IAE
>
> I think I'm confused here.
>
> This method is called by `MemorySegment::getString` with an explicit length.
>
> The question is -- what does the length represent? There's two options:
>
> 1. the length, in bytes, of the region of memory that needs to be _decoded_ into the final string bytes
> 2. the length of the final string bytes
>
> IMHO, only (1) really makes sense -- as (2) involves some charset-specific guesswork.
>
> But if (1) is (as I hope) what we mean, I think we should probably make that clearer in the javadoc. Then, the exception we throw here is related to the fact that we don't have a way to construct a string from something bigger than a byte[] -- which is, in a way, an impl specific issue (one can imagine manually decoding the segment bytes into a new char[], which is then used to create the string -- in which case it might be possible e.g. to read 4 * N UTF32 string bytes into a LATIN1 string backed by a byte array with size N).
Thanks, I agree seems like the interpretation we want, and that it is worth clarifying that in the javadoc. I have updated it to "length, in bytes, of the region of memory to read and decode into a string", suggestions for improvements to that are welcome.
-------------
PR Review Comment: https://git.openjdk.org/jdk/pull/28043#discussion_r2561080479
More information about the core-libs-dev
mailing list