Skip to content

Instantly share code, notes, and snippets.

@lordcodes
Created February 25, 2020 20:18
Show Gist options
  • Save lordcodes/9175a8a0a76c553af38c58b13f31b7fe to your computer and use it in GitHub Desktop.
Save lordcodes/9175a8a0a76c553af38c58b13f31b7fe to your computer and use it in GitHub Desktop.
Code for the article: "Uploading a file with progress in Kotlin"
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