|
| 1 | +package sk.ainet.data.mnist |
| 2 | + |
| 3 | +import io.ktor.client.HttpClient |
| 4 | +import io.ktor.client.engine.android.Android |
| 5 | +import io.ktor.client.request.get |
| 6 | +import io.ktor.client.statement.HttpResponse |
| 7 | +import io.ktor.client.call.body |
| 8 | +import kotlinx.coroutines.Dispatchers |
| 9 | +import kotlinx.coroutines.withContext |
| 10 | +import java.io.File |
| 11 | +import java.io.FileInputStream |
| 12 | +import java.io.FileOutputStream |
| 13 | +import java.util.zip.GZIPInputStream |
| 14 | + |
| 15 | +/** |
| 16 | + * Android implementation of the MNIST loader. |
| 17 | + * |
| 18 | + * @property config The configuration for the MNIST loader. |
| 19 | + */ |
| 20 | +public class MNISTLoaderAndroid(config: MNISTLoaderConfig) : MNISTLoaderCommon(config) { |
| 21 | + |
| 22 | + /** |
| 23 | + * Downloads and caches a file. |
| 24 | + * |
| 25 | + * @param url The URL to download from. |
| 26 | + * @param filename The name of the file to save. |
| 27 | + * @return The bytes of the decompressed file. |
| 28 | + */ |
| 29 | + override suspend fun downloadAndCacheFile(url: String, filename: String): ByteArray = withContext(Dispatchers.IO) { |
| 30 | + val cacheDir = File(config.cacheDir) |
| 31 | + if (!cacheDir.exists()) { |
| 32 | + cacheDir.mkdirs() |
| 33 | + } |
| 34 | + |
| 35 | + val gzipFile = File(cacheDir, filename) |
| 36 | + val decompressedFile = File(cacheDir, filename.removeSuffix(".gz")) |
| 37 | + |
| 38 | + // Check if the decompressed file already exists in cache |
| 39 | + if (config.useCache && decompressedFile.exists()) { |
| 40 | + println("Using cached file: ${decompressedFile.path}") |
| 41 | + return@withContext decompressedFile.readBytes() |
| 42 | + } |
| 43 | + |
| 44 | + // Check if the gzip file already exists in cache |
| 45 | + if (!gzipFile.exists() || !config.useCache) { |
| 46 | + println("Downloading file: $url") |
| 47 | + downloadFile(url, gzipFile.path) |
| 48 | + } else { |
| 49 | + println("Using cached gzip file: ${gzipFile.path}") |
| 50 | + } |
| 51 | + |
| 52 | + // Decompress the gzip file |
| 53 | + println("Decompressing file: ${gzipFile.path}") |
| 54 | + decompressGzipFile(gzipFile.path, decompressedFile.path) |
| 55 | + |
| 56 | + return@withContext decompressedFile.readBytes() |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * Downloads a file from a URL. |
| 61 | + * |
| 62 | + * @param url The URL to download from. |
| 63 | + * @param outputPath The path to save the file to. |
| 64 | + */ |
| 65 | + private suspend fun downloadFile(url: String, outputPath: String) { |
| 66 | + val client = HttpClient(Android) { |
| 67 | + // No plugins needed for basic functionality |
| 68 | + } |
| 69 | + |
| 70 | + try { |
| 71 | + val file = File(outputPath) |
| 72 | + |
| 73 | + val httpResponse: HttpResponse = client.get(url) |
| 74 | + val responseBody: ByteArray = httpResponse.body() |
| 75 | + file.writeBytes(responseBody) |
| 76 | + |
| 77 | + println("File saved to ${file.path}") |
| 78 | + } finally { |
| 79 | + client.close() |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * Decompresses a gzip file. |
| 85 | + * |
| 86 | + * @param gzipFilePath The path to the gzip file. |
| 87 | + * @param outputFilePath The path to save the decompressed file to. |
| 88 | + */ |
| 89 | + private fun decompressGzipFile(gzipFilePath: String, outputFilePath: String) { |
| 90 | + GZIPInputStream(FileInputStream(gzipFilePath)).use { gzipInputStream -> |
| 91 | + FileOutputStream(outputFilePath).use { outputStream -> |
| 92 | + val buffer = ByteArray(1024) |
| 93 | + var len: Int |
| 94 | + while (gzipInputStream.read(buffer).also { len = it } > 0) { |
| 95 | + outputStream.write(buffer, 0, len) |
| 96 | + } |
| 97 | + } |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + public companion object { |
| 102 | + /** |
| 103 | + * Creates a new instance of MNISTLoaderAndroid with the default configuration. |
| 104 | + * |
| 105 | + * @return A new instance of MNISTLoaderAndroid. |
| 106 | + */ |
| 107 | + public fun create(): MNISTLoaderAndroid { |
| 108 | + return MNISTLoaderAndroid(MNISTLoaderConfig()) |
| 109 | + } |
| 110 | + |
| 111 | + /** |
| 112 | + * Creates a new instance of MNISTLoaderAndroid with a custom cache directory. |
| 113 | + * |
| 114 | + * @param cacheDir The directory to use for caching. |
| 115 | + * @return A new instance of MNISTLoaderAndroid. |
| 116 | + */ |
| 117 | + public fun create(cacheDir: String): MNISTLoaderAndroid { |
| 118 | + return MNISTLoaderAndroid(MNISTLoaderConfig(cacheDir = cacheDir)) |
| 119 | + } |
| 120 | + |
| 121 | + /** |
| 122 | + * Creates a new instance of MNISTLoaderAndroid with a custom configuration. |
| 123 | + * |
| 124 | + * @param config The configuration to use. |
| 125 | + * @return A new instance of MNISTLoaderAndroid. |
| 126 | + */ |
| 127 | + public fun create(config: MNISTLoaderConfig): MNISTLoaderAndroid { |
| 128 | + return MNISTLoaderAndroid(config) |
| 129 | + } |
| 130 | + } |
| 131 | +} |
0 commit comments