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..faab26be1 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 \(typeName).init(buffer.array(), arena); + } + """ + ) // Print the descriptor class for copyBytes native call using the shared helper let copyBytesCFunc = CFunction(