Implement Base64 without need of JDK8. (#78)

This commit is contained in:
Nikolay Igotti
2016-11-21 10:42:49 +03:00
committed by GitHub
parent 5161155934
commit 96797a4b2f
4 changed files with 15 additions and 10 deletions
@@ -116,14 +116,7 @@ internal interface ContextUtils {
*/
val String.globalHashBase64: String
get() {
val bytes = this.toByteArray(Charsets.UTF_8)
val hashBytes = this.globalHashBytes
val base64Bytes = java.util.Base64.getEncoder().encode(globalHashBytes)
// TODO: do not use JRE for base64
// (it is impossible right now due to native interop limitations)
return String(base64Bytes, Charsets.US_ASCII)
return base64Encode(globalHashBytes)
}
/**
@@ -21,4 +21,15 @@ internal fun globalHash(data: ByteArray, retValPlacement: Placement): GlobalHash
return res
}
internal fun base64Encode(data: ByteArray): String {
memScoped {
val resultSize = 4 * data.size / 3 + 3 + 1
val result = alloc(NativeArray of Int8Box length resultSize)
val bytes = allocNativeArrayOf(data)
Base64Encode(bytes.ptr, data.size, result.ptr, resultSize)
// TODO: any better way to do that without two copies?
return CString.fromArray(result).toString()
}
}
internal class LocalHash(val value: Long) : ConstValue by Int64(value)