149. Copying and slicing memory segments
Let’s consider the following memory segment (arena is an instance of Arena):
MemorySegment srcSegment = arena.allocateArray(
ValueLayout.JAVA_INT, 1, 2, 3, 4, -1, -1, -1,
52, 22, 33, -1, -1, -1, -1, -1, 4);
Next, let’s see how we can copy the content of this segment.
Copying a segment
We can make a copy of this memory segment via copyFrom(MemorySegment src) as follows:
MemorySegment copySegment = srcSegment.copyFrom(srcSegment);
We can easily see if the data was copied as follows:
System.out.println(“Data: ” + Arrays.toString(
copySegment.toArray(ValueLayout.JAVA_INT)));
This is a bulk operation that creates a full copy of the given memory segment.
Copying a part of the segment into another segment (1)
Let’s suppose that we want to copy only a part from srcSegment into another segment (dstSegment). For instance, if we want to copy the last 8 elements ([22, 33, -1, -1, -1, -1, -1, 4]) from srcSegment to dstSegment, we start by allocating the dstSegment accordingly:
MemorySegment dstSegment
= arena.allocateArray(ValueLayout.JAVA_INT, 8);
Next, we call copy(MemorySegment srcSegment, long srcOffset, MemorySegment dstSegment, long dstOffset, long bytes) method as in the following figure:

Figure 7.20 – Copying a part of a segment into another segment (1)
So, we specify the source segment as srcSsegment, the source offset as 32 (skip the first 8 elements), the destination segment as dstSegment, the destination offset as 0, and the number of bytes to be copied as 32 (the last 8 elements are copied):
MemorySegment.copy(srcSegment, 32, dstSegment, 0, 32);
Practically, we copied half of the srcSegment to dstSegment.
Copying a segment into an on-heap array
Let’s suppose that we want to copy only a part from srcSegment into an on-heap Java regular array (dstArray). For instance, if we want to copy the last 8 elements ([22, 33, -1, -1, -1, -1, -1, 4]) from srcSegment to dstArray, we start by creating the dstArray accordingly:
int[] dstArray = new int[8];
Next, we call copy(MemorySegment srcSegment, ValueLayout srcLayout, long srcOffset, Object dstArray, int dstIndex, int elementCount) as in the following figure:

Figure 7.21 – Copying a segment into an on-heap array
So, we specify the source segment as srcSegment, the source layout as JAVA_INT, the source offset as 32 (skip the first 8 elements), the destination array as dstArray, the destination array index as 0, and the number of elements to be copied as 8:
MemorySegment.copy(
srcSegment, ValueLayout.JAVA_INT, 32, dstArray, 0, 8);
Practically, we copied half of the off-heap srcSegment to the on-heap dstArray.
Copying an on-heap array into a segment
Let’s suppose that we want to copy an on-heap array (or a part of it) into a segment. The given on-heap array is arraySrc:
int[] srcArray = new int[]{10, 44, 2, 6, 55, 65, 7, 89};
The destination segment can hold 16 integer values:
MemorySegment dstSegment
= arena.allocateArray(ValueLayout.JAVA_INT, 16);
Next, we want to overwrite the last 8 elements from dstSegment with the elements from srcArray, while the first elements remain 0. For this, we call copy(Object srcArray, int srcIndex, MemorySegment dstSegment, ValueLayout dstLayout, long dstOffset, int elementCount) as in the following figure:

Figure 7.22 – Copying an on-heap array into a segment
So, we specify the source array as srcArray, the source index as 0, the destination segment as dstSegment, the destination layout as JAVA_INT, the destination offset as 32 (skip the first 8 elements), and the number of elements to be copied as 8:
MemorySegment.copy(
srcArray, 0, dstSegment, ValueLayout.JAVA_INT, 32, 8);
Practically, we copied the on-heap srcArray as the second half of the off-heap destSegment.