1+ /*
2+ * MIT License
3+ *
4+ * Copyright (c) 2023 Agora Community
5+ *
6+ * Permission is hereby granted, free of charge, to any person obtaining a copy
7+ * of this software and associated documentation files (the "Software"), to deal
8+ * in the Software without restriction, including without limitation the rights
9+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+ * copies of the Software, and to permit persons to whom the Software is
11+ * furnished to do so, subject to the following conditions:
12+ *
13+ * The above copyright notice and this permission notice shall be included in all
14+ * copies or substantial portions of the Software.
15+ *
16+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+ * SOFTWARE.
23+ */
24+
25+ package io.agora.api.example.examples.advanced.beauty.utils
26+
27+ import android.content.Context
28+ import android.util.Log
29+ import java.io.BufferedInputStream
30+ import java.io.BufferedOutputStream
31+ import java.io.BufferedReader
32+ import java.io.File
33+ import java.io.FileOutputStream
34+ import java.io.IOException
35+ import java.io.InputStream
36+ import java.io.InputStreamReader
37+ import java.io.OutputStream
38+
39+ object FileUtils {
40+ val TAG = " FileUtils"
41+
42+ fun getAssetsString (context : Context , path : String ): String {
43+ val sb = StringBuilder ()
44+ var isr: InputStreamReader ? = null
45+ var br: BufferedReader ? = null
46+ try {
47+ isr = InputStreamReader (context.resources.assets.open(path))
48+ br = BufferedReader (isr)
49+ var line: String? = null
50+ while (br.readLine().also { line = it } != null ) {
51+ sb.append(line).append(" \n " )
52+ }
53+ } catch (e: IOException ) {
54+ Log .e(TAG , " getAssetsString error: $e " )
55+ } finally {
56+ if (isr != null ) {
57+ try {
58+ isr.close()
59+ } catch (e: IOException ) {
60+ e.printStackTrace()
61+ }
62+ }
63+ if (br != null ) {
64+ try {
65+ br.close()
66+ } catch (e: IOException ) {
67+ e.printStackTrace()
68+ }
69+ }
70+ }
71+ return sb.toString()
72+ }
73+
74+ fun copyAssets (context : Context , assetsPath : String , targetPath : String ) {
75+ val fileNames = context.resources.assets.list(assetsPath)
76+ if (fileNames?.isNotEmpty() == true ) {
77+ val targetFile = File (targetPath)
78+ if (! targetFile.exists() && ! targetFile.mkdirs()) {
79+ return
80+ }
81+ for (fileName in fileNames) {
82+ copyAssets(
83+ context,
84+ " $assetsPath /$fileName " ,
85+ " $targetPath /$fileName "
86+ )
87+ }
88+ } else {
89+ copyAssetsFile(context, assetsPath, targetPath)
90+ }
91+ }
92+
93+ private fun copyAssetsFile (context : Context , assetsFile : String , targetPath : String ) {
94+ val dest = File (targetPath)
95+ dest.parentFile?.mkdirs()
96+ var input: InputStream ? = null
97+ var output: OutputStream ? = null
98+ try {
99+ input = BufferedInputStream (context.assets.open(assetsFile))
100+ output = BufferedOutputStream (FileOutputStream (dest))
101+ val buffer = ByteArray (1024 )
102+ var length = 0
103+ while (input.read(buffer).also { length = it } != - 1 ) {
104+ output.write(buffer, 0 , length)
105+ }
106+ } catch (e: Exception ) {
107+ Log .e(TAG , " copyAssetsFile" , e)
108+ } finally {
109+ output?.close()
110+ input?.close()
111+ }
112+ }
113+ }
0 commit comments