Created
February 25, 2020 20:18
-
-
Save lordcodes/9175a8a0a76c553af38c58b13f31b7fe to your computer and use it in GitHub Desktop.
Code for the article: "Uploading a file with progress in Kotlin"
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| typealias CountingRequestListener = (bytesWritten: Long, contentLength: Long) -> Unit | |
| class CountingSink( | |
| sink: Sink, | |
| private val requestBody: RequestBody, | |
| private val onProgressUpdate: CountingRequestListener | |
| ) : ForwardingSink(sink) { | |
| private var bytesWritten = 0L | |
| override fun write(source: Buffer, byteCount: Long) { | |
| super.write(source, byteCount) | |
| bytesWritten += byteCount | |
| onProgressUpdate(bytesWritten, requestBody.contentLength()) | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment