Java Panama / Foreign Function & Memory API: Reading byte arrays of unknown length

Consider a header file defining this method:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>ErrorCode_t GetBlob(Handle_const_t hHandle, uint8_t* const pValue, size_t* const pnSize);
</code>
<code>ErrorCode_t GetBlob(Handle_const_t hHandle, uint8_t* const pValue, size_t* const pnSize); </code>
ErrorCode_t GetBlob(Handle_const_t hHandle, uint8_t* const pValue, size_t* const pnSize);

This method writes n bytes into the array starting at the pointer of pValue. The size n will be set at the pointer of pnSize.

The header can be converted into Java source code using jextract, which gives:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>public static long GetBlob(MemorySegment hHandle, MemorySegment pValue, MemorySegment pnSize) {
// ..
}
</code>
<code>public static long GetBlob(MemorySegment hHandle, MemorySegment pValue, MemorySegment pnSize) { // .. } </code>
public static long GetBlob(MemorySegment hHandle, MemorySegment pValue, MemorySegment pnSize) {
    // ..
}

After reading /questions/76978598 and /questions/71250218 I allocated a C_POINTER with unbounded address layout and called the method:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code> MemorySegment handle = ..;
MemorySegment arrayPointer = Arena.global().allocate(C_POINTER);
MemorySegment sizePointer = Arena.global().allocate(JAVA_LONG);
Accessor_h.GetBlob(handle, arrayPointer, sizePointer);
</code>
<code> MemorySegment handle = ..; MemorySegment arrayPointer = Arena.global().allocate(C_POINTER); MemorySegment sizePointer = Arena.global().allocate(JAVA_LONG); Accessor_h.GetBlob(handle, arrayPointer, sizePointer); </code>
    MemorySegment handle = ..;
    MemorySegment arrayPointer = Arena.global().allocate(C_POINTER);
    MemorySegment sizePointer = Arena.global().allocate(JAVA_LONG);
    Accessor_h.GetBlob(handle, arrayPointer, sizePointer);

The call is successful. The sizePointer contains the size of the array.

But reading the array data fails. I tried the following:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code> arrayPointer.get(C_POINTER, 0L)
.toArray(JAVA_BYTE); // fails with "Segment is too large to wrap as byte[]. Size: 9223372036854775807"
</code>
<code> arrayPointer.get(C_POINTER, 0L) .toArray(JAVA_BYTE); // fails with "Segment is too large to wrap as byte[]. Size: 9223372036854775807" </code>
    arrayPointer.get(C_POINTER, 0L)
        .toArray(JAVA_BYTE); // fails with "Segment is too large to wrap as byte[]. Size: 9223372036854775807"

Or:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code> arrayPointer.get(C_POINTER, 0L)
.asSlice(0L, sizePointer.get(JAVA_LONG, 0L))
.toArray(JAVA_BYTE); // SIGSEGV (0xb)
</code>
<code> arrayPointer.get(C_POINTER, 0L) .asSlice(0L, sizePointer.get(JAVA_LONG, 0L)) .toArray(JAVA_BYTE); // SIGSEGV (0xb) </code>
    arrayPointer.get(C_POINTER, 0L)
            .asSlice(0L, sizePointer.get(JAVA_LONG, 0L))
            .toArray(JAVA_BYTE); // SIGSEGV (0xb)

Or:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code> arrayPointer
.toArray(JAVA_BYTE); // seems to contain the correct data, but only the first eight bytes
</code>
<code> arrayPointer .toArray(JAVA_BYTE); // seems to contain the correct data, but only the first eight bytes </code>
    arrayPointer
            .toArray(JAVA_BYTE); // seems to contain the correct data, but only the first eight bytes

Or:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code> arrayPointer
.asSlice(0L, sizePointer.get(JAVA_LONG, 0L))
.toArray(JAVA_BYTE); // fails with "java.lang.IndexOutOfBoundsException: Out of bound access on segment MemorySegment"
</code>
<code> arrayPointer .asSlice(0L, sizePointer.get(JAVA_LONG, 0L)) .toArray(JAVA_BYTE); // fails with "java.lang.IndexOutOfBoundsException: Out of bound access on segment MemorySegment" </code>
    arrayPointer
            .asSlice(0L, sizePointer.get(JAVA_LONG, 0L))
            .toArray(JAVA_BYTE); // fails with "java.lang.IndexOutOfBoundsException: Out of bound access on segment MemorySegment"

What can I do, to safely read the byte array in Java?

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật