-
Notifications
You must be signed in to change notification settings - Fork 29
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
JavaKit: inout primitive arrays #156
Comments
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 |
Ah, it's tricky to implement this in Swift without reimplementing a lot of JavaKit. The options are really:
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
.The text was updated successfully, but these errors were encountered: