Better method to put binary

This commit is contained in:
J-Jamet
2021-03-18 13:37:33 +01:00
parent 174e562dcb
commit 0f0b6b4a8a
4 changed files with 19 additions and 12 deletions

View File

@@ -43,13 +43,10 @@ abstract class BinaryPool<T> {
/**
* Create and return a new binary file not yet linked to a binary
*/
fun put(cacheDirectory: File,
key: T? = null,
compression: Boolean = false,
protection: Boolean = false): KeyBinary<T> {
val fileInCache = File(cacheDirectory, "$poolId$creationId$binaryFileIncrement")
fun put(key: T? = null,
builder: (uniqueBinaryId: String) -> BinaryData): KeyBinary<T> {
binaryFileIncrement++
val newBinaryFile = BinaryFile(fileInCache, compression, protection)
val newBinaryFile: BinaryData = builder("$poolId$creationId$binaryFileIncrement")
val newKey = put(key, newBinaryFile)
return KeyBinary(newBinaryFile, newKey)
}

View File

@@ -45,7 +45,7 @@ class DatabaseKDB : DatabaseVersioned<Int, UUID, GroupKDB, EntryKDB>() {
private var kdfListV3: MutableList<KdfEngine> = ArrayList()
private var binaryIncrement = 0
private var binaryPool = AttachmentPool()
override val version: String
get() = "KeePass 1"
@@ -276,9 +276,10 @@ class DatabaseKDB : DatabaseVersioned<Int, UUID, GroupKDB, EntryKDB>() {
fun buildNewAttachment(cacheDirectory: File): BinaryData {
// Generate an unique new file
val fileInCache = File(cacheDirectory, binaryIncrement.toString())
binaryIncrement++
return BinaryFile(fileInCache)
return binaryPool.put { uniqueBinaryId ->
val fileInCache = File(cacheDirectory, uniqueBinaryId)
BinaryFile(fileInCache)
}.binary
}
companion object {

View File

@@ -638,7 +638,10 @@ class DatabaseKDBX : DatabaseVersioned<UUID, UUID, GroupKDBX, EntryKDBX> {
compression: Boolean,
protection: Boolean,
binaryPoolId: Int? = null): BinaryData {
return binaryPool.put(cacheDirectory, binaryPoolId, compression, protection).binary
return binaryPool.put(binaryPoolId) { uniqueBinaryId ->
val fileInCache = File(cacheDirectory, uniqueBinaryId)
BinaryFile(fileInCache, compression, protection)
}.binary
}
fun removeUnlinkedAttachment(binary: BinaryData, clear: Boolean) {

View File

@@ -21,6 +21,7 @@ package com.kunzisoft.keepass.database.element.icon
import android.util.Log
import com.kunzisoft.keepass.database.element.database.BinaryData
import com.kunzisoft.keepass.database.element.database.BinaryFile
import com.kunzisoft.keepass.database.element.database.CustomIconPool
import com.kunzisoft.keepass.database.element.icon.IconImageStandard.Companion.KEY_ID
import com.kunzisoft.keepass.icons.IconPack.Companion.NB_ICONS
@@ -52,7 +53,12 @@ class IconsManager {
fun buildNewCustomIcon(cacheDirectory: File,
key: UUID? = null,
result: (IconImageCustom, BinaryData?) -> Unit) {
val keyBinary = customCache.put(cacheDirectory, key)
val keyBinary = customCache.put(key) { uniqueBinaryId ->
//BinaryByte()
// TODO change to BinaryByte to increase performance
val fileInCache = File(cacheDirectory, uniqueBinaryId)
BinaryFile(fileInCache)
}
result.invoke(IconImageCustom(keyBinary.keys.first()), keyBinary.binary)
}