Skip to content

Commit

Permalink
refactor(KtorDownloader): reuse buffers
Browse files Browse the repository at this point in the history
Reduce memory usage caused by new buffers for every body read
  • Loading branch information
rushiiMachine committed Aug 2, 2024
1 parent 9759fbd commit 57ade5a
Showing 1 changed file with 16 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@ package com.aliucord.manager.manager.download

import com.aliucord.manager.manager.download.IDownloadManager.Result
import io.ktor.client.HttpClient
import io.ktor.client.request.*
import io.ktor.client.request.header
import io.ktor.client.request.prepareGet
import io.ktor.client.statement.bodyAsChannel
import io.ktor.http.HttpHeaders
import io.ktor.http.contentLength
import io.ktor.utils.io.core.isEmpty
import io.ktor.utils.io.core.readBytes
import kotlinx.coroutines.*
import io.ktor.utils.io.readAvailable
import kotlinx.coroutines.CancellationException
import java.io.File
import kotlin.math.min

/**
* Handle downloading remote urls to a path with Ktor.
Expand All @@ -37,24 +36,23 @@ class KtorDownloadManager(private val http: HttpClient) : IDownloadManager {
val total = resp.contentLength() ?: 0
var retrieved = 0L

val buf = ByteArray(1024 * 1024 * 1)
var bufLen = 0

tmpOut.outputStream().use { stream ->
while (!channel.isClosedForRead) {
channel.awaitContent()
val packet = channel.readRemaining(1024 * 1024 * 1)
bufLen = channel.readAvailable(buf)
if (bufLen <= 0) break

while (!packet.isEmpty) {
// TODO: reuse bytearray
val bytes = packet.readBytes()
stream.write(bytes)
stream.flush()
stream.write(buf, 0, bufLen)
stream.flush()

retrieved += bytes.size
retrieved += bufLen

if (total > 0) {
onProgressUpdate?.onUpdate(retrieved / total.toFloat())
} else {
onProgressUpdate?.onUpdate(null)
}
if (total > 0) {
onProgressUpdate?.onUpdate(retrieved / total.toFloat())
} else {
onProgressUpdate?.onUpdate(null)
}
}
}
Expand Down

0 comments on commit 57ade5a

Please sign in to comment.