Skip to content

Commit 3e0da58

Browse files
committed
8333843: Provide guidelines on MemorySegment to read strings with known lengths
Reviewed-by: mcimadamore
1 parent 3c4d15b commit 3e0da58

File tree

1 file changed

+8
-0
lines changed

1 file changed

+8
-0
lines changed

src/java.base/share/classes/java/lang/foreign/MemorySegment.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1285,6 +1285,14 @@ MemorySegment reinterpret(long newSize,
12851285
* sequences with this charset's default replacement string. The {@link
12861286
* java.nio.charset.CharsetDecoder} class should be used when more control
12871287
* over the decoding process is required.
1288+
* <p>
1289+
* Getting a string from a segment with a known byte offset and
1290+
* known byte length can be done like so:
1291+
* {@snippet lang=java :
1292+
* byte[] bytes = new byte[length];
1293+
* MemorySegment.copy(segment, JAVA_BYTE, offset, bytes, 0, length);
1294+
* return new String(bytes, charset);
1295+
* }
12881296
*
12891297
* @param offset offset in bytes (relative to this segment address) at which this
12901298
* access operation will occur

0 commit comments

Comments
 (0)