Skip to content

Commit 070d0fb

Browse files
supporting passing fileEncoding to DataSaverProperties #8
1 parent 23f5e66 commit 070d0fb

3 files changed

Lines changed: 55 additions & 21 deletions

File tree

composeApp/src/desktopMain/kotlin/com/funny/data_saver/main.kt

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import com.funny.data_saver.core.LocalDataSaver
77
import com.funny.data_saver.ui.ExampleComposable
88
import com.funny.data_saver.ui.theme.FunnyTheme
99
import kotlinx.serialization.ExperimentalSerializationApi
10+
import moe.tlaster.precompose.ProvidePreComposeLocals
1011

1112
@OptIn(ExperimentalSerializationApi::class)
1213
fun main() {
@@ -15,9 +16,11 @@ fun main() {
1516
onCloseRequest = ::exitApplication,
1617
title = "Compose Data Saver"
1718
) {
18-
FunnyTheme {
19-
CompositionLocalProvider(LocalDataSaver provides AppConfig.dataSaver){
20-
ExampleComposable()
19+
ProvidePreComposeLocals {
20+
FunnyTheme {
21+
CompositionLocalProvider(LocalDataSaver provides AppConfig.dataSaver) {
22+
ExampleComposable()
23+
}
2124
}
2225
}
2326
}

data-saver-core/src/desktopMain/kotlin/com/funny/data_saver/core/DataSaverEncryptedProperties.kt

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
package com.funny.data_saver.core
22

3+
import com.funny.data_saver.kmp.Log
4+
import java.io.FileInputStream
35
import java.io.FileNotFoundException
4-
import java.io.FileReader
5-
import java.io.FileWriter
6+
import java.io.FileOutputStream
7+
import java.io.InputStreamReader
8+
import java.io.OutputStreamWriter
69
import java.security.MessageDigest
710
import java.util.Base64
811
import java.util.Properties
@@ -11,40 +14,45 @@ import javax.crypto.spec.IvParameterSpec
1114
import javax.crypto.spec.SecretKeySpec
1215

1316
/**
14-
* Use [Properties] to save data in a properties file with encryption. The algorithm is AES/CBC/NoPadding.
17+
* Use [Properties] to save data in a properties file with encryption. The algorithm is AES/CBC/PKCS5Padding.
1518
*
1619
* @property filePath String The file path to save the data file. 数据文件的保存路径。
1720
* @param encryptionKey String The key to encrypt the data, can be any string. 用于加密数据的密钥,可以是任意字符串(实际使用的密钥为该字符串的 SHA-256 哈希值,且仅用其前 16 位产生 iv)
21+
* @param fileEncoding String The encoding of the data file, default to UTF-8. 数据文件的编码,默认为 UTF-8。
1822
*/
1923

20-
open class DataSaverEncryptedProperties(private val filePath: String, private val encryptionKey: String) : DataSaverInterface() {
24+
open class DataSaverEncryptedProperties(
25+
private val filePath: String,
26+
private val encryptionKey: String,
27+
private val fileEncoding: String = "UTF-8"
28+
) : DataSaverInterface() {
2129
private val properties = Properties()
2230
private val hashedKey = hashKey(encryptionKey)
2331
private val encryptCipher by lazy { createCipher(Cipher.ENCRYPT_MODE) }
2432
private val decryptCipher by lazy { createCipher(Cipher.DECRYPT_MODE) }
2533

2634
init {
2735
try {
28-
createFile(filePath)
29-
FileReader(filePath).use { reader ->
36+
InputStreamReader(FileInputStream(filePath), fileEncoding).use { reader ->
3037
properties.load(reader)
3138
}
3239
} catch (e: FileNotFoundException) {
33-
// Handle file not found exception
40+
// 处理文件不存在等异常
41+
createFile(filePath)
3442
} catch (e: Exception) {
35-
// Handle other exceptions
36-
e.printStackTrace()
43+
Log.e(TAG, "Error loading properties: ${e.message}", e)
3744
}
3845
}
3946

4047
private fun saveProperties() {
4148
try {
42-
FileWriter(filePath).use { writer ->
49+
OutputStreamWriter(FileOutputStream(filePath), fileEncoding).use { writer ->
4350
properties.store(writer, null)
4451
}
52+
} catch (e: FileNotFoundException) {
53+
Log.e(TAG, "File not found: $filePath")
4554
} catch (e: Exception) {
46-
// Handle file write exception
47-
e.printStackTrace()
55+
Log.e(TAG, "Error saving properties: ${e.message}", e)
4856
}
4957
}
5058

@@ -99,4 +107,8 @@ open class DataSaverEncryptedProperties(private val filePath: String, private va
99107
override fun contains(key: String): Boolean {
100108
return properties.containsKey(key)
101109
}
110+
111+
companion object {
112+
private const val TAG = "DataSaverEncryptedProperties"
113+
}
102114
}

data-saver-core/src/desktopMain/kotlin/com/funny/data_saver/core/DataSaverProperties.kt

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
package com.funny.data_saver.core
22

3+
import com.funny.data_saver.kmp.Log
34
import java.io.File
5+
import java.io.FileInputStream
46
import java.io.FileNotFoundException
5-
import java.io.FileReader
6-
import java.io.FileWriter
7+
import java.io.FileOutputStream
8+
import java.io.InputStreamReader
9+
import java.io.OutputStreamWriter
710
import java.util.Properties
811

912
/**
@@ -12,25 +15,37 @@ import java.util.Properties
1215
* ---
1316
* 基于 [Properties] 的数据存储器,所有数据都是以 **明文** 存储的。如果你想要加密数据,可以使用 [DataSaverEncryptedProperties]。
1417
* @property filePath String The file path to save the data file. 数据文件的保存路径。
18+
* @property fileEncoding String The encoding of the data file, default to UTF-8. 数据文件的编码,默认为 UTF-8。
1519
* @constructor
1620
*/
17-
class DataSaverProperties(private val filePath: String) : DataSaverInterface() {
21+
class DataSaverProperties(
22+
private val filePath: String,
23+
private val fileEncoding: String = "UTF-8"
24+
) : DataSaverInterface() {
1825
private val properties = Properties()
1926

2027
init {
2128
try {
22-
FileReader(filePath).use { reader ->
29+
InputStreamReader(FileInputStream(filePath), fileEncoding).use { reader ->
2330
properties.load(reader)
2431
}
2532
} catch (e: FileNotFoundException) {
2633
// 处理文件不存在等异常
2734
createFile(filePath)
35+
} catch (e: Exception) {
36+
Log.e(TAG, "Error loading properties: ${e.message}", e)
2837
}
2938
}
3039

3140
private fun saveProperties() {
32-
FileWriter(filePath).use { writer ->
33-
properties.store(writer, null)
41+
try {
42+
OutputStreamWriter(FileOutputStream(filePath), fileEncoding).use { writer ->
43+
properties.store(writer, null)
44+
}
45+
} catch (e: FileNotFoundException) {
46+
Log.e(TAG, "File not found: $filePath")
47+
} catch (e: Exception) {
48+
Log.e(TAG, "Error saving properties: ${e.message}", e)
3449
}
3550
}
3651

@@ -60,6 +75,10 @@ class DataSaverProperties(private val filePath: String) : DataSaverInterface() {
6075
override fun contains(key: String): Boolean {
6176
return properties.containsKey(key)
6277
}
78+
79+
companion object {
80+
private const val TAG = "DataSaverProperties"
81+
}
6382
}
6483

6584
internal fun createFile(filePath: String) {

0 commit comments

Comments
 (0)