package com.example.cmake_project_test

import android.content.Context
import android.util.Log
import java.io.IOException

/**
 * 文件帮助类
 * 负责文件读取操作
 */
object FileHelper {
    
    /**
     * 从 assets 文件夹读取文件到字节数组
     */
    fun readAssetFile(context: Context, fileName: String): ByteArray? {
        return try {
            context.assets.open(fileName).use { inputStream ->
                // 使用更可靠的文件读取方式
                val bytes = mutableListOf<Byte>()
                val buffer = ByteArray(1024)
                var bytesRead: Int
                
                while (inputStream.read(buffer).also { bytesRead = it } != -1) {
                    for (i in 0 until bytesRead) {
                        bytes.add(buffer[i])
                    }
                }
                
                bytes.toByteArray()
            }
        } catch (e: IOException) {
            Log.e("FileHelper", "Error reading asset file: $fileName", e)
            null
        } catch (e: Exception) {
            Log.e("FileHelper", "Unexpected error reading asset file: $fileName", e)
            null
        }
    }
}
