Skip to content

Commit

Permalink
fix: auto-close file after reading
Browse files Browse the repository at this point in the history
  • Loading branch information
flogy authored Apr 12, 2024
1 parent 57669c1 commit acc265c
Showing 1 changed file with 11 additions and 7 deletions.
18 changes: 11 additions & 7 deletions android/src/main/java/com/alpha0010/fs/FileAccessModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -404,16 +404,20 @@ class FileAccessModule internal constructor(context: ReactApplicationContext) :
override fun readFileChunk(path: String, offset: Double, length: Double, encoding: String, promise: Promise) {
ioScope.launch {
try {
val inputStream = openForReading(path);
inputStream.skip(offset.toLong())
val data = ByteArray(length.toInt())
inputStream.read(data);
val data = openForReading(path).use { inputStream ->
inputStream.skip(offset.toLong())
val data = ByteArray(length.toInt())
inputStream.read(data)
data
}

if (encoding == "base64") {
promise.resolve(Base64.encodeToString(data, Base64.NO_WRAP))
val result = if (encoding == "base64") {
Base64.encodeToString(data, Base64.NO_WRAP)
} else {
promise.resolve(data.decodeToString())
data.decodeToString()
}

promise.resolve(result)
} catch (e: Throwable) {
promise.reject(e)
}
Expand Down

0 comments on commit acc265c

Please sign in to comment.