Optimize I/O code using Okio - part 2 (#4372)
- Read license resource using Okio inside a coroutine (instead of the main thread) in `LicenseActivity` - Use Okio and its buffer system to copy ContentProvider streams and files to a temporary file in `MediaUploader.prepareMedia()` - Properly close the input file after copying it to a temporary file in `MediaUploader.prepareMedia()` - Properly close sink in case of null body source during file copy in `Uri.copyToFolder()` in `DraftHelper.kt` - Add comment explaining the current value of `DEFAULT_CHUNK_SIZE` in `UriRequestBody.kt` and indent the file properly - Replace hardcoded `Charset` and `Int` byte size with the proper constants, and align the `hashCode()` implementation with other `BitmapTransformation` implementations in `CompositeWithOpaqueBackground` - Properly close `InputStream` in case of error during Bitmap size decoding in `getImageSquarePixels()` - return `Int` instead of `Long` in `getImageSquarePixels()`, since the current code simply converts the `Int` result to a `Long` _after_ multiplication and not before (and `Int.MAX_VALUE` is already way above the maximum number of pixels a decoded Bitmap could return) - Simplify `getImageOrientation()` - Add explicit dependency to the Okio library and upgrade it to its latest version.
This commit is contained in:
parent
2504f42f7b
commit
f69cae2315
8 changed files with 77 additions and 76 deletions
|
|
@ -1,4 +1,5 @@
|
|||
/* Copyright 2024 Tusky Contributors
|
||||
/*
|
||||
* Copyright 2024 Tusky Contributors
|
||||
*
|
||||
* This file is a part of Tusky.
|
||||
*
|
||||
|
|
@ -11,7 +12,8 @@
|
|||
* Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along with Tusky; if not,
|
||||
* see <http://www.gnu.org/licenses>. */
|
||||
* see <http://www.gnu.org/licenses>.
|
||||
*/
|
||||
package com.keylesspalace.tusky.network
|
||||
|
||||
import android.content.ContentResolver
|
||||
|
|
@ -23,13 +25,19 @@ import okio.Buffer
|
|||
import okio.BufferedSink
|
||||
import okio.source
|
||||
|
||||
// Align with Okio Segment size for better performance
|
||||
private const val DEFAULT_CHUNK_SIZE = 8192L
|
||||
|
||||
fun interface UploadCallback {
|
||||
fun onProgressUpdate(percentage: Int)
|
||||
}
|
||||
|
||||
fun Uri.asRequestBody(contentResolver: ContentResolver, contentType: MediaType? = null, contentLength: Long = -1L, uploadListener: UploadCallback? = null): RequestBody {
|
||||
fun Uri.asRequestBody(
|
||||
contentResolver: ContentResolver,
|
||||
contentType: MediaType? = null,
|
||||
contentLength: Long = -1L,
|
||||
uploadListener: UploadCallback? = null
|
||||
): RequestBody {
|
||||
return object : RequestBody() {
|
||||
override fun contentType(): MediaType? = contentType
|
||||
|
||||
|
|
@ -38,7 +46,8 @@ fun Uri.asRequestBody(contentResolver: ContentResolver, contentType: MediaType?
|
|||
override fun writeTo(sink: BufferedSink) {
|
||||
val buffer = Buffer()
|
||||
var uploaded: Long = 0
|
||||
val inputStream = contentResolver.openInputStream(this@asRequestBody) ?: throw FileNotFoundException("Unavailable ContentProvider")
|
||||
val inputStream = contentResolver.openInputStream(this@asRequestBody)
|
||||
?: throw FileNotFoundException("Unavailable ContentProvider")
|
||||
|
||||
inputStream.source().use { source ->
|
||||
while (true) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue