diff --git a/app/src/androidTest/java/com/kunzisoft/keepass/tests/stream/BinaryFileTest.kt b/app/src/androidTest/java/com/kunzisoft/keepass/tests/stream/BinaryFileTest.kt index 858044e63..c854a51bc 100644 --- a/app/src/androidTest/java/com/kunzisoft/keepass/tests/stream/BinaryFileTest.kt +++ b/app/src/androidTest/java/com/kunzisoft/keepass/tests/stream/BinaryFileTest.kt @@ -41,7 +41,7 @@ class BinaryFileTest { val binaryB = BinaryFile(fileB) saveBinary(TEST_TEXT_ASSET, binaryA) saveBinary(TEST_TEXT_ASSET, binaryB) - assertEquals("Save text binary length failed.", binaryA.length, binaryB.length) + assertEquals("Save text binary length failed.", binaryA.getSize(), binaryB.getSize()) assertEquals("Save text binary MD5 failed.", binaryA.binaryHash(), binaryB.binaryHash()) } @@ -51,7 +51,7 @@ class BinaryFileTest { val binaryB = BinaryFile(fileB) saveBinary(TEST_IMAGE_ASSET, binaryA) saveBinary(TEST_IMAGE_ASSET, binaryB) - assertEquals("Save image binary length failed.", binaryA.length, binaryB.length) + assertEquals("Save image binary length failed.", binaryA.getSize(), binaryB.getSize()) assertEquals("Save image binary failed.", binaryA.binaryHash(), binaryB.binaryHash()) } @@ -65,10 +65,10 @@ class BinaryFileTest { saveBinary(TEST_TEXT_ASSET, binaryC) binaryA.compress(loadedKey) binaryB.compress(loadedKey) - assertEquals("Compress text length failed.", binaryA.length, binaryB.length) + assertEquals("Compress text length failed.", binaryA.getSize(), binaryB.getSize()) assertEquals("Compress text MD5 failed.", binaryA.binaryHash(), binaryB.binaryHash()) binaryB.decompress(loadedKey) - assertEquals("Decompress text length failed.", binaryB.length, binaryC.length) + assertEquals("Decompress text length failed.", binaryB.getSize(), binaryC.getSize()) assertEquals("Decompress text MD5 failed.", binaryB.binaryHash(), binaryC.binaryHash()) } @@ -82,11 +82,11 @@ class BinaryFileTest { saveBinary(TEST_IMAGE_ASSET, binaryC) binaryA.compress(loadedKey) binaryB.compress(loadedKey) - assertEquals("Compress image length failed.", binaryA.length, binaryA.length) + assertEquals("Compress image length failed.", binaryA.getSize(), binaryA.getSize()) assertEquals("Compress image failed.", binaryA.binaryHash(), binaryA.binaryHash()) binaryB = BinaryFile(fileB, true) binaryB.decompress(loadedKey) - assertEquals("Decompress image length failed.", binaryB.length, binaryC.length) + assertEquals("Decompress image length failed.", binaryB.getSize(), binaryC.getSize()) assertEquals("Decompress image failed.", binaryB.binaryHash(), binaryC.binaryHash()) } diff --git a/app/src/main/java/com/kunzisoft/keepass/activities/IconPickerActivity.kt b/app/src/main/java/com/kunzisoft/keepass/activities/IconPickerActivity.kt index a614dfc4d..bd5ef9a21 100644 --- a/app/src/main/java/com/kunzisoft/keepass/activities/IconPickerActivity.kt +++ b/app/src/main/java/com/kunzisoft/keepass/activities/IconPickerActivity.kt @@ -237,7 +237,7 @@ class IconPickerActivity : LockingActivity() { when { binary == null -> { } - binary.length <= 0 -> { + binary.getSize() <= 0 -> { } mDatabase?.isCustomIconBinaryDuplicate(binary) == true -> { iconCustomState.errorStringId = R.string.error_duplicate_file diff --git a/app/src/main/java/com/kunzisoft/keepass/activities/ImageViewerActivity.kt b/app/src/main/java/com/kunzisoft/keepass/activities/ImageViewerActivity.kt index fdd8fcf53..ec4906b71 100644 --- a/app/src/main/java/com/kunzisoft/keepass/activities/ImageViewerActivity.kt +++ b/app/src/main/java/com/kunzisoft/keepass/activities/ImageViewerActivity.kt @@ -64,7 +64,7 @@ class ImageViewerActivity : LockingActivity() { intent.getParcelableExtra(IMAGE_ATTACHMENT_TAG)?.let { attachment -> supportActionBar?.title = attachment.name - supportActionBar?.subtitle = Formatter.formatFileSize(this, attachment.binaryFile.length) + supportActionBar?.subtitle = Formatter.formatFileSize(this, attachment.binaryFile.getSize()) BinaryDatabaseManager.loadBitmap( attachment.binaryFile, diff --git a/app/src/main/java/com/kunzisoft/keepass/adapters/EntryAttachmentsItemsAdapter.kt b/app/src/main/java/com/kunzisoft/keepass/adapters/EntryAttachmentsItemsAdapter.kt index d4edc2a0b..a9fbb4d71 100644 --- a/app/src/main/java/com/kunzisoft/keepass/adapters/EntryAttachmentsItemsAdapter.kt +++ b/app/src/main/java/com/kunzisoft/keepass/adapters/EntryAttachmentsItemsAdapter.kt @@ -124,7 +124,7 @@ class EntryAttachmentsItemsAdapter(context: Context) holder.binaryFileTitle.setTextColor(mTitleColor) } holder.binaryFileSize.text = Formatter.formatFileSize(context, - entryAttachmentState.attachment.binaryFile.length) + entryAttachmentState.attachment.binaryFile.getSize()) holder.binaryFileCompression.apply { if (entryAttachmentState.attachment.binaryFile.isCompressed) { text = CompressionAlgorithm.GZip.getName(context.resources) diff --git a/app/src/main/java/com/kunzisoft/keepass/database/element/database/BinaryFile.kt b/app/src/main/java/com/kunzisoft/keepass/database/element/database/BinaryFile.kt index 90ef7e632..c83a78acd 100644 --- a/app/src/main/java/com/kunzisoft/keepass/database/element/database/BinaryFile.kt +++ b/app/src/main/java/com/kunzisoft/keepass/database/element/database/BinaryFile.kt @@ -39,9 +39,8 @@ import javax.crypto.spec.IvParameterSpec class BinaryFile : Parcelable { - private var dataFile: File? = null - var length: Long = 0 - private set + private var mDataFile: File? = null + private var mLength: Long = 0 private var mBinaryHash = 0 var isCompressed: Boolean = false private set @@ -58,17 +57,17 @@ class BinaryFile : Parcelable { constructor() constructor(dataFile: File, compressed: Boolean = false, protected: Boolean = false) { - this.dataFile = dataFile - this.length = 0 + this.mDataFile = dataFile + this.mLength = 0 this.isCompressed = compressed this.isProtected = protected } private constructor(parcel: Parcel) { parcel.readString()?.let { - dataFile = File(it) + mDataFile = File(it) } - length = parcel.readLong() + mLength = parcel.readLong() isCompressed = parcel.readByte().toInt() != 0 isProtected = parcel.readByte().toInt() != 0 isCorrupted = parcel.readByte().toInt() != 0 @@ -76,12 +75,12 @@ class BinaryFile : Parcelable { @Throws(IOException::class) fun getInputDataStream(cipherKey: Database.LoadedKey): InputStream { - return buildInputStream(dataFile, cipherKey) + return buildInputStream(mDataFile, cipherKey) } @Throws(IOException::class) fun getOutputDataStream(cipherKey: Database.LoadedKey): OutputStream { - return buildOutputStream(dataFile, cipherKey) + return buildOutputStream(mDataFile, cipherKey) } @Throws(IOException::class) @@ -126,7 +125,7 @@ class BinaryFile : Parcelable { @Throws(IOException::class) fun compress(cipherKey: Database.LoadedKey) { - dataFile?.let { concreteDataFile -> + mDataFile?.let { concreteDataFile -> // To compress, create a new binary with file if (!isCompressed) { // Encrypt the new gzipped temp file @@ -151,7 +150,7 @@ class BinaryFile : Parcelable { @Throws(IOException::class) fun decompress(cipherKey: Database.LoadedKey) { - dataFile?.let { concreteDataFile -> + mDataFile?.let { concreteDataFile -> if (isCompressed) { // Encrypt the new ungzipped temp file val fileBinaryDecompress = File(concreteDataFile.parent, concreteDataFile.name + "_temp") @@ -175,12 +174,16 @@ class BinaryFile : Parcelable { @Throws(IOException::class) fun clear() { - if (dataFile != null && !dataFile!!.delete()) - throw IOException("Unable to delete temp file " + dataFile!!.absolutePath) + if (mDataFile != null && !mDataFile!!.delete()) + throw IOException("Unable to delete temp file " + mDataFile!!.absolutePath) } fun dataExists(): Boolean { - return dataFile != null && length > 0 + return mDataFile != null && mLength > 0 + } + + fun getSize(): Long { + return mLength } /** @@ -200,7 +203,7 @@ class BinaryFile : Parcelable { return false var sameData = false - if (dataFile != null && dataFile == other.dataFile) + if (mDataFile != null && mDataFile == other.mDataFile) sameData = true return isCompressed == other.isCompressed @@ -215,13 +218,13 @@ class BinaryFile : Parcelable { result = 31 * result + if (isCompressed) 1 else 0 result = 31 * result + if (isProtected) 1 else 0 result = 31 * result + if (isCorrupted) 1 else 0 - result = 31 * result + dataFile!!.hashCode() - result = 31 * result + length.hashCode() + result = 31 * result + mDataFile!!.hashCode() + result = 31 * result + mLength.hashCode() return result } override fun toString(): String { - return dataFile.toString() + return mDataFile.toString() } override fun describeContents(): Int { @@ -229,8 +232,8 @@ class BinaryFile : Parcelable { } override fun writeToParcel(dest: Parcel, flags: Int) { - dest.writeString(dataFile?.absolutePath) - dest.writeLong(length) + dest.writeString(mDataFile?.absolutePath) + dest.writeLong(mLength) dest.writeByte((if (isCompressed) 1 else 0).toByte()) dest.writeByte((if (isProtected) 1 else 0).toByte()) dest.writeByte((if (isCorrupted) 1 else 0).toByte()) @@ -243,14 +246,14 @@ class BinaryFile : Parcelable { private val mMessageDigest: MessageDigest init { - length = 0 + mLength = 0 mMessageDigest = MessageDigest.getInstance("MD5") mBinaryHash = 0 } override fun beforeWrite(n: Int) { super.beforeWrite(n) - length = byteCount + mLength = byteCount } override fun write(idx: Int) { @@ -270,7 +273,7 @@ class BinaryFile : Parcelable { override fun close() { super.close() - length = byteCount + mLength = byteCount val bytes = mMessageDigest.digest() mBinaryHash = ByteBuffer.wrap(bytes).int } diff --git a/app/src/main/java/com/kunzisoft/keepass/database/element/database/BinaryPool.kt b/app/src/main/java/com/kunzisoft/keepass/database/element/database/BinaryPool.kt index 6785cfa0b..1ce76e3df 100644 --- a/app/src/main/java/com/kunzisoft/keepass/database/element/database/BinaryPool.kt +++ b/app/src/main/java/com/kunzisoft/keepass/database/element/database/BinaryPool.kt @@ -124,7 +124,7 @@ abstract class BinaryPool { fun isBinaryDuplicate(binaryFile: BinaryFile?): Boolean { try { binaryFile?.let { - if (it.length > 0) { + if (it.getSize() > 0) { val searchBinaryMD5 = it.binaryHash() var i = 0 for ((_, binary) in pool) { @@ -168,7 +168,7 @@ abstract class BinaryPool { // Don't deduplicate val existentBinary = try { - if (binary.length > 0) { + if (binary.getSize() > 0) { keyBinaryList.find { val hash0 = it.binary.binaryHash() val hash1 = binary.binaryHash() diff --git a/app/src/main/java/com/kunzisoft/keepass/database/element/entry/EntryKDBX.kt b/app/src/main/java/com/kunzisoft/keepass/database/element/entry/EntryKDBX.kt index 351561d81..4d981380e 100644 --- a/app/src/main/java/com/kunzisoft/keepass/database/element/entry/EntryKDBX.kt +++ b/app/src/main/java/com/kunzisoft/keepass/database/element/entry/EntryKDBX.kt @@ -297,7 +297,7 @@ class EntryKDBX : EntryVersioned, NodeKDBXInte var size = 0L for ((label, poolId) in binaries) { size += label.length.toLong() - size += attachmentPool[poolId]?.length ?: 0 + size += attachmentPool[poolId]?.getSize() ?: 0 } return size } diff --git a/app/src/main/java/com/kunzisoft/keepass/database/file/output/DatabaseOutputKDBX.kt b/app/src/main/java/com/kunzisoft/keepass/database/file/output/DatabaseOutputKDBX.kt index 55fdb3284..77e5b9ccf 100644 --- a/app/src/main/java/com/kunzisoft/keepass/database/file/output/DatabaseOutputKDBX.kt +++ b/app/src/main/java/com/kunzisoft/keepass/database/file/output/DatabaseOutputKDBX.kt @@ -143,7 +143,7 @@ class DatabaseOutputKDBX(private val mDatabaseKDBX: DatabaseKDBX, // Write type binary dataOutputStream.writeByte(DatabaseHeaderKDBX.PwDbInnerHeaderV4Fields.Binary) // Write size - dataOutputStream.writeUInt(UnsignedInt.fromKotlinLong(binary.length + 1)) + dataOutputStream.writeUInt(UnsignedInt.fromKotlinLong(binary.getSize() + 1)) // Write protected flag var flag = DatabaseHeaderKDBX.KdbxBinaryFlags.None if (binary.isProtected) { @@ -500,7 +500,7 @@ class DatabaseOutputKDBX(private val mDatabaseKDBX: DatabaseKDBX, mDatabaseKDBX.binaryPool.doForEachOrderedBinaryWithoutDuplication { index, binary -> xml.startTag(null, DatabaseKDBXXML.ElemBinary) xml.attribute(null, DatabaseKDBXXML.AttrId, index.toString()) - if (binary.length > 0) { + if (binary.getSize() > 0) { if (binary.isCompressed) { xml.attribute(null, DatabaseKDBXXML.AttrCompressed, DatabaseKDBXXML.ValTrue) } diff --git a/app/src/main/java/com/kunzisoft/keepass/database/file/output/EntryOutputKDB.kt b/app/src/main/java/com/kunzisoft/keepass/database/file/output/EntryOutputKDB.kt index 6639c7c1f..a18aa8201 100644 --- a/app/src/main/java/com/kunzisoft/keepass/database/file/output/EntryOutputKDB.kt +++ b/app/src/main/java/com/kunzisoft/keepass/database/file/output/EntryOutputKDB.kt @@ -97,7 +97,7 @@ class EntryOutputKDB(private val mEntry: EntryKDB, mCipherKey?.let { cipherKey -> mOutputStream.write(BINARY_DATA_FIELD_TYPE) val binaryData = mEntry.binaryData - val binaryDataLength = binaryData?.length ?: 0L + val binaryDataLength = binaryData?.getSize() ?: 0L // Write data length mOutputStream.write(uIntTo4Bytes(UnsignedInt.fromKotlinLong(binaryDataLength))) // Write data diff --git a/app/src/main/java/com/kunzisoft/keepass/tasks/BinaryDatabaseManager.kt b/app/src/main/java/com/kunzisoft/keepass/tasks/BinaryDatabaseManager.kt index 3c550f333..3395ddf57 100644 --- a/app/src/main/java/com/kunzisoft/keepass/tasks/BinaryDatabaseManager.kt +++ b/app/src/main/java/com/kunzisoft/keepass/tasks/BinaryDatabaseManager.kt @@ -37,7 +37,7 @@ object BinaryDatabaseManager { update: ((percent: Int)->Unit)? = null, canceled: ()-> Boolean = { false }, bufferSize: Int = DEFAULT_BUFFER_SIZE) { - val fileSize = binaryFile.length + val fileSize = binaryFile.getSize() var dataDownloaded = 0L Database.getInstance().loadedCipherKey?.let { binaryCipherKey -> binaryFile.getUnGzipInputDataStream(binaryCipherKey).use { inputStream ->