Condition to choose BinaryByte instead of BinaryFile

This commit is contained in:
J-Jamet committed 2021-03-18 14:58:56 +01:00
1 parent dea2ad6904
commit 0735cc1a54
4 files changed
+26 -5

No files matched your search

@@ -147,6 +147,7 @@ class BinaryByte : BinaryData {
companion object {
private val TAG = BinaryByte::class.java.name
const val MAX_BINARY_BYTES = 10240
@JvmField
val CREATOR: Parcelable.Creator<BinaryByte> = object : Parcelable.Creator<BinaryByte> {
@@ -316,6 +316,13 @@ class DatabaseKDBX : DatabaseVersioned<UUID, UUID, GroupKDBX, EntryKDBX> {
iconsManager.buildNewCustomIcon(cacheDirectory, customIconId, result)
}
fun addCustomIcon(cacheDirectory: File,
customIconId: UUID? = null,
dataSize: Int,
result: (IconImageCustom, BinaryData?) -> Unit) {
iconsManager.addCustomIcon(cacheDirectory, customIconId, dataSize, result)
}
fun isCustomIconBinaryDuplicate(binary: BinaryData): Boolean {
return iconsManager.isCustomIconBinaryDuplicate(binary)
}
@@ -20,6 +20,8 @@
package com.kunzisoft.keepass.database.element.icon
import android.util.Log
import com.kunzisoft.keepass.database.element.database.BinaryByte
import com.kunzisoft.keepass.database.element.database.BinaryByte.Companion.MAX_BINARY_BYTES
import com.kunzisoft.keepass.database.element.database.BinaryData
import com.kunzisoft.keepass.database.element.database.BinaryFile
import com.kunzisoft.keepass.database.element.database.CustomIconPool
@@ -53,11 +55,22 @@ class IconsManager {
fun buildNewCustomIcon(cacheDirectory: File,
key: UUID? = null,
result: (IconImageCustom, BinaryData?) -> Unit) {
// Create a binary file for a brand new custom icon
addCustomIcon(cacheDirectory, key, -1, result)
}
fun addCustomIcon(cacheDirectory: File,
key: UUID? = null,
dataSize: Int,
result: (IconImageCustom, BinaryData?) -> Unit) {
val keyBinary = customCache.put(key) { uniqueBinaryId ->
//BinaryByte()
// TODO change to BinaryByte to increase performance
val fileInCache = File(cacheDirectory, uniqueBinaryId)
BinaryFile(fileInCache)
// Create a byte array for better performance with small data
if (dataSize in 1..MAX_BINARY_BYTES) {
BinaryByte()
} else {
val fileInCache = File(cacheDirectory, uniqueBinaryId)
BinaryFile(fileInCache)
}
}
result.invoke(IconImageCustom(keyBinary.keys.first()), keyBinary.binary)
}
@@ -704,7 +704,7 @@ class DatabaseInputKDBX(cacheDirectory: File)
return KdbContext.Meta
} else if (ctx == KdbContext.CustomIcon && name.equals(DatabaseKDBXXML.ElemCustomIconItem, ignoreCase = true)) {
if (customIconID != DatabaseVersioned.UUID_ZERO && customIconData != null) {
mDatabase.buildNewCustomIcon(cacheDirectory, customIconID) { _, binary ->
mDatabase.addCustomIcon(cacheDirectory, customIconID, customIconData!!.size) { _, binary ->
mDatabase.loadedCipherKey?.let { cipherKey ->
binary?.getOutputDataStream(cipherKey)?.use { outputStream ->
outputStream.write(customIconData)