-
-
Notifications
You must be signed in to change notification settings - Fork 390
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Optimize I/O code using Okio #4366
Conversation
…e buffer segments and properly close input and output in all cases
…rs, increasing the chunk size and correctly handle Okio retries by allowing the source stream to be reopened multiple times.
…ntentProvider is unavailable during Uri RequestBody read
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And we can replace java.nio.ByteBuffer
and java.nio.charset.Charset
.
Tusky/app/src/main/java/com/keylesspalace/tusky/util/CompositeWithOpaqueBackground.kt
Line 64 in db27186
messageDigest.update(ByteBuffer.allocate(4).putInt(backgroundColor.hashCode()).array()) |
Tusky/app/src/main/java/com/keylesspalace/tusky/util/CompositeWithOpaqueBackground.kt
Line 114 in db27186
private val ID_BYTES = ID.toByteArray(Charset.forName("UTF-8")) |
fun onProgressUpdate(percentage: Int) | ||
} | ||
|
||
fun Uri.asRequestBody(contentResolver: ContentResolver, contentType: MediaType? = null, contentLength: Long = -1L, uploadListener: UploadCallback? = null): RequestBody { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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 { |
Sorry I didn't have time to apply your suggested changes today. Do you mean replacing |
Yeah, just wanna replace all nio usages. |
`ImageDownsizer.downsizeImage()`: - Remove the return value, it was ignored - Throw `FileNotFoundException` when `openInputStream` returns null `ImageDownsizer.getImageOrientation()`: - Throw `FileNotFoundException` when `openInputStream` returns null `MediaUploader.prepareMedia()`: - Copy URI contents using Okio buffers / source / sink `UriExtensions`: - Rename from `IOUtils` - Implement `Uri.copyToFile()` using Okio buffers / source / sink - Replace `ProgressRequestBody()` with `Uri.asRequestBody()` using Okio buffers / source / sink `DraftHelper.copyToFolder()` - Use Okio buffers / source / sink `CompositeWithOpaqueBackground` - Use constants `SIZE_BYTES` and `CHARSET` instead of magic values - Use `Objects.hash` when hashing multiple objects Based on work by Christophe Beyls in - tuskyapp/Tusky#4366 - tuskyapp/Tusky#4372
`ImageDownsizer.downsizeImage()`: - Remove the return value, it was ignored - Throw `FileNotFoundException` when `openInputStream` returns null `ImageDownsizer.getImageOrientation()`: - Throw `FileNotFoundException` when `openInputStream` returns null `MediaUploader.prepareMedia()`: - Copy URI contents using Okio buffers / source / sink `UriExtensions`: - Rename from `IOUtils` - Implement `Uri.copyToFile()` using Okio buffers / source / sink - Replace `ProgressRequestBody()` with `Uri.asRequestBody()` using Okio buffers / source / sink `DraftHelper.copyToFolder()` - Use Okio buffers / source / sink `CompositeWithOpaqueBackground` - Use constants `SIZE_BYTES` and `CHARSET` instead of magic values - Use `Objects.hash` when hashing multiple objects Based on work by Christophe Beyls in - tuskyapp/Tusky#4366 - tuskyapp/Tusky#4372
This pull request takes advantage of the Okio library to simplify, fix or improve performance of some I/O related code in Tusky.
FileNotFoundException
early in casecontentResolver.openInputStream()
returnsnull
instead of throwingNullPointerException
later. Change the signature ofCloseable.closeQuietly()
to only accept a non-nullCloseable
.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.ProgressRequestBody
asUri.asRequestBody()
(adding to the existing extension functions available in the Okio library to create aRequestBody
). The new implementation uses Okio'sBuffer
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 casecontentLength
is0
. Finally, this implementation now takes aUri
as input instead of anInputStream
, because aRequestBody
must be replayable in case Okio retries the request, and anInputStream
can only be used once.