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

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


On Tue, 27 Aug 2024 09:36:56 GMT, Per Minborg <pminborg at openjdk.org> wrote:

> This PR proposes to add a new overload to `MemorySegment::getString` whereby it is possible to pass in a known byte length of the content in a segment that should be converted to a String. This is useful in case one already knows the byte length and thereby does not need to scan for a null terminator.

src/java.base/share/classes/java/lang/foreign/MemorySegment.java line 1326:

> 1324:      * @param offset  offset in bytes (relative to this segment address) at which this
> 1325:      *                access operation will occur
> 1326:      * @param length  byte length to be used for string conversion (not including any

I suppose this might also create confusion - e.g. some users might expect the length to be "logical" (e.g. expressed in number of chars, which then can be turned into a physical length using the charset).

src/java.base/share/classes/jdk/internal/foreign/StringSupport.java line 58:

> 56:     public static String read(MemorySegment segment, long offset, int len, Charset charset) {
> 57:         return switch (CharsetKind.of(charset)) {
> 58:             case SINGLE_BYTE -> readByte(segment, offset, len, charset);

How does this work, exactly? All methods called here seem to do:

byte[] bytes = new byte[len];
MemorySegment.copy(segment, JAVA_BYTE, offset, bytes, 0, len);


Why do we need different methods?

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

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


More information about the core-libs-dev mailing list