diff --git a/common/src/commonMain/kotlin/tk/zwander/common/tools/CryptUtils.kt b/common/src/commonMain/kotlin/tk/zwander/common/tools/CryptUtils.kt index 943ec3c09..748d4af6b 100644 --- a/common/src/commonMain/kotlin/tk/zwander/common/tools/CryptUtils.kt +++ b/common/src/commonMain/kotlin/tk/zwander/common/tools/CryptUtils.kt @@ -200,19 +200,19 @@ object CryptUtils { enc: Source?, encSize: Long, expected: Long, - progressCallback: suspend (current: Long, max: Long, bps: Long) -> Unit + progressCallback: suspend (current: Long, max: Long, bps: Long) -> Unit, ): Boolean { if (enc == null) { return false } + val buffer = ByteArray(DEFAULT_CHUNK_SIZE) var crcVal = CRC32.initialValue trackOperationProgress( size = encSize, progressCallback = progressCallback, operation = { - val buffer = ByteArray(DEFAULT_CHUNK_SIZE) val len = enc.readAtMostTo(buffer, 0, buffer.size) if (len > 0) { diff --git a/common/src/commonMain/kotlin/tk/zwander/common/tools/delegates/Downloader.kt b/common/src/commonMain/kotlin/tk/zwander/common/tools/delegates/Downloader.kt index 8a59ea659..e4eca4fe5 100644 --- a/common/src/commonMain/kotlin/tk/zwander/common/tools/delegates/Downloader.kt +++ b/common/src/commonMain/kotlin/tk/zwander/common/tools/delegates/Downloader.kt @@ -154,7 +154,7 @@ object Downloader { model.statusText.value = MR.strings.checkingCRC() val result = CryptUtils.checkCrc32( encFile.openInputStream() ?: return, - size, + encFile.getLength(), crc32, ) { current, max, bps -> model.progress.value = current to max @@ -164,7 +164,7 @@ object Downloader { Event.Download.Progress( MR.strings.checkingCRC(), current, - max + max, ) ) } diff --git a/common/src/commonMain/kotlin/tk/zwander/common/util/Averager.kt b/common/src/commonMain/kotlin/tk/zwander/common/util/Averager.kt index 066c486de..dcaa39611 100644 --- a/common/src/commonMain/kotlin/tk/zwander/common/util/Averager.kt +++ b/common/src/commonMain/kotlin/tk/zwander/common/util/Averager.kt @@ -21,6 +21,12 @@ class Averager(initialCapacity: Int = 1000, private val thresholdNanos: Long = 1 } } + suspend fun close() { + mutex.withLock { + chunk.clear() + } + } + private fun unsafeUpdate(durationNano: Long, read: Long) { val currentTimeNano = currentTimeNano() chunk.add(ChunkData(durationNano, read, currentTimeNano)) diff --git a/common/src/commonMain/kotlin/tk/zwander/common/util/ProgressUtils.kt b/common/src/commonMain/kotlin/tk/zwander/common/util/ProgressUtils.kt index 99f66b443..cb50d81df 100644 --- a/common/src/commonMain/kotlin/tk/zwander/common/util/ProgressUtils.kt +++ b/common/src/commonMain/kotlin/tk/zwander/common/util/ProgressUtils.kt @@ -117,5 +117,6 @@ suspend fun trackOperationProgress( } finished.value = true + averager.close() } }