mirror of
https://github.com/advplyr/audiobookshelf-app.git
synced 2026-08-26 21:34:05 +02:00
InternalDownloadManager autoformatting and comments
This commit is contained in:
+76
-33
@@ -1,60 +1,98 @@
|
|||||||
package com.audiobookshelf.app.managers
|
package com.audiobookshelf.app.managers
|
||||||
|
|
||||||
import android.util.Log
|
import android.util.Log
|
||||||
import com.google.common.net.HttpHeaders.CONTENT_LENGTH
|
|
||||||
import okhttp3.*
|
|
||||||
import java.io.*
|
import java.io.*
|
||||||
import java.util.*
|
import java.util.concurrent.TimeUnit
|
||||||
|
import okhttp3.*
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Manages the internal download process.
|
||||||
|
*
|
||||||
|
* @property outputStream The output stream to write the downloaded data.
|
||||||
|
* @property progressCallback The callback to report download progress.
|
||||||
|
*/
|
||||||
|
class InternalDownloadManager(
|
||||||
|
private val outputStream: FileOutputStream,
|
||||||
|
private val progressCallback: DownloadItemManager.InternalProgressCallback
|
||||||
|
) : AutoCloseable {
|
||||||
|
|
||||||
class InternalDownloadManager(outputStream:FileOutputStream, private val progressCallback: DownloadItemManager.InternalProgressCallback) : AutoCloseable {
|
|
||||||
private val tag = "InternalDownloadManager"
|
private val tag = "InternalDownloadManager"
|
||||||
|
private val client: OkHttpClient =
|
||||||
private val client: OkHttpClient = OkHttpClient()
|
OkHttpClient.Builder()
|
||||||
|
.connectTimeout(30, TimeUnit.SECONDS)
|
||||||
|
.readTimeout(30, TimeUnit.SECONDS)
|
||||||
|
.build()
|
||||||
private val writer = BinaryFileWriter(outputStream, progressCallback)
|
private val writer = BinaryFileWriter(outputStream, progressCallback)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Downloads a file from the given URL.
|
||||||
|
*
|
||||||
|
* @param url The URL to download the file from.
|
||||||
|
* @throws IOException If an I/O error occurs.
|
||||||
|
*/
|
||||||
@Throws(IOException::class)
|
@Throws(IOException::class)
|
||||||
fun download(url:String) {
|
fun download(url: String) {
|
||||||
val request: Request = Request.Builder().url(url).build()
|
val request: Request = Request.Builder().url(url).build()
|
||||||
client.newCall(request).enqueue(object : Callback {
|
client.newCall(request)
|
||||||
override fun onFailure(call: Call, e: IOException) {
|
.enqueue(
|
||||||
Log.e(tag, "download URL $url FAILED")
|
object : Callback {
|
||||||
progressCallback.onComplete(true)
|
override fun onFailure(call: Call, e: IOException) {
|
||||||
}
|
Log.e(tag, "Download URL $url FAILED", e)
|
||||||
|
progressCallback.onComplete(true)
|
||||||
|
}
|
||||||
|
|
||||||
override fun onResponse(call: Call, response: Response) {
|
override fun onResponse(call: Call, response: Response) {
|
||||||
val responseBody: ResponseBody = response.body
|
response.body?.let { responseBody ->
|
||||||
?: throw IllegalStateException("Response doesn't contain a file")
|
val length: Long = response.header("Content-Length")?.toLongOrNull() ?: 0L
|
||||||
|
writer.write(responseBody.byteStream(), length)
|
||||||
val length: Long = (response.header(CONTENT_LENGTH, "1") ?: "0").toLong()
|
}
|
||||||
writer.write(responseBody.byteStream(), length)
|
?: run {
|
||||||
}
|
Log.e(tag, "Response doesn't contain a file")
|
||||||
})
|
progressCallback.onComplete(true)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Closes the download manager and releases resources.
|
||||||
|
*
|
||||||
|
* @throws Exception If an error occurs during closing.
|
||||||
|
*/
|
||||||
@Throws(Exception::class)
|
@Throws(Exception::class)
|
||||||
override fun close() {
|
override fun close() {
|
||||||
writer.close()
|
writer.close()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class BinaryFileWriter(outputStream: OutputStream, progressCallback: DownloadItemManager.InternalProgressCallback) :
|
/**
|
||||||
AutoCloseable {
|
* Writes binary data to an output stream.
|
||||||
private val outputStream: OutputStream
|
*
|
||||||
private val progressCallback: DownloadItemManager.InternalProgressCallback
|
* @property outputStream The output stream to write the data to.
|
||||||
|
* @property progressCallback The callback to report write progress.
|
||||||
init {
|
*/
|
||||||
this.outputStream = outputStream
|
class BinaryFileWriter(
|
||||||
this.progressCallback = progressCallback
|
private val outputStream: OutputStream,
|
||||||
}
|
private val progressCallback: DownloadItemManager.InternalProgressCallback
|
||||||
|
) : AutoCloseable {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Writes data from the input stream to the output stream.
|
||||||
|
*
|
||||||
|
* @param inputStream The input stream to read the data from.
|
||||||
|
* @param length The total length of the data to be written.
|
||||||
|
* @return The total number of bytes written.
|
||||||
|
* @throws IOException If an I/O error occurs.
|
||||||
|
*/
|
||||||
@Throws(IOException::class)
|
@Throws(IOException::class)
|
||||||
fun write(inputStream: InputStream?, length: Long): Long {
|
fun write(inputStream: InputStream, length: Long): Long {
|
||||||
BufferedInputStream(inputStream).use { input ->
|
BufferedInputStream(inputStream).use { input ->
|
||||||
val dataBuffer = ByteArray(CHUNK_SIZE)
|
val dataBuffer = ByteArray(CHUNK_SIZE)
|
||||||
var readBytes: Int
|
|
||||||
var totalBytes: Long = 0
|
var totalBytes: Long = 0
|
||||||
|
var readBytes: Int
|
||||||
while (input.read(dataBuffer).also { readBytes = it } != -1) {
|
while (input.read(dataBuffer).also { readBytes = it } != -1) {
|
||||||
totalBytes += readBytes.toLong()
|
totalBytes += readBytes
|
||||||
outputStream.write(dataBuffer, 0, readBytes)
|
outputStream.write(dataBuffer, 0, readBytes)
|
||||||
progressCallback.onProgress(totalBytes, (totalBytes * 100L) / length)
|
progressCallback.onProgress(totalBytes, (totalBytes * 100L) / length)
|
||||||
}
|
}
|
||||||
@@ -63,12 +101,17 @@ class BinaryFileWriter(outputStream: OutputStream, progressCallback: DownloadIte
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Closes the writer and releases resources.
|
||||||
|
*
|
||||||
|
* @throws IOException If an error occurs during closing.
|
||||||
|
*/
|
||||||
@Throws(IOException::class)
|
@Throws(IOException::class)
|
||||||
override fun close() {
|
override fun close() {
|
||||||
outputStream.close()
|
outputStream.close()
|
||||||
}
|
}
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
private const val CHUNK_SIZE = 1024
|
private const val CHUNK_SIZE = 8192 // Increased chunk size for better performance
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user