Optimize I/O code using Okio (#4366)

This pull request takes advantage of the Okio library to simplify, fix
or improve performance of some I/O related code in Tusky.

- Return early or throw `FileNotFoundException` early in case
`contentResolver.openInputStream()` returns `null` instead of throwing
`NullPointerException` later. Change the signature of
`Closeable.closeQuietly()` to only accept a non-null `Closeable`.
- Reimplement `Uri.copyToFile()` using Okio. This takes advantage of the
built-in high-performance buffers of the library so a buffer doesn't
need to be allocated or managed manually. The new implementation also
makes sure that the input and output streams are always closed, as the
original code could in some cases return without properly closing a
stream.
- Reimplement `ProgressRequestBody` as `Uri.asRequestBody()` (adding to
the existing extension functions available in the Okio library to create
a `RequestBody`). The new implementation uses Okio's `Buffer` instead of
a manually managed byte array, which allows to avoid copying bytes from
one buffer to the next. The max number of bytes read at once was
increased from 2K to 8K to improve performance. Avoid division by zero
in case `contentLength` is `0`. Finally, this implementation now takes a
`Uri` as input instead of an `InputStream`, because a `RequestBody` must
be replayable in case Okio retries the request, and an `InputStream` can
only be used once.
This commit is contained in:
Christophe Beyls 2024-04-10 21:52:55 +02:00 committed by GitHub
commit 65af26993b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 80 additions and 102 deletions

View file

@ -15,52 +15,33 @@
package com.keylesspalace.tusky.util
import android.annotation.SuppressLint
import android.content.ContentResolver
import android.net.Uri
import java.io.Closeable
import java.io.File
import java.io.FileNotFoundException
import java.io.FileOutputStream
import java.io.IOException
import java.io.InputStream
import okio.buffer
import okio.sink
import okio.source
private const val DEFAULT_BLOCKSIZE = 16384
fun Closeable?.closeQuietly() {
fun Closeable.closeQuietly() {
try {
this?.close()
close()
} catch (e: IOException) {
// intentionally unhandled
}
}
@SuppressLint("Recycle") // The linter can't tell that the stream gets closed by a helper method
fun Uri.copyToFile(contentResolver: ContentResolver, file: File): Boolean {
val from: InputStream?
val to: FileOutputStream
try {
from = contentResolver.openInputStream(this)
to = FileOutputStream(file)
} catch (e: FileNotFoundException) {
return false
}
if (from == null) return false
val chunk = ByteArray(DEFAULT_BLOCKSIZE)
try {
while (true) {
val bytes = from.read(chunk, 0, chunk.size)
if (bytes < 0) break
to.write(chunk, 0, bytes)
return try {
val inputStream = contentResolver.openInputStream(this) ?: return false
inputStream.source().use { source ->
file.sink().buffer().use { bufferedSink ->
bufferedSink.writeAll(source)
}
}
true
} catch (e: IOException) {
return false
false
}
from.closeQuietly()
to.closeQuietly()
return true
}

View file

@ -69,7 +69,7 @@ fun getMediaSize(contentResolver: ContentResolver, uri: Uri?): Long {
@Throws(FileNotFoundException::class)
fun getImageSquarePixels(contentResolver: ContentResolver, uri: Uri): Long {
val input = contentResolver.openInputStream(uri)
val input = contentResolver.openInputStream(uri) ?: throw FileNotFoundException("Unavailable ContentProvider")
val options = BitmapFactory.Options()
options.inJustDecodeBounds = true