Replace BinaryFile length method

This commit is contained in:
J-Jamet committed 2021-03-18 11:40:01 +01:00
1 parent 428c2818a5
commit 621415fe51
10 files changed
+42 -39

No files matched your search

@@ -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
@@ -64,7 +64,7 @@ class ImageViewerActivity : LockingActivity() {
intent.getParcelableExtra<Attachment>(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,
@@ -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)
@@ -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
}
@@ -124,7 +124,7 @@ abstract class BinaryPool<T> {
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<T> {
// 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()
@@ -297,7 +297,7 @@ class EntryKDBX : EntryVersioned<UUID, UUID, GroupKDBX, EntryKDBX>, 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
}
@@ -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)
}
@@ -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
@@ -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 ->