From 1e94d956dd7bc62c6ad49aab10e2f9feb5fbea9f Mon Sep 17 00:00:00 2001 From: Amr Hesham Date: Sun, 19 Jul 2026 14:28:01 +0200 Subject: [PATCH 1/2] jextract: Implement fromByteBuffer helper for the foundation Data --- .../java/com/example/swift/DataImportTest.java | 11 +++++++++++ .../FFMSwift2JavaGenerator+FoundationData.swift | 16 +++++++++++++++- 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/Samples/SwiftJavaExtractFFMSampleApp/src/test/java/com/example/swift/DataImportTest.java b/Samples/SwiftJavaExtractFFMSampleApp/src/test/java/com/example/swift/DataImportTest.java index 82fb09464..870404640 100644 --- a/Samples/SwiftJavaExtractFFMSampleApp/src/test/java/com/example/swift/DataImportTest.java +++ b/Samples/SwiftJavaExtractFFMSampleApp/src/test/java/com/example/swift/DataImportTest.java @@ -18,6 +18,7 @@ import org.swift.swiftkit.ffm.AllocatingSwiftArena; import java.lang.foreign.ValueLayout; +import java.nio.ByteBuffer; import static org.junit.jupiter.api.Assertions.*; @@ -90,6 +91,16 @@ void test_Data_fromByteArray() { } } + @Test + void test_Data_fromByteBuffer() { + try (var arena = AllocatingSwiftArena.ofConfined()) { + byte[] original = new byte[] { 1, 2, 3, 4, 5 }; + ByteBuffer buffer = ByteBuffer.wrap(original); + var data = Data.fromByteBuffer(buffer, arena); + assertEquals(5, data.getCount()); + } + } + @Test void test_Data_toMemorySegment() { try (var arena = AllocatingSwiftArena.ofConfined()) { diff --git a/Sources/JExtractSwiftLib/FFM/FFMSwift2JavaGenerator+FoundationData.swift b/Sources/JExtractSwiftLib/FFM/FFMSwift2JavaGenerator+FoundationData.swift index 7b10ddc42..649543414 100644 --- a/Sources/JExtractSwiftLib/FFM/FFMSwift2JavaGenerator+FoundationData.swift +++ b/Sources/JExtractSwiftLib/FFM/FFMSwift2JavaGenerator+FoundationData.swift @@ -47,7 +47,21 @@ extension FFMSwift2JavaGenerator { """ ) - // TODO: Implement a fromByteBuffer as well + printer.print( + """ + /** + * Creates a new Swift {@link \(typeName)} instance from a {@link ByteBuffer}. + * + * @param buffer The ByteBuffer to copy byte from it into the \(typeName) + * @param arena The arena for memory management + * @return A new \(typeName) instance containing a copy of the bytes + */ + public static \(typeName) fromByteBuffer(java.nio.ByteBuffer buffer, AllocatingSwiftArena arena) { + Objects.requireNonNull(buffer, "buffer cannot be null"); + return Data.init(buffer.array(), arena); + } + """ + ) // Print the descriptor class for copyBytes native call using the shared helper let copyBytesCFunc = CFunction( From 436cc6f6d06cabdf243ad9582adc24fa592ff828 Mon Sep 17 00:00:00 2001 From: Amr Hesham Date: Sun, 19 Jul 2026 18:48:22 +0200 Subject: [PATCH 2/2] Use the typeName not hard code Date type --- .../FFM/FFMSwift2JavaGenerator+FoundationData.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sources/JExtractSwiftLib/FFM/FFMSwift2JavaGenerator+FoundationData.swift b/Sources/JExtractSwiftLib/FFM/FFMSwift2JavaGenerator+FoundationData.swift index 649543414..faab26be1 100644 --- a/Sources/JExtractSwiftLib/FFM/FFMSwift2JavaGenerator+FoundationData.swift +++ b/Sources/JExtractSwiftLib/FFM/FFMSwift2JavaGenerator+FoundationData.swift @@ -58,7 +58,7 @@ extension FFMSwift2JavaGenerator { */ public static \(typeName) fromByteBuffer(java.nio.ByteBuffer buffer, AllocatingSwiftArena arena) { Objects.requireNonNull(buffer, "buffer cannot be null"); - return Data.init(buffer.array(), arena); + return \(typeName).init(buffer.array(), arena); } """ )