Rename BinaryFile by BinaryData

This commit is contained in:
J-Jamet
2021-03-18 11:42:28 +01:00
parent 621415fe51
commit f080750545
18 changed files with 125 additions and 126 deletions

View File

@@ -3,7 +3,7 @@ package com.kunzisoft.keepass.tests.stream
import android.content.Context import android.content.Context
import androidx.test.platform.app.InstrumentationRegistry import androidx.test.platform.app.InstrumentationRegistry
import com.kunzisoft.keepass.database.element.Database import com.kunzisoft.keepass.database.element.Database
import com.kunzisoft.keepass.database.element.database.BinaryFile import com.kunzisoft.keepass.database.element.database.BinaryData
import com.kunzisoft.keepass.stream.readAllBytes import com.kunzisoft.keepass.stream.readAllBytes
import com.kunzisoft.keepass.utils.UriUtil import com.kunzisoft.keepass.utils.UriUtil
import junit.framework.TestCase.assertEquals import junit.framework.TestCase.assertEquals
@@ -12,7 +12,7 @@ import java.io.DataInputStream
import java.io.File import java.io.File
import java.io.InputStream import java.io.InputStream
class BinaryFileTest { class BinaryDataTest {
private val context: Context by lazy { private val context: Context by lazy {
InstrumentationRegistry.getInstrumentation().context InstrumentationRegistry.getInstrumentation().context
@@ -25,9 +25,9 @@ class BinaryFileTest {
private val loadedKey = Database.LoadedKey.generateNewCipherKey() private val loadedKey = Database.LoadedKey.generateNewCipherKey()
private fun saveBinary(asset: String, binaryFile: BinaryFile) { private fun saveBinary(asset: String, binaryData: BinaryData) {
context.assets.open(asset).use { assetInputStream -> context.assets.open(asset).use { assetInputStream ->
binaryFile.getOutputDataStream(loadedKey).use { binaryOutputStream -> binaryData.getOutputDataStream(loadedKey).use { binaryOutputStream ->
assetInputStream.readAllBytes(DEFAULT_BUFFER_SIZE) { buffer -> assetInputStream.readAllBytes(DEFAULT_BUFFER_SIZE) { buffer ->
binaryOutputStream.write(buffer) binaryOutputStream.write(buffer)
} }
@@ -37,8 +37,8 @@ class BinaryFileTest {
@Test @Test
fun testSaveTextInCache() { fun testSaveTextInCache() {
val binaryA = BinaryFile(fileA) val binaryA = BinaryData(fileA)
val binaryB = BinaryFile(fileB) val binaryB = BinaryData(fileB)
saveBinary(TEST_TEXT_ASSET, binaryA) saveBinary(TEST_TEXT_ASSET, binaryA)
saveBinary(TEST_TEXT_ASSET, binaryB) saveBinary(TEST_TEXT_ASSET, binaryB)
assertEquals("Save text binary length failed.", binaryA.getSize(), binaryB.getSize()) assertEquals("Save text binary length failed.", binaryA.getSize(), binaryB.getSize())
@@ -47,8 +47,8 @@ class BinaryFileTest {
@Test @Test
fun testSaveImageInCache() { fun testSaveImageInCache() {
val binaryA = BinaryFile(fileA) val binaryA = BinaryData(fileA)
val binaryB = BinaryFile(fileB) val binaryB = BinaryData(fileB)
saveBinary(TEST_IMAGE_ASSET, binaryA) saveBinary(TEST_IMAGE_ASSET, binaryA)
saveBinary(TEST_IMAGE_ASSET, binaryB) saveBinary(TEST_IMAGE_ASSET, binaryB)
assertEquals("Save image binary length failed.", binaryA.getSize(), binaryB.getSize()) assertEquals("Save image binary length failed.", binaryA.getSize(), binaryB.getSize())
@@ -57,9 +57,9 @@ class BinaryFileTest {
@Test @Test
fun testCompressText() { fun testCompressText() {
val binaryA = BinaryFile(fileA) val binaryA = BinaryData(fileA)
val binaryB = BinaryFile(fileB) val binaryB = BinaryData(fileB)
val binaryC = BinaryFile(fileC) val binaryC = BinaryData(fileC)
saveBinary(TEST_TEXT_ASSET, binaryA) saveBinary(TEST_TEXT_ASSET, binaryA)
saveBinary(TEST_TEXT_ASSET, binaryB) saveBinary(TEST_TEXT_ASSET, binaryB)
saveBinary(TEST_TEXT_ASSET, binaryC) saveBinary(TEST_TEXT_ASSET, binaryC)
@@ -74,9 +74,9 @@ class BinaryFileTest {
@Test @Test
fun testCompressImage() { fun testCompressImage() {
val binaryA = BinaryFile(fileA) val binaryA = BinaryData(fileA)
var binaryB = BinaryFile(fileB) var binaryB = BinaryData(fileB)
val binaryC = BinaryFile(fileC) val binaryC = BinaryData(fileC)
saveBinary(TEST_IMAGE_ASSET, binaryA) saveBinary(TEST_IMAGE_ASSET, binaryA)
saveBinary(TEST_IMAGE_ASSET, binaryB) saveBinary(TEST_IMAGE_ASSET, binaryB)
saveBinary(TEST_IMAGE_ASSET, binaryC) saveBinary(TEST_IMAGE_ASSET, binaryC)
@@ -84,7 +84,7 @@ class BinaryFileTest {
binaryB.compress(loadedKey) binaryB.compress(loadedKey)
assertEquals("Compress image length failed.", binaryA.getSize(), binaryA.getSize()) assertEquals("Compress image length failed.", binaryA.getSize(), binaryA.getSize())
assertEquals("Compress image failed.", binaryA.binaryHash(), binaryA.binaryHash()) assertEquals("Compress image failed.", binaryA.binaryHash(), binaryA.binaryHash())
binaryB = BinaryFile(fileB, true) binaryB = BinaryData(fileB, true)
binaryB.decompress(loadedKey) binaryB.decompress(loadedKey)
assertEquals("Decompress image length failed.", binaryB.getSize(), binaryC.getSize()) assertEquals("Decompress image length failed.", binaryB.getSize(), binaryC.getSize())
assertEquals("Decompress image failed.", binaryB.binaryHash(), binaryC.binaryHash()) assertEquals("Decompress image failed.", binaryB.binaryHash(), binaryC.binaryHash())
@@ -92,7 +92,7 @@ class BinaryFileTest {
@Test @Test
fun testReadText() { fun testReadText() {
val binaryA = BinaryFile(fileA) val binaryA = BinaryData(fileA)
saveBinary(TEST_TEXT_ASSET, binaryA) saveBinary(TEST_TEXT_ASSET, binaryA)
assert(streamAreEquals(context.assets.open(TEST_TEXT_ASSET), assert(streamAreEquals(context.assets.open(TEST_TEXT_ASSET),
binaryA.getInputDataStream(loadedKey))) binaryA.getInputDataStream(loadedKey)))
@@ -100,7 +100,7 @@ class BinaryFileTest {
@Test @Test
fun testReadImage() { fun testReadImage() {
val binaryA = BinaryFile(fileA) val binaryA = BinaryData(fileA)
saveBinary(TEST_IMAGE_ASSET, binaryA) saveBinary(TEST_IMAGE_ASSET, binaryA)
assert(streamAreEquals(context.assets.open(TEST_IMAGE_ASSET), assert(streamAreEquals(context.assets.open(TEST_IMAGE_ASSET),
binaryA.getInputDataStream(loadedKey))) binaryA.getInputDataStream(loadedKey)))

View File

@@ -64,10 +64,10 @@ class ImageViewerActivity : LockingActivity() {
intent.getParcelableExtra<Attachment>(IMAGE_ATTACHMENT_TAG)?.let { attachment -> intent.getParcelableExtra<Attachment>(IMAGE_ATTACHMENT_TAG)?.let { attachment ->
supportActionBar?.title = attachment.name supportActionBar?.title = attachment.name
supportActionBar?.subtitle = Formatter.formatFileSize(this, attachment.binaryFile.getSize()) supportActionBar?.subtitle = Formatter.formatFileSize(this, attachment.binaryData.getSize())
BinaryDatabaseManager.loadBitmap( BinaryDatabaseManager.loadBitmap(
attachment.binaryFile, attachment.binaryData,
Database.getInstance().loadedCipherKey, Database.getInstance().loadedCipherKey,
mImagePreviewMaxWidth mImagePreviewMaxWidth
) { bitmapLoaded -> ) { bitmapLoaded ->

View File

@@ -83,7 +83,7 @@ class EntryAttachmentsItemsAdapter(context: Context)
entryAttachmentState.previewState = AttachmentState.IN_PROGRESS entryAttachmentState.previewState = AttachmentState.IN_PROGRESS
// Load the bitmap image // Load the bitmap image
BinaryDatabaseManager.loadBitmap( BinaryDatabaseManager.loadBitmap(
entryAttachmentState.attachment.binaryFile, entryAttachmentState.attachment.binaryData,
binaryCipherKey, binaryCipherKey,
mImagePreviewMaxWidth mImagePreviewMaxWidth
) { imageLoaded -> ) { imageLoaded ->
@@ -111,22 +111,22 @@ class EntryAttachmentsItemsAdapter(context: Context)
} }
holder.binaryFileBroken.apply { holder.binaryFileBroken.apply {
setColorFilter(Color.RED) setColorFilter(Color.RED)
visibility = if (entryAttachmentState.attachment.binaryFile.isCorrupted) { visibility = if (entryAttachmentState.attachment.binaryData.isCorrupted) {
View.VISIBLE View.VISIBLE
} else { } else {
View.GONE View.GONE
} }
} }
holder.binaryFileTitle.text = entryAttachmentState.attachment.name holder.binaryFileTitle.text = entryAttachmentState.attachment.name
if (entryAttachmentState.attachment.binaryFile.isCorrupted) { if (entryAttachmentState.attachment.binaryData.isCorrupted) {
holder.binaryFileTitle.setTextColor(Color.RED) holder.binaryFileTitle.setTextColor(Color.RED)
} else { } else {
holder.binaryFileTitle.setTextColor(mTitleColor) holder.binaryFileTitle.setTextColor(mTitleColor)
} }
holder.binaryFileSize.text = Formatter.formatFileSize(context, holder.binaryFileSize.text = Formatter.formatFileSize(context,
entryAttachmentState.attachment.binaryFile.getSize()) entryAttachmentState.attachment.binaryData.getSize())
holder.binaryFileCompression.apply { holder.binaryFileCompression.apply {
if (entryAttachmentState.attachment.binaryFile.isCompressed) { if (entryAttachmentState.attachment.binaryData.isCompressed) {
text = CompressionAlgorithm.GZip.getName(context.resources) text = CompressionAlgorithm.GZip.getName(context.resources)
visibility = View.VISIBLE visibility = View.VISIBLE
} else { } else {

View File

@@ -49,7 +49,7 @@ class UpdateEntryRunnable constructor(
newEntryAttachments.forEach { newAttachment -> newEntryAttachments.forEach { newAttachment ->
oldEntryAttachments.forEach { oldAttachment -> oldEntryAttachments.forEach { oldAttachment ->
if (oldAttachment.name == newAttachment.name if (oldAttachment.name == newAttachment.name
&& oldAttachment.binaryFile == newAttachment.binaryFile) && oldAttachment.binaryData == newAttachment.binaryData)
attachmentsToRemove.remove(oldAttachment) attachmentsToRemove.remove(oldAttachment)
} }
} }

View File

@@ -21,20 +21,20 @@ package com.kunzisoft.keepass.database.element
import android.os.Parcel import android.os.Parcel
import android.os.Parcelable import android.os.Parcelable
import com.kunzisoft.keepass.database.element.database.BinaryFile import com.kunzisoft.keepass.database.element.database.BinaryData
data class Attachment(var name: String, data class Attachment(var name: String,
var binaryFile: BinaryFile) : Parcelable { var binaryData: BinaryData) : Parcelable {
constructor(parcel: Parcel) : this( constructor(parcel: Parcel) : this(
parcel.readString() ?: "", parcel.readString() ?: "",
parcel.readParcelable(BinaryFile::class.java.classLoader) ?: BinaryFile() parcel.readParcelable(BinaryData::class.java.classLoader) ?: BinaryData()
) )
override fun writeToParcel(parcel: Parcel, flags: Int) { override fun writeToParcel(parcel: Parcel, flags: Int) {
parcel.writeString(name) parcel.writeString(name)
parcel.writeParcelable(binaryFile, flags) parcel.writeParcelable(binaryData, flags)
} }
override fun describeContents(): Int { override fun describeContents(): Int {
@@ -42,7 +42,7 @@ data class Attachment(var name: String,
} }
override fun toString(): String { override fun toString(): String {
return "$name at $binaryFile" return "$name at $binaryData"
} }
override fun equals(other: Any?): Boolean { override fun equals(other: Any?): Boolean {

View File

@@ -117,7 +117,7 @@ class Database {
val allowCustomIcons: Boolean val allowCustomIcons: Boolean
get() = mDatabaseKDBX != null get() = mDatabaseKDBX != null
fun doForEachCustomIcons(action: (IconImageCustom, BinaryFile) -> Unit) { fun doForEachCustomIcons(action: (IconImageCustom, BinaryData) -> Unit) {
return iconsManager.doForEachCustomIcon(action) return iconsManager.doForEachCustomIcon(action)
} }
@@ -126,12 +126,12 @@ class Database {
} }
fun buildNewCustomIcon(cacheDirectory: File, fun buildNewCustomIcon(cacheDirectory: File,
result: (IconImageCustom?, BinaryFile?) -> Unit) { result: (IconImageCustom?, BinaryData?) -> Unit) {
mDatabaseKDBX?.buildNewCustomIcon(cacheDirectory, null, result) mDatabaseKDBX?.buildNewCustomIcon(cacheDirectory, null, result)
} }
fun isCustomIconBinaryDuplicate(binaryFile: BinaryFile): Boolean { fun isCustomIconBinaryDuplicate(binaryData: BinaryData): Boolean {
return mDatabaseKDBX?.isCustomIconBinaryDuplicate(binaryFile) ?: false return mDatabaseKDBX?.isCustomIconBinaryDuplicate(binaryData) ?: false
} }
fun removeCustomIcon(customIcon: IconImageCustom) { fun removeCustomIcon(customIcon: IconImageCustom) {
@@ -590,7 +590,7 @@ class Database {
fun buildNewBinaryAttachment(cacheDirectory: File, fun buildNewBinaryAttachment(cacheDirectory: File,
compressed: Boolean = false, compressed: Boolean = false,
protected: Boolean = false): BinaryFile? { protected: Boolean = false): BinaryData? {
return mDatabaseKDB?.buildNewAttachment(cacheDirectory) return mDatabaseKDB?.buildNewAttachment(cacheDirectory)
?: mDatabaseKDBX?.buildNewAttachment(cacheDirectory, compressed, protected) ?: mDatabaseKDBX?.buildNewAttachment(cacheDirectory, compressed, protected)
} }
@@ -598,7 +598,7 @@ class Database {
fun removeAttachmentIfNotUsed(attachment: Attachment) { fun removeAttachmentIfNotUsed(attachment: Attachment) {
// No need in KDB database because unique attachment by entry // No need in KDB database because unique attachment by entry
// Don't clear to fix upload multiple times // Don't clear to fix upload multiple times
mDatabaseKDBX?.removeUnlinkedAttachment(attachment.binaryFile, false) mDatabaseKDBX?.removeUnlinkedAttachment(attachment.binaryData, false)
} }
fun removeUnlinkedAttachments() { fun removeUnlinkedAttachments() {

View File

@@ -37,7 +37,7 @@ import javax.crypto.CipherInputStream
import javax.crypto.CipherOutputStream import javax.crypto.CipherOutputStream
import javax.crypto.spec.IvParameterSpec import javax.crypto.spec.IvParameterSpec
class BinaryFile : Parcelable { class BinaryData : Parcelable {
private var mDataFile: File? = null private var mDataFile: File? = null
private var mLength: Long = 0 private var mLength: Long = 0
@@ -199,7 +199,7 @@ class BinaryFile : Parcelable {
return true return true
if (other == null || javaClass != other.javaClass) if (other == null || javaClass != other.javaClass)
return false return false
if (other !is BinaryFile) if (other !is BinaryData)
return false return false
var sameData = false var sameData = false
@@ -281,15 +281,15 @@ class BinaryFile : Parcelable {
companion object { companion object {
private val TAG = BinaryFile::class.java.name private val TAG = BinaryData::class.java.name
@JvmField @JvmField
val CREATOR: Parcelable.Creator<BinaryFile> = object : Parcelable.Creator<BinaryFile> { val CREATOR: Parcelable.Creator<BinaryData> = object : Parcelable.Creator<BinaryData> {
override fun createFromParcel(parcel: Parcel): BinaryFile { override fun createFromParcel(parcel: Parcel): BinaryData {
return BinaryFile(parcel) return BinaryData(parcel)
} }
override fun newArray(size: Int): Array<BinaryFile?> { override fun newArray(size: Int): Array<BinaryData?> {
return arrayOfNulls(size) return arrayOfNulls(size)
} }
} }

View File

@@ -26,7 +26,7 @@ import kotlin.math.abs
abstract class BinaryPool<T> { abstract class BinaryPool<T> {
protected val pool = LinkedHashMap<T, BinaryFile>() protected val pool = LinkedHashMap<T, BinaryData>()
// To build unique file id // To build unique file id
private var creationId: String = System.currentTimeMillis().toString() private var creationId: String = System.currentTimeMillis().toString()
@@ -36,7 +36,7 @@ abstract class BinaryPool<T> {
/** /**
* To get a binary by the pool key (ref attribute in entry) * To get a binary by the pool key (ref attribute in entry)
*/ */
operator fun get(key: T): BinaryFile? { operator fun get(key: T): BinaryData? {
return pool[key] return pool[key]
} }
@@ -49,7 +49,7 @@ abstract class BinaryPool<T> {
protection: Boolean = false): KeyBinary<T> { protection: Boolean = false): KeyBinary<T> {
val fileInCache = File(cacheDirectory, "$poolId$creationId$binaryFileIncrement") val fileInCache = File(cacheDirectory, "$poolId$creationId$binaryFileIncrement")
binaryFileIncrement++ binaryFileIncrement++
val newBinaryFile = BinaryFile(fileInCache, compression, protection) val newBinaryFile = BinaryData(fileInCache, compression, protection)
val newKey = put(key, newBinaryFile) val newKey = put(key, newBinaryFile)
return KeyBinary(newBinaryFile, newKey) return KeyBinary(newBinaryFile, newKey)
} }
@@ -57,7 +57,7 @@ abstract class BinaryPool<T> {
/** /**
* To linked a binary with a pool key, if the pool key doesn't exists, create an unused one * To linked a binary with a pool key, if the pool key doesn't exists, create an unused one
*/ */
fun put(key: T?, value: BinaryFile): T { fun put(key: T?, value: BinaryData): T {
if (key == null) if (key == null)
return put(value) return put(value)
else else
@@ -66,16 +66,16 @@ abstract class BinaryPool<T> {
} }
/** /**
* To put a [binaryFile] in the pool, * To put a [binaryData] in the pool,
* if already exists, replace the current one, * if already exists, replace the current one,
* else add it with a new key * else add it with a new key
*/ */
fun put(binaryFile: BinaryFile): T { fun put(binaryData: BinaryData): T {
var key: T? = findKey(binaryFile) var key: T? = findKey(binaryData)
if (key == null) { if (key == null) {
key = findUnusedKey() key = findUnusedKey()
} }
pool[key!!] = binaryFile pool[key!!] = binaryData
return key return key
} }
@@ -92,8 +92,8 @@ abstract class BinaryPool<T> {
* Remove a binary from the pool, the file is not deleted * Remove a binary from the pool, the file is not deleted
*/ */
@Throws(IOException::class) @Throws(IOException::class)
fun remove(binaryFile: BinaryFile) { fun remove(binaryData: BinaryData) {
findKey(binaryFile)?.let { findKey(binaryData)?.let {
pool.remove(it) pool.remove(it)
} }
// Don't clear attachment here because a file can be used in many BinaryAttachment // Don't clear attachment here because a file can be used in many BinaryAttachment
@@ -105,15 +105,15 @@ abstract class BinaryPool<T> {
abstract fun findUnusedKey(): T abstract fun findUnusedKey(): T
/** /**
* Return key of [binaryFileToRetrieve] or null if not found * Return key of [binaryDataToRetrieve] or null if not found
*/ */
private fun findKey(binaryFileToRetrieve: BinaryFile): T? { private fun findKey(binaryDataToRetrieve: BinaryData): T? {
val contains = pool.containsValue(binaryFileToRetrieve) val contains = pool.containsValue(binaryDataToRetrieve)
return if (!contains) return if (!contains)
null null
else { else {
for ((key, binary) in pool) { for ((key, binary) in pool) {
if (binary == binaryFileToRetrieve) { if (binary == binaryDataToRetrieve) {
return key return key
} }
} }
@@ -121,9 +121,9 @@ abstract class BinaryPool<T> {
} }
} }
fun isBinaryDuplicate(binaryFile: BinaryFile?): Boolean { fun isBinaryDuplicate(binaryData: BinaryData?): Boolean {
try { try {
binaryFile?.let { binaryData?.let {
if (it.getSize() > 0) { if (it.getSize() > 0) {
val searchBinaryMD5 = it.binaryHash() val searchBinaryMD5 = it.binaryHash()
var i = 0 var i = 0
@@ -145,8 +145,8 @@ abstract class BinaryPool<T> {
/** /**
* To do an action on each binary in the pool (order is not important) * To do an action on each binary in the pool (order is not important)
*/ */
private fun doForEachBinary(action: (key: T, binary: BinaryFile) -> Unit, private fun doForEachBinary(action: (key: T, binary: BinaryData) -> Unit,
condition: (key: T, binary: BinaryFile) -> Boolean) { condition: (key: T, binary: BinaryData) -> Boolean) {
for ((key, value) in pool) { for ((key, value) in pool) {
if (condition.invoke(key, value)) { if (condition.invoke(key, value)) {
action.invoke(key, value) action.invoke(key, value)
@@ -154,14 +154,14 @@ abstract class BinaryPool<T> {
} }
} }
fun doForEachBinary(action: (key: T, binary: BinaryFile) -> Unit) { fun doForEachBinary(action: (key: T, binary: BinaryData) -> Unit) {
doForEachBinary(action) { _, _ -> true } doForEachBinary(action) { _, _ -> true }
} }
/** /**
* Utility method to order binaries and solve index problem in database v4 * Utility method to order binaries and solve index problem in database v4
*/ */
protected fun orderedBinariesWithoutDuplication(condition: ((binary: BinaryFile) -> Boolean) = { true }) protected fun orderedBinariesWithoutDuplication(condition: ((binary: BinaryData) -> Boolean) = { true })
: List<KeyBinary<T>> { : List<KeyBinary<T>> {
val keyBinaryList = ArrayList<KeyBinary<T>>() val keyBinaryList = ArrayList<KeyBinary<T>>()
for ((key, binary) in pool) { for ((key, binary) in pool) {
@@ -199,7 +199,7 @@ abstract class BinaryPool<T> {
* Different from doForEach, provide an ordered index to each binary * Different from doForEach, provide an ordered index to each binary
*/ */
private fun doForEachBinaryWithoutDuplication(action: (keyBinary: KeyBinary<T>) -> Unit, private fun doForEachBinaryWithoutDuplication(action: (keyBinary: KeyBinary<T>) -> Unit,
conditionToAdd: (binary: BinaryFile) -> Boolean) { conditionToAdd: (binary: BinaryData) -> Boolean) {
orderedBinariesWithoutDuplication(conditionToAdd).forEach { keyBinary -> orderedBinariesWithoutDuplication(conditionToAdd).forEach { keyBinary ->
action.invoke(keyBinary) action.invoke(keyBinary)
} }
@@ -212,14 +212,14 @@ abstract class BinaryPool<T> {
/** /**
* Different from doForEach, provide an ordered index to each binary * Different from doForEach, provide an ordered index to each binary
*/ */
private fun doForEachOrderedBinaryWithoutDuplication(action: (index: Int, binary: BinaryFile) -> Unit, private fun doForEachOrderedBinaryWithoutDuplication(action: (index: Int, binary: BinaryData) -> Unit,
conditionToAdd: (binary: BinaryFile) -> Boolean) { conditionToAdd: (binary: BinaryData) -> Boolean) {
orderedBinariesWithoutDuplication(conditionToAdd).forEachIndexed { index, keyBinary -> orderedBinariesWithoutDuplication(conditionToAdd).forEachIndexed { index, keyBinary ->
action.invoke(index, keyBinary.binary) action.invoke(index, keyBinary.binary)
} }
} }
fun doForEachOrderedBinaryWithoutDuplication(action: (index: Int, binary: BinaryFile) -> Unit) { fun doForEachOrderedBinaryWithoutDuplication(action: (index: Int, binary: BinaryData) -> Unit) {
doForEachOrderedBinaryWithoutDuplication(action, { true }) doForEachOrderedBinaryWithoutDuplication(action, { true })
} }
@@ -249,7 +249,7 @@ abstract class BinaryPool<T> {
/** /**
* Utility class to order binaries * Utility class to order binaries
*/ */
class KeyBinary<T>(val binary: BinaryFile, key: T) { class KeyBinary<T>(val binary: BinaryData, key: T) {
val keys = HashSet<T>() val keys = HashSet<T>()
init { init {
addKey(key) addKey(key)

View File

@@ -274,11 +274,11 @@ class DatabaseKDB : DatabaseVersioned<Int, UUID, GroupKDB, EntryKDB>() {
addEntryTo(entry, origParent) addEntryTo(entry, origParent)
} }
fun buildNewAttachment(cacheDirectory: File): BinaryFile { fun buildNewAttachment(cacheDirectory: File): BinaryData {
// Generate an unique new file // Generate an unique new file
val fileInCache = File(cacheDirectory, binaryIncrement.toString()) val fileInCache = File(cacheDirectory, binaryIncrement.toString())
binaryIncrement++ binaryIncrement++
return BinaryFile(fileInCache) return BinaryData(fileInCache)
} }
companion object { companion object {

View File

@@ -312,11 +312,11 @@ class DatabaseKDBX : DatabaseVersioned<UUID, UUID, GroupKDBX, EntryKDBX> {
fun buildNewCustomIcon(cacheDirectory: File, fun buildNewCustomIcon(cacheDirectory: File,
customIconId: UUID? = null, customIconId: UUID? = null,
result: (IconImageCustom, BinaryFile?) -> Unit) { result: (IconImageCustom, BinaryData?) -> Unit) {
iconsManager.buildNewCustomIcon(cacheDirectory, customIconId, result) iconsManager.buildNewCustomIcon(cacheDirectory, customIconId, result)
} }
fun isCustomIconBinaryDuplicate(binary: BinaryFile): Boolean { fun isCustomIconBinaryDuplicate(binary: BinaryData): Boolean {
return iconsManager.isCustomIconBinaryDuplicate(binary) return iconsManager.isCustomIconBinaryDuplicate(binary)
} }
@@ -637,12 +637,12 @@ class DatabaseKDBX : DatabaseVersioned<UUID, UUID, GroupKDBX, EntryKDBX> {
fun buildNewAttachment(cacheDirectory: File, fun buildNewAttachment(cacheDirectory: File,
compression: Boolean, compression: Boolean,
protection: Boolean, protection: Boolean,
binaryPoolId: Int? = null): BinaryFile { binaryPoolId: Int? = null): BinaryData {
return binaryPool.put(cacheDirectory, binaryPoolId, compression, protection).binary return binaryPool.put(cacheDirectory, binaryPoolId, compression, protection).binary
} }
fun removeUnlinkedAttachment(binary: BinaryFile, clear: Boolean) { fun removeUnlinkedAttachment(binary: BinaryData, clear: Boolean) {
val listBinaries = ArrayList<BinaryFile>() val listBinaries = ArrayList<BinaryData>()
listBinaries.add(binary) listBinaries.add(binary)
removeUnlinkedAttachments(listBinaries, clear) removeUnlinkedAttachments(listBinaries, clear)
} }
@@ -651,9 +651,9 @@ class DatabaseKDBX : DatabaseVersioned<UUID, UUID, GroupKDBX, EntryKDBX> {
removeUnlinkedAttachments(emptyList(), clear) removeUnlinkedAttachments(emptyList(), clear)
} }
private fun removeUnlinkedAttachments(binaries: List<BinaryFile>, clear: Boolean) { private fun removeUnlinkedAttachments(binaries: List<BinaryData>, clear: Boolean) {
// Build binaries to remove with all binaries known // Build binaries to remove with all binaries known
val binariesToRemove = ArrayList<BinaryFile>() val binariesToRemove = ArrayList<BinaryData>()
if (binaries.isEmpty()) { if (binaries.isEmpty()) {
binaryPool.doForEachBinary { _, binary -> binaryPool.doForEachBinary { _, binary ->
binariesToRemove.add(binary) binariesToRemove.add(binary)
@@ -665,7 +665,7 @@ class DatabaseKDBX : DatabaseVersioned<UUID, UUID, GroupKDBX, EntryKDBX> {
rootGroup?.doForEachChild(object : NodeHandler<EntryKDBX>() { rootGroup?.doForEachChild(object : NodeHandler<EntryKDBX>() {
override fun operate(node: EntryKDBX): Boolean { override fun operate(node: EntryKDBX): Boolean {
node.getAttachments(binaryPool, true).forEach { node.getAttachments(binaryPool, true).forEach {
binariesToRemove.remove(it.binaryFile) binariesToRemove.remove(it.binaryData)
} }
return binariesToRemove.isNotEmpty() return binariesToRemove.isNotEmpty()
} }

View File

@@ -22,7 +22,7 @@ package com.kunzisoft.keepass.database.element.entry
import android.os.Parcel import android.os.Parcel
import android.os.Parcelable import android.os.Parcelable
import com.kunzisoft.keepass.database.element.Attachment import com.kunzisoft.keepass.database.element.Attachment
import com.kunzisoft.keepass.database.element.database.BinaryFile import com.kunzisoft.keepass.database.element.database.BinaryData
import com.kunzisoft.keepass.database.element.group.GroupKDB import com.kunzisoft.keepass.database.element.group.GroupKDB
import com.kunzisoft.keepass.database.element.icon.IconImageStandard.Companion.KEY_ID import com.kunzisoft.keepass.database.element.icon.IconImageStandard.Companion.KEY_ID
import com.kunzisoft.keepass.database.element.node.NodeId import com.kunzisoft.keepass.database.element.node.NodeId
@@ -56,7 +56,7 @@ class EntryKDB : EntryVersioned<Int, UUID, GroupKDB, EntryKDB>, NodeKDBInterface
/** A string describing what is in binaryData */ /** A string describing what is in binaryData */
var binaryDescription = "" var binaryDescription = ""
var binaryData: BinaryFile? = null var binaryData: BinaryData? = null
// Determine if this is a MetaStream entry // Determine if this is a MetaStream entry
val isMetaStream: Boolean val isMetaStream: Boolean
@@ -89,7 +89,7 @@ class EntryKDB : EntryVersioned<Int, UUID, GroupKDB, EntryKDB>, NodeKDBInterface
url = parcel.readString() ?: url url = parcel.readString() ?: url
notes = parcel.readString() ?: notes notes = parcel.readString() ?: notes
binaryDescription = parcel.readString() ?: binaryDescription binaryDescription = parcel.readString() ?: binaryDescription
binaryData = parcel.readParcelable(BinaryFile::class.java.classLoader) binaryData = parcel.readParcelable(BinaryData::class.java.classLoader)
} }
override fun readParentParcelable(parcel: Parcel): GroupKDB? { override fun readParentParcelable(parcel: Parcel): GroupKDB? {
@@ -151,7 +151,7 @@ class EntryKDB : EntryVersioned<Int, UUID, GroupKDB, EntryKDB>, NodeKDBInterface
fun putAttachment(attachment: Attachment) { fun putAttachment(attachment: Attachment) {
this.binaryDescription = attachment.name this.binaryDescription = attachment.name
this.binaryData = attachment.binaryFile this.binaryData = attachment.binaryData
} }
fun removeAttachment(attachment: Attachment? = null) { fun removeAttachment(attachment: Attachment? = null) {

View File

@@ -282,7 +282,7 @@ class EntryKDBX : EntryVersioned<UUID, UUID, GroupKDBX, EntryKDBX>, NodeKDBXInte
} }
fun putAttachment(attachment: Attachment, attachmentPool: AttachmentPool) { fun putAttachment(attachment: Attachment, attachmentPool: AttachmentPool) {
binaries[attachment.name] = attachmentPool.put(attachment.binaryFile) binaries[attachment.name] = attachmentPool.put(attachment.binaryData)
} }
fun removeAttachment(attachment: Attachment) { fun removeAttachment(attachment: Attachment) {

View File

@@ -20,13 +20,12 @@
package com.kunzisoft.keepass.database.element.icon package com.kunzisoft.keepass.database.element.icon
import android.util.Log import android.util.Log
import com.kunzisoft.keepass.database.element.database.BinaryFile import com.kunzisoft.keepass.database.element.database.BinaryData
import com.kunzisoft.keepass.database.element.database.CustomIconPool import com.kunzisoft.keepass.database.element.database.CustomIconPool
import com.kunzisoft.keepass.database.element.icon.IconImageStandard.Companion.KEY_ID import com.kunzisoft.keepass.database.element.icon.IconImageStandard.Companion.KEY_ID
import com.kunzisoft.keepass.icons.IconPack.Companion.NB_ICONS import com.kunzisoft.keepass.icons.IconPack.Companion.NB_ICONS
import java.io.File import java.io.File
import java.util.* import java.util.*
import kotlin.collections.ArrayList
class IconsManager { class IconsManager {
@@ -52,7 +51,7 @@ class IconsManager {
fun buildNewCustomIcon(cacheDirectory: File, fun buildNewCustomIcon(cacheDirectory: File,
key: UUID? = null, key: UUID? = null,
result: (IconImageCustom, BinaryFile?) -> Unit) { result: (IconImageCustom, BinaryData?) -> Unit) {
val keyBinary = customCache.put(cacheDirectory, key) val keyBinary = customCache.put(cacheDirectory, key)
result.invoke(IconImageCustom(keyBinary.keys.first()), keyBinary.binary) result.invoke(IconImageCustom(keyBinary.keys.first()), keyBinary.binary)
} }
@@ -61,8 +60,8 @@ class IconsManager {
return IconImageCustom(iconUuid) return IconImageCustom(iconUuid)
} }
fun isCustomIconBinaryDuplicate(binaryFile: BinaryFile): Boolean { fun isCustomIconBinaryDuplicate(binaryData: BinaryData): Boolean {
return customCache.isBinaryDuplicate(binaryFile) return customCache.isBinaryDuplicate(binaryData)
} }
fun removeCustomIcon(iconUuid: UUID) { fun removeCustomIcon(iconUuid: UUID) {
@@ -75,11 +74,11 @@ class IconsManager {
} }
} }
fun getBinaryForCustomIcon(iconUuid: UUID): BinaryFile? { fun getBinaryForCustomIcon(iconUuid: UUID): BinaryData? {
return customCache[iconUuid] return customCache[iconUuid]
} }
fun doForEachCustomIcon(action: (IconImageCustom, BinaryFile) -> Unit) { fun doForEachCustomIcon(action: (IconImageCustom, BinaryData) -> Unit) {
customCache.doForEachBinary { key, binary -> customCache.doForEachBinary { key, binary ->
action.invoke(IconImageCustom(key), binary) action.invoke(IconImageCustom(key), binary)
} }

View File

@@ -29,7 +29,7 @@ import com.kunzisoft.keepass.database.element.Attachment
import com.kunzisoft.keepass.database.element.Database import com.kunzisoft.keepass.database.element.Database
import com.kunzisoft.keepass.database.element.DateInstant import com.kunzisoft.keepass.database.element.DateInstant
import com.kunzisoft.keepass.database.element.DeletedObject import com.kunzisoft.keepass.database.element.DeletedObject
import com.kunzisoft.keepass.database.element.database.BinaryFile import com.kunzisoft.keepass.database.element.database.BinaryData
import com.kunzisoft.keepass.database.element.database.CompressionAlgorithm import com.kunzisoft.keepass.database.element.database.CompressionAlgorithm
import com.kunzisoft.keepass.database.element.database.DatabaseKDBX import com.kunzisoft.keepass.database.element.database.DatabaseKDBX
import com.kunzisoft.keepass.database.element.database.DatabaseKDBX.Companion.BASE_64_FLAG import com.kunzisoft.keepass.database.element.database.DatabaseKDBX.Companion.BASE_64_FLAG
@@ -78,7 +78,7 @@ class DatabaseInputKDBX(cacheDirectory: File)
private var ctxStringName: String? = null private var ctxStringName: String? = null
private var ctxStringValue: ProtectedString? = null private var ctxStringValue: ProtectedString? = null
private var ctxBinaryName: String? = null private var ctxBinaryName: String? = null
private var ctxBinaryValue: BinaryFile? = null private var ctxBinaryValue: BinaryData? = null
private var ctxATName: String? = null private var ctxATName: String? = null
private var ctxATSeq: String? = null private var ctxATSeq: String? = null
private var entryInHistory = false private var entryInHistory = false
@@ -966,7 +966,7 @@ class DatabaseInputKDBX(cacheDirectory: File)
} }
@Throws(XmlPullParserException::class, IOException::class) @Throws(XmlPullParserException::class, IOException::class)
private fun readBinary(xpp: XmlPullParser): BinaryFile? { private fun readBinary(xpp: XmlPullParser): BinaryData? {
// Reference Id to a binary already present in binary pool // Reference Id to a binary already present in binary pool
val ref = xpp.getAttributeValue(null, DatabaseKDBXXML.AttrRef) val ref = xpp.getAttributeValue(null, DatabaseKDBXXML.AttrRef)
@@ -997,7 +997,7 @@ class DatabaseInputKDBX(cacheDirectory: File)
} }
@Throws(IOException::class, XmlPullParserException::class) @Throws(IOException::class, XmlPullParserException::class)
private fun createBinary(binaryId: Int?, xpp: XmlPullParser): BinaryFile? { private fun createBinary(binaryId: Int?, xpp: XmlPullParser): BinaryData? {
var compressed = false var compressed = false
var protected = true var protected = true

View File

@@ -34,7 +34,7 @@ import androidx.core.graphics.drawable.toBitmap
import androidx.core.widget.ImageViewCompat import androidx.core.widget.ImageViewCompat
import com.kunzisoft.keepass.R import com.kunzisoft.keepass.R
import com.kunzisoft.keepass.database.element.Database import com.kunzisoft.keepass.database.element.Database
import com.kunzisoft.keepass.database.element.database.BinaryFile import com.kunzisoft.keepass.database.element.database.BinaryData
import com.kunzisoft.keepass.database.element.icon.IconImageCustom import com.kunzisoft.keepass.database.element.icon.IconImageCustom
import com.kunzisoft.keepass.database.element.icon.IconImageDraw import com.kunzisoft.keepass.database.element.icon.IconImageDraw
import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.CoroutineScope
@@ -49,7 +49,7 @@ import kotlin.collections.HashMap
* Factory class who build database icons dynamically, can assign an icon of IconPack, or a custom icon to an ImageView with a tint * Factory class who build database icons dynamically, can assign an icon of IconPack, or a custom icon to an ImageView with a tint
*/ */
class IconDrawableFactory(private val retrieveCipherKey : () -> Database.LoadedKey?, class IconDrawableFactory(private val retrieveCipherKey : () -> Database.LoadedKey?,
private val retrieveCustomIconBinary : (iconId: UUID) -> BinaryFile?) { private val retrieveCustomIconBinary : (iconId: UUID) -> BinaryData?) {
/** customIconMap /** customIconMap
* Cache for icon drawable. * Cache for icon drawable.
@@ -85,7 +85,7 @@ class IconDrawableFactory(private val retrieveCipherKey : () -> Database.LoadedK
/** /**
* Build a custom [Drawable] from custom [icon] * Build a custom [Drawable] from custom [icon]
*/ */
private fun getIconDrawable(resources: Resources, icon: IconImageCustom, iconCustomBinary: BinaryFile?): Drawable? { private fun getIconDrawable(resources: Resources, icon: IconImageCustom, iconCustomBinary: BinaryData?): Drawable? {
val patternIcon = PatternIcon(resources) val patternIcon = PatternIcon(resources)
val cipherKey = retrieveCipherKey() val cipherKey = retrieveCipherKey()
if (cipherKey != null) { if (cipherKey != null) {

View File

@@ -22,7 +22,7 @@ package com.kunzisoft.keepass.model
import android.os.Parcel import android.os.Parcel
import android.os.Parcelable import android.os.Parcelable
import com.kunzisoft.keepass.database.element.Attachment import com.kunzisoft.keepass.database.element.Attachment
import com.kunzisoft.keepass.database.element.database.BinaryFile import com.kunzisoft.keepass.database.element.database.BinaryData
import com.kunzisoft.keepass.utils.readEnum import com.kunzisoft.keepass.utils.readEnum
import com.kunzisoft.keepass.utils.writeEnum import com.kunzisoft.keepass.utils.writeEnum
@@ -33,7 +33,7 @@ data class EntryAttachmentState(var attachment: Attachment,
var previewState: AttachmentState = AttachmentState.NULL) : Parcelable { var previewState: AttachmentState = AttachmentState.NULL) : Parcelable {
constructor(parcel: Parcel) : this( constructor(parcel: Parcel) : this(
parcel.readParcelable(Attachment::class.java.classLoader) ?: Attachment("", BinaryFile()), parcel.readParcelable(Attachment::class.java.classLoader) ?: Attachment("", BinaryData()),
parcel.readEnum<StreamDirection>() ?: StreamDirection.DOWNLOAD, parcel.readEnum<StreamDirection>() ?: StreamDirection.DOWNLOAD,
parcel.readEnum<AttachmentState>() ?: AttachmentState.NULL, parcel.readEnum<AttachmentState>() ?: AttachmentState.NULL,
parcel.readInt(), parcel.readInt(),

View File

@@ -346,7 +346,7 @@ class AttachmentFileNotificationService: LockNotificationService() {
StreamDirection.UPLOAD -> { StreamDirection.UPLOAD -> {
BinaryDatabaseManager.uploadToDatabase( BinaryDatabaseManager.uploadToDatabase(
attachmentNotification.uri, attachmentNotification.uri,
attachment.binaryFile, attachment.binaryData,
contentResolver, contentResolver,
{ percent -> { percent ->
publishProgress(percent) publishProgress(percent)
@@ -359,7 +359,7 @@ class AttachmentFileNotificationService: LockNotificationService() {
StreamDirection.DOWNLOAD -> { StreamDirection.DOWNLOAD -> {
BinaryDatabaseManager.downloadFromDatabase( BinaryDatabaseManager.downloadFromDatabase(
attachmentNotification.uri, attachmentNotification.uri,
attachment.binaryFile, attachment.binaryData,
contentResolver, contentResolver,
{ percent -> { percent ->
publishProgress(percent) publishProgress(percent)

View File

@@ -6,7 +6,7 @@ import android.graphics.BitmapFactory
import android.net.Uri import android.net.Uri
import android.util.Log import android.util.Log
import com.kunzisoft.keepass.database.element.Database import com.kunzisoft.keepass.database.element.Database
import com.kunzisoft.keepass.database.element.database.BinaryFile import com.kunzisoft.keepass.database.element.database.BinaryData
import com.kunzisoft.keepass.stream.readAllBytes import com.kunzisoft.keepass.stream.readAllBytes
import com.kunzisoft.keepass.utils.UriUtil import com.kunzisoft.keepass.utils.UriUtil
import kotlinx.coroutines.* import kotlinx.coroutines.*
@@ -22,25 +22,25 @@ import kotlin.math.pow
object BinaryDatabaseManager { object BinaryDatabaseManager {
fun downloadFromDatabase(attachmentToUploadUri: Uri, fun downloadFromDatabase(attachmentToUploadUri: Uri,
binaryFile: BinaryFile, binaryData: BinaryData,
contentResolver: ContentResolver, contentResolver: ContentResolver,
update: ((percent: Int)->Unit)? = null, update: ((percent: Int)->Unit)? = null,
canceled: ()-> Boolean = { false }, canceled: ()-> Boolean = { false },
bufferSize: Int = DEFAULT_BUFFER_SIZE) { bufferSize: Int = DEFAULT_BUFFER_SIZE) {
UriUtil.getUriOutputStream(contentResolver, attachmentToUploadUri)?.use { outputStream -> UriUtil.getUriOutputStream(contentResolver, attachmentToUploadUri)?.use { outputStream ->
downloadFromDatabase(outputStream, binaryFile, update, canceled, bufferSize) downloadFromDatabase(outputStream, binaryData, update, canceled, bufferSize)
} }
} }
private fun downloadFromDatabase(outputStream: OutputStream, private fun downloadFromDatabase(outputStream: OutputStream,
binaryFile: BinaryFile, binaryData: BinaryData,
update: ((percent: Int)->Unit)? = null, update: ((percent: Int)->Unit)? = null,
canceled: ()-> Boolean = { false }, canceled: ()-> Boolean = { false },
bufferSize: Int = DEFAULT_BUFFER_SIZE) { bufferSize: Int = DEFAULT_BUFFER_SIZE) {
val fileSize = binaryFile.getSize() val fileSize = binaryData.getSize()
var dataDownloaded = 0L var dataDownloaded = 0L
Database.getInstance().loadedCipherKey?.let { binaryCipherKey -> Database.getInstance().loadedCipherKey?.let { binaryCipherKey ->
binaryFile.getUnGzipInputDataStream(binaryCipherKey).use { inputStream -> binaryData.getUnGzipInputDataStream(binaryCipherKey).use { inputStream ->
inputStream.readAllBytes(bufferSize, canceled) { buffer -> inputStream.readAllBytes(bufferSize, canceled) { buffer ->
outputStream.write(buffer) outputStream.write(buffer)
dataDownloaded += buffer.size dataDownloaded += buffer.size
@@ -56,26 +56,26 @@ object BinaryDatabaseManager {
} }
fun uploadToDatabase(attachmentFromDownloadUri: Uri, fun uploadToDatabase(attachmentFromDownloadUri: Uri,
binaryFile: BinaryFile, binaryData: BinaryData,
contentResolver: ContentResolver, contentResolver: ContentResolver,
update: ((percent: Int)->Unit)? = null, update: ((percent: Int)->Unit)? = null,
canceled: ()-> Boolean = { false }, canceled: ()-> Boolean = { false },
bufferSize: Int = DEFAULT_BUFFER_SIZE) { bufferSize: Int = DEFAULT_BUFFER_SIZE) {
val fileSize = contentResolver.openFileDescriptor(attachmentFromDownloadUri, "r")?.statSize ?: 0 val fileSize = contentResolver.openFileDescriptor(attachmentFromDownloadUri, "r")?.statSize ?: 0
UriUtil.getUriInputStream(contentResolver, attachmentFromDownloadUri)?.use { inputStream -> UriUtil.getUriInputStream(contentResolver, attachmentFromDownloadUri)?.use { inputStream ->
uploadToDatabase(inputStream, fileSize, binaryFile, update, canceled, bufferSize) uploadToDatabase(inputStream, fileSize, binaryData, update, canceled, bufferSize)
} }
} }
private fun uploadToDatabase(inputStream: InputStream, private fun uploadToDatabase(inputStream: InputStream,
fileSize: Long, fileSize: Long,
binaryFile: BinaryFile, binaryData: BinaryData,
update: ((percent: Int)->Unit)? = null, update: ((percent: Int)->Unit)? = null,
canceled: ()-> Boolean = { false }, canceled: ()-> Boolean = { false },
bufferSize: Int = DEFAULT_BUFFER_SIZE) { bufferSize: Int = DEFAULT_BUFFER_SIZE) {
var dataUploaded = 0L var dataUploaded = 0L
Database.getInstance().loadedCipherKey?.let { binaryCipherKey -> Database.getInstance().loadedCipherKey?.let { binaryCipherKey ->
binaryFile.getGzipOutputDataStream(binaryCipherKey).use { outputStream -> binaryData.getGzipOutputDataStream(binaryCipherKey).use { outputStream ->
inputStream.readAllBytes(bufferSize, canceled) { buffer -> inputStream.readAllBytes(bufferSize, canceled) { buffer ->
outputStream.write(buffer) outputStream.write(buffer)
dataUploaded += buffer.size dataUploaded += buffer.size
@@ -92,9 +92,9 @@ object BinaryDatabaseManager {
fun resizeBitmapAndStoreDataInBinaryFile(contentResolver: ContentResolver, fun resizeBitmapAndStoreDataInBinaryFile(contentResolver: ContentResolver,
bitmapUri: Uri?, bitmapUri: Uri?,
binaryFile: BinaryFile?) { binaryData: BinaryData?) {
try { try {
binaryFile?.let { binaryData?.let {
UriUtil.getUriInputStream(contentResolver, bitmapUri)?.use { inputStream -> UriUtil.getUriInputStream(contentResolver, bitmapUri)?.use { inputStream ->
BitmapFactory.decodeStream(inputStream)?.let { bitmap -> BitmapFactory.decodeStream(inputStream)?.let { bitmap ->
val bitmapResized = bitmap.resize(DEFAULT_ICON_WIDTH) val bitmapResized = bitmap.resize(DEFAULT_ICON_WIDTH)
@@ -105,7 +105,7 @@ object BinaryDatabaseManager {
uploadToDatabase( uploadToDatabase(
byteArrayInputStream, byteArrayInputStream,
bitmapData.size.toLong(), bitmapData.size.toLong(),
binaryFile binaryData
) )
} }
} }
@@ -136,7 +136,7 @@ object BinaryDatabaseManager {
return Bitmap.createScaledBitmap(this, width, height, true) return Bitmap.createScaledBitmap(this, width, height, true)
} }
fun loadBitmap(binaryFile: BinaryFile, fun loadBitmap(binaryData: BinaryData,
binaryCipherKey: Database.LoadedKey?, binaryCipherKey: Database.LoadedKey?,
maxWidth: Int, maxWidth: Int,
actionOnFinish: (Bitmap?) -> Unit) { actionOnFinish: (Bitmap?) -> Unit) {
@@ -145,7 +145,7 @@ object BinaryDatabaseManager {
val asyncResult: Deferred<Bitmap?> = async { val asyncResult: Deferred<Bitmap?> = async {
runCatching { runCatching {
binaryCipherKey?.let { binaryKey -> binaryCipherKey?.let { binaryKey ->
val bitmap: Bitmap? = decodeSampledBitmap(binaryFile, val bitmap: Bitmap? = decodeSampledBitmap(binaryData,
binaryKey, binaryKey,
maxWidth) maxWidth)
bitmap bitmap
@@ -159,14 +159,14 @@ object BinaryDatabaseManager {
} }
} }
private fun decodeSampledBitmap(binaryFile: BinaryFile, private fun decodeSampledBitmap(binaryData: BinaryData,
binaryCipherKey: Database.LoadedKey, binaryCipherKey: Database.LoadedKey,
maxWidth: Int): Bitmap? { maxWidth: Int): Bitmap? {
// First decode with inJustDecodeBounds=true to check dimensions // First decode with inJustDecodeBounds=true to check dimensions
return BitmapFactory.Options().run { return BitmapFactory.Options().run {
try { try {
inJustDecodeBounds = true inJustDecodeBounds = true
binaryFile.getUnGzipInputDataStream(binaryCipherKey).use { binaryData.getUnGzipInputDataStream(binaryCipherKey).use {
BitmapFactory.decodeStream(it, null, this) BitmapFactory.decodeStream(it, null, this)
} }
// Calculate inSampleSize // Calculate inSampleSize
@@ -178,7 +178,7 @@ object BinaryDatabaseManager {
// Decode bitmap with inSampleSize set // Decode bitmap with inSampleSize set
inJustDecodeBounds = false inJustDecodeBounds = false
binaryFile.getUnGzipInputDataStream(binaryCipherKey).use { binaryData.getUnGzipInputDataStream(binaryCipherKey).use {
BitmapFactory.decodeStream(it, null, this) BitmapFactory.decodeStream(it, null, this)
} }
} catch (e: Exception) { } catch (e: Exception) {