Open lhoward opened 2 weeks ago
Feel free to close if known issue.
I'd also be happy with a special case for byte buffers that lives within JavaKit (there is precedence, JNI has one).
Issues are good, please keep em coming but I'd like to ask if you could qualify that they're all about "JavaKit" or "Java2Swift" because the same kinds of things need to be done in jextract -- it'll be easier to know which side of conversions the issue is about this way.
Anyway, lemme add tags :)
Note to self: may be possible to use jniGetArrayRegion()
. Essentially I'm just trying to bridge java.nio.ByteBuffer
to/from Data.
Ah, it's tricky to implement this in Swift without reimplementing a lot of JavaKit. The options are really:
inout
parameters in JavaKitbyte[]
To unblock progress (well, I was unblocked, but reading a byte at a time!), I've opted for the third option for now.
import FoundationEssentials
import JavaKit
import JavaRuntime
extension Data {
func asJavaNIOByteBuffer() -> JavaNIOByteBuffer {
let byteBufferClass = try! JavaClass<JavaNIOByteBuffer>()
let byteBuffer = byteBufferClass.allocateDirect(Int32(count))!
return byteBuffer.put(map { Int8(bitPattern: $0) }, 0, Int32(count))
}
}
extension JavaNIOByteBuffer {
func asData() throws -> Data {
let array: [Int8]
if hasArray() {
let position = Int(position()), limit = Int(limit())
let offset = Int(arrayOffset()) + position
array = Array(self.array()[offset..<(offset + limit)])
} else {
array = try! JavaClass<ByteBufferHelper>(environment: javaEnvironment)
.getByteBufferContents(self)
}
return Data(array.map { UInt8(bitPattern: $0) })
}
}
Presume this is on the TODO list :)
I'd like to use
but the projection of
byte[]
is notinout
.