Add safeguard to prevent ArchiveInputStream from being closed twice (#967)

* fix: Add safeguard to prevent ArchiveInputStream from being closed twice

* detekt

* lint: Make detekt happy

---------

Co-authored-by: AntsyLich <59261191+AntsyLich@users.noreply.github.com>
This commit is contained in:
Ahmad Ansori Palembani 2024-06-30 20:57:29 +07:00 committed by GitHub
parent c0f9de88e7
commit e620665dda
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -5,8 +5,14 @@ import me.zhanghai.android.libarchive.ArchiveEntry
import me.zhanghai.android.libarchive.ArchiveException
import java.io.InputStream
import java.nio.ByteBuffer
import kotlin.concurrent.Volatile
class ArchiveInputStream(buffer: Long, size: Long) : InputStream() {
private val lock = Any()
@Volatile
private var isClosed = false
private val archive = Archive.readNew()
init {
@ -41,6 +47,11 @@ class ArchiveInputStream(buffer: Long, size: Long) : InputStream() {
}
override fun close() {
synchronized(lock) {
if (isClosed) return
isClosed = true
}
Archive.readFree(archive)
}