RFR: 8333843: Provide methods on MemorySegment to read strings with known lengths

Maurizio Cimadamore mcimadamore at openjdk.org
Wed Aug 28 10:00:19 UTC 2024


On Wed, 28 Aug 2024 09:56:43 GMT, Maurizio Cimadamore <mcimadamore at openjdk.org> wrote:

>> src/java.base/share/classes/java/lang/foreign/MemorySegment.java line 1316:
>> 
>>> 1314: 
>>> 1315:     /**
>>> 1316:      * Reads a string using the given byte length from this segment at the given offset,
>> 
>> What happens is there's a null terminator before `length` ? Seems like we're just copying it? IMHO, this is the tension with this API.
>
> Overall, I guess I'm not overly convinced that it's worth adding a method like this for:
> 
> byte[] bytes = new byte[len];
> MemorySegment.copy(segment, JAVA_BYTE, offset, bytes, 0, len);
> return new String(bytes, charset);
> 
> e.g. the API provides string shortcuts for the common case. The more convoluted case can still be achieve via `copy`. Is there anything I'm missing here?

Ok, I suppose what you do get is the auto-detection of charset length, which adjusts the size of the copy...

-------------

PR Review Comment: https://git.openjdk.org/jdk/pull/20725#discussion_r1734369629


More information about the core-libs-dev mailing list