Dev: add Data encoding/decoding
This commit is contained in:
parent
fc83412915
commit
9a361dbac5
|
@ -16,6 +16,10 @@ public protocol JNIArgumentProtocol {
|
|||
public struct jnull: JNIArgumentProtocol {
|
||||
|
||||
let className: String
|
||||
|
||||
public init() {
|
||||
className = ""
|
||||
}
|
||||
|
||||
public func value(locals: UnsafeMutablePointer<[jobject]>) -> jvalue {
|
||||
return jvalue()
|
||||
|
|
|
@ -24,6 +24,7 @@ let SetClassname = "java/util/Set"
|
|||
let UriClassname = "android/net/Uri"
|
||||
let DateClassname = "java/util/Date"
|
||||
let HashSetClassname = "java/util/HashSet"
|
||||
let ByteBufferClassname = "java/nio/ByteBuffer"
|
||||
|
||||
// MARK: Java Classes
|
||||
var IntegerClass = try! JNI.getJavaClass("java/lang/Integer")
|
||||
|
@ -38,6 +39,7 @@ let UriClass = try! JNI.getJavaClass("android/net/Uri")
|
|||
let DateClass = try! JNI.getJavaClass("java/util/Date")
|
||||
let VMDebugClass = try! JNI.getJavaClass("dalvik/system/VMDebug")
|
||||
let HashSetClass = try! JNI.getJavaClass("java/util/HashSet")
|
||||
let ByteBufferClass = try! JNI.getJavaClass("java/nio/ByteBuffer")
|
||||
|
||||
// MARK: Java methods
|
||||
let UriConstructor = JNI.api.GetStaticMethodID(JNI.env, UriClass, "parse", "(Ljava/lang/String;)Landroid/net/Uri;")
|
||||
|
@ -71,6 +73,8 @@ let CollectionSizeMethod = try! JNI.getJavaMethod(forClass: "java/util/Collectio
|
|||
let IteratorNextMethod = try! JNI.getJavaMethod(forClass: "java/util/Iterator", method: "next", sig: "()Ljava/lang/Object;")
|
||||
let DateGetTimeMethod = try! JNI.getJavaMethod(forClass: "java/util/Date", method: "getTime", sig:"()J")
|
||||
let VMDebugDumpReferenceTablesMethod = JNI.api.GetStaticMethodID(JNI.env, VMDebugClass, "dumpReferenceTables", "()V")
|
||||
let ByteBufferArray = try! JNI.getJavaMethod(forClass: "java/nio/ByteBuffer", method: "array", sig: "()[B")
|
||||
let ByteBufferWrap = JNI.api.GetStaticMethodID(JNI.env, ByteBufferClass, "wrap", "([B)Ljava/nio/ByteBuffer;")!
|
||||
|
||||
fileprivate extension NSLock {
|
||||
|
||||
|
|
|
@ -4,6 +4,7 @@
|
|||
|
||||
import Foundation
|
||||
import java_swift
|
||||
import CJavaVM
|
||||
|
||||
public typealias JavaEncodableClosure = (Encodable) throws -> jobject
|
||||
public typealias JavaDecodableClosure = (jobject) throws -> Decodable
|
||||
|
@ -154,6 +155,25 @@ public struct JavaCoderConfig {
|
|||
let pathString = JNI.api.CallObjectMethodA(JNI.env, $0, ObjectToStringMethod, nil)
|
||||
return URL(string: String(javaObject: pathString))
|
||||
})
|
||||
|
||||
RegisterType(type: Data.self, javaClassname: ByteBufferClassname, encodableClosure: {
|
||||
let valueData = $0 as! Data
|
||||
let byteArray = JNI.api.NewByteArray(JNI.env, valueData.count)!
|
||||
valueData.withUnsafeBytes({ (pointer: UnsafePointer<Int8>) -> Void in
|
||||
JNI.api.SetByteArrayRegion(JNI.env, byteArray, 0, valueData.count, pointer)
|
||||
})
|
||||
return JNI.CallStaticObjectMethod(ByteBufferClass, methodID: ByteBufferWrap, args: [jvalue(l: byteArray)])!
|
||||
}, decodableClosure: {
|
||||
let byteArray = JNI.CallObjectMethod($0, methodID: ByteBufferArray)
|
||||
guard let pointer = JNI.api.GetByteArrayElements(JNI.env, byteArray, nil) else {
|
||||
throw JavaCodingError.cantFindObject("ByteBuffer")
|
||||
}
|
||||
let length = JNI.api.GetArrayLength(JNI.env, byteArray)
|
||||
defer {
|
||||
JNI.api.ReleaseByteArrayElements(JNI.env, byteArray, pointer, 0)
|
||||
}
|
||||
return Data.init(bytes: pointer, count: length)
|
||||
})
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue