-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
converted ProgressResponseBody to Kt
- Loading branch information
Showing
3 changed files
with
45 additions
and
62 deletions.
There are no files selected for viewing
This file contains 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
61 changes: 0 additions & 61 deletions
61
usecases/src/main/java/com/zeyad/usecases/network/ProgressResponseBody.java
This file was deleted.
Oops, something went wrong.
44 changes: 44 additions & 0 deletions
44
usecases/src/main/java/com/zeyad/usecases/network/ProgressResponseBody.kt
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package com.zeyad.usecases.network | ||
|
||
import okhttp3.MediaType | ||
import okhttp3.ResponseBody | ||
import okio.* | ||
import java.io.IOException | ||
|
||
/** | ||
* @author by ZIaDo on 7/11/17. | ||
*/ | ||
internal class ProgressResponseBody(private val responseBody: ResponseBody, private val progressListener: ProgressListener) : ResponseBody() { | ||
private lateinit var bufferedSource: BufferedSource | ||
|
||
override fun contentType(): MediaType? { | ||
return responseBody.contentType() | ||
} | ||
|
||
override fun contentLength(): Long { | ||
return responseBody.contentLength() | ||
} | ||
|
||
override fun source(): BufferedSource { | ||
if (!::bufferedSource.isInitialized) { | ||
bufferedSource = Okio.buffer(source(responseBody.source())) | ||
} | ||
return bufferedSource | ||
} | ||
|
||
private fun source(source: Source): Source { | ||
return object : ForwardingSource(source) { | ||
var totalBytesRead = 0L | ||
|
||
@Throws(IOException::class) | ||
override fun read(sink: Buffer, byteCount: Long): Long { | ||
val bytesRead = super.read(sink, byteCount) | ||
// read() returns the number of bytes read, or -1 if this source is exhausted. | ||
totalBytesRead += if (bytesRead != -1L) bytesRead else 0 | ||
progressListener.update(totalBytesRead, responseBody.contentLength(), | ||
bytesRead == -1L) | ||
return bytesRead | ||
} | ||
} | ||
} | ||
} |