Skip to content

Commit 14f9bf8

Browse files
committed
style: update README.md
pref: lazy load object pref: Modify the param of AesExtension's encrypt and decrypt method
1 parent c0b0e66 commit 14f9bf8

File tree

7 files changed

+81
-67
lines changed

7 files changed

+81
-67
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# sdk-ktx
2-
A lightweight SDK used to provide utils function and various extension
2+
A lightweight SDK for Android used to provide utils function and various extension.
3+
Only support for Kotlin.
34

45
## usage
56

sdk-library/src/main/java/pers/pslilysm/sdk_library/extention/AesExtension.kt

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package pers.pslilysm.sdk_library.extention
22

33
import android.util.Base64
44
import java.nio.charset.StandardCharsets
5+
import java.security.Key
6+
import java.security.spec.AlgorithmParameterSpec
57
import javax.crypto.Cipher
68
import javax.crypto.spec.IvParameterSpec
79
import javax.crypto.spec.SecretKeySpec
@@ -82,18 +84,14 @@ fun String.decrypt(): String {
8284
*
8385
* @param mode The name of the cipher mode, e.g.,
8486
* <i>DES/CBC/PKCS5Padding</i>.
85-
* @param secretKey The secret key
86-
* @param ivKey The iv key
87+
* @param key The encryption key
88+
* @param params The algorithm parameters
8789
* @return A encrypted Base64 String
8890
*/
89-
fun String.encrypt(mode: String, secretKey: String, ivKey: String): String {
91+
fun String.encrypt(mode: String, key: Key, params: AlgorithmParameterSpec): String {
9092
return try {
9193
val cipher = Cipher.getInstance(mode)
92-
cipher.init(
93-
Cipher.ENCRYPT_MODE,
94-
SecretKeySpec(secretKey.toByteArray(), "AES"),
95-
IvParameterSpec(ivKey.toByteArray())
96-
)
94+
cipher.init(Cipher.ENCRYPT_MODE, key, params)
9795
val bytes = cipher.doFinal(toByteArray(StandardCharsets.UTF_8))
9896
Base64.encodeToString(bytes, Base64.NO_WRAP)
9997
} catch (e: Exception) {
@@ -106,18 +104,14 @@ fun String.encrypt(mode: String, secretKey: String, ivKey: String): String {
106104
*
107105
* @param mode The name of the cipher mode, e.g.,
108106
* <i>DES/CBC/PKCS5Padding</i>.
109-
* @param secretKey The secret key
110-
* @param ivKey The iv key
107+
* @param key The encryption key
108+
* @param params The algorithm parameters
111109
* @return A decrypted Base64 String
112110
*/
113-
fun String.decrypt(mode: String, secretKey: String, ivKey: String): String {
111+
fun String.decrypt(mode: String, key: Key, params: AlgorithmParameterSpec): String {
114112
return try {
115113
val cipher = Cipher.getInstance(mode)
116-
cipher.init(
117-
Cipher.DECRYPT_MODE,
118-
SecretKeySpec(secretKey.toByteArray(), "AES"),
119-
IvParameterSpec(ivKey.toByteArray())
120-
)
114+
cipher.init(Cipher.DECRYPT_MODE, key, params)
121115
String(cipher.doFinal(Base64.decode(this, Base64.NO_WRAP)), StandardCharsets.UTF_8)
122116
} catch (e: Exception) {
123117
throw e.rethrow()

sdk-library/src/main/java/pers/pslilysm/sdk_library/extention/FormatterExtension.kt

Lines changed: 29 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -20,45 +20,54 @@ const val MB_SIZE = 1024f * 1024
2020

2121
const val KB_SIZE = 1024
2222

23-
private val sDateFormatTLS: ThreadLocal<SimpleDateFormat> =
23+
private val sDateFormatTLS: ThreadLocal<SimpleDateFormat> by lazy {
2424
object : ThreadLocal<SimpleDateFormat>() {
2525
override fun initialValue(): SimpleDateFormat {
2626
return SimpleDateFormat("", Locale.getDefault())
2727
}
2828
}
29-
private val sDecimalFormatTLS: ThreadLocal<DecimalFormat> =
29+
}
30+
31+
private val sDecimalFormatTLS: ThreadLocal<DecimalFormat> by lazy {
3032
object : ThreadLocal<DecimalFormat>() {
3133
override fun initialValue(): DecimalFormat {
3234
return DecimalFormat()
3335
}
3436
}
37+
}
3538

3639
val simpleDateFormat: SimpleDateFormat get() = sDateFormatTLS.get() as SimpleDateFormat
3740
val decimalFormat: DecimalFormat get() = sDecimalFormatTLS.get() as DecimalFormat
3841

39-
val pattern_yyyyMMddHHmmssDateFormat: SimpleDateFormat get() = simpleDateFormat.apply {
40-
applyPattern("yyyy-MM-dd HH:mm:ss")
41-
}
42+
val pattern_yyyyMMddHHmmssDateFormat: SimpleDateFormat
43+
get() = simpleDateFormat.apply {
44+
applyPattern("yyyy-MM-dd HH:mm:ss")
45+
}
4246

43-
val pattern_MMddDateFormat: SimpleDateFormat get() = simpleDateFormat.apply {
44-
applyPattern("MM-dd")
45-
}
47+
val pattern_MMddDateFormat: SimpleDateFormat
48+
get() = simpleDateFormat.apply {
49+
applyPattern("MM-dd")
50+
}
4651

47-
val pattern_HHmmssDateFormat: SimpleDateFormat get() = simpleDateFormat.apply {
48-
applyPattern("HH:mm:ss")
49-
}
52+
val pattern_HHmmssDateFormat: SimpleDateFormat
53+
get() = simpleDateFormat.apply {
54+
applyPattern("HH:mm:ss")
55+
}
5056

51-
val pattern_000DecimalFormat: DecimalFormat get() = decimalFormat.apply {
52-
applyPattern("#.000")
53-
}
57+
val pattern_000DecimalFormat: DecimalFormat
58+
get() = decimalFormat.apply {
59+
applyPattern("#.000")
60+
}
5461

55-
val pattern_00DecimalFormat: DecimalFormat get() = decimalFormat.apply {
56-
applyPattern("#.00")
57-
}
62+
val pattern_00DecimalFormat: DecimalFormat
63+
get() = decimalFormat.apply {
64+
applyPattern("#.00")
65+
}
5866

59-
val pattern_0DecimalFormat: DecimalFormat get() = decimalFormat.apply {
60-
applyPattern("#.0")
61-
}
67+
val pattern_0DecimalFormat: DecimalFormat
68+
get() = decimalFormat.apply {
69+
applyPattern("#.0")
70+
}
6271

6372
/**
6473
* @return A formatted file size string with pattern "#.00"

sdk-library/src/main/java/pers/pslilysm/sdk_library/extention/FragmentExtension.kt

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,16 @@ import pers.pslilysm.sdk_library.util.reflection.ReflectionUtil
1414
* @since 2.2.0
1515
*/
1616

17-
val sViewPager2Finder: FragmentFinder = object : FragmentFinder {
18-
override fun <T : Fragment?> findFragment(
19-
fmtClass: Class<out Fragment?>,
20-
vararg args: Any?
21-
): T? {
22-
val manager = args[0] as FragmentManager
23-
val position = args[1] as Int
24-
return manager.findFragmentByTag("f$position") as T?
17+
val sViewPager2Finder: FragmentFinder by lazy {
18+
object : FragmentFinder {
19+
override fun <T : Fragment?> findFragment(
20+
fmtClass: Class<out Fragment?>,
21+
vararg args: Any?
22+
): T? {
23+
val manager = args[0] as FragmentManager
24+
val position = args[1] as Int
25+
return manager.findFragmentByTag("f$position") as T?
26+
}
2527
}
2628
}
2729

sdk-library/src/main/java/pers/pslilysm/sdk_library/extention/GsonExtension.kt

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,26 +20,32 @@ import pers.pslilysm.sdk_library.util.reflection.ReflectionUtil
2020
* @since 2.2.0
2121
*/
2222

23-
private val strategy: ExclusionStrategy = object : ExclusionStrategy {
24-
override fun shouldSkipField(f: FieldAttributes): Boolean {
25-
return f.getAnnotation(GsonExclude::class.java) != null
26-
}
23+
private val strategy: ExclusionStrategy by lazy {
24+
object : ExclusionStrategy {
25+
override fun shouldSkipField(f: FieldAttributes): Boolean {
26+
return f.getAnnotation(GsonExclude::class.java) != null
27+
}
2728

28-
override fun shouldSkipClass(clazz: Class<*>): Boolean {
29-
return clazz.getAnnotation(GsonExclude::class.java) != null
29+
override fun shouldSkipClass(clazz: Class<*>): Boolean {
30+
return clazz.getAnnotation(GsonExclude::class.java) != null
31+
}
3032
}
3133
}
3234

33-
private val sPrettyGson: Gson = GsonBuilder()
34-
.setPrettyPrinting()
35-
.disableHtmlEscaping()
36-
.setExclusionStrategies(strategy)
37-
.create()
35+
private val sPrettyGson: Gson by lazy {
36+
GsonBuilder()
37+
.setPrettyPrinting()
38+
.disableHtmlEscaping()
39+
.setExclusionStrategies(strategy)
40+
.create()
41+
}
3842

39-
private val sGson: Gson = GsonBuilder()
40-
.disableHtmlEscaping()
41-
.setExclusionStrategies(strategy)
42-
.create()
43+
private val sGson: Gson by lazy {
44+
GsonBuilder()
45+
.disableHtmlEscaping()
46+
.setExclusionStrategies(strategy)
47+
.create()
48+
}
4349

4450
/**
4551
* Serialize object to json

sdk-library/src/main/java/pers/pslilysm/sdk_library/util/concurrent/GlobalExecutors.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import java.util.concurrent.atomic.AtomicInteger
2020
object GlobalExecutors {
2121
private val sIONum = AtomicInteger()
2222
private val sComputeNum = AtomicInteger()
23-
private val lazyGlobalIOExecutor by lazy {
23+
private val sIOExecutor by lazy {
2424
val corePoolSize = 1
2525
val maxPoolSize = Runtime.getRuntime().availableProcessors() * 10
2626
val keepAliveTimeSeconds = 2
@@ -56,7 +56,7 @@ object GlobalExecutors {
5656
workQueue.setExecutor(ioES)
5757
ScheduledThreadPoolExecutorWrapper(ioES)
5858
}
59-
private val lazyGlobalComputeExecutor by lazy {
59+
private val sComputeExecutor by lazy {
6060
val corePoolSize = 1
6161
val maxPoolSize = Runtime.getRuntime().availableProcessors()
6262
val keepAliveTimeSeconds = 2
@@ -104,12 +104,12 @@ object GlobalExecutors {
104104
/**
105105
* @return a global io executor, the core pool size is `cpu cores * 5`
106106
*/
107-
val io: ScheduledExecutorService get() = lazyGlobalIOExecutor
107+
val io: ScheduledExecutorService get() = sIOExecutor
108108

109109
/**
110110
* @return a global compute executor, the core pool size is cpu cores
111111
*/
112-
val compute: ScheduledExecutorService get() = lazyGlobalComputeExecutor
112+
val compute: ScheduledExecutorService get() = sComputeExecutor
113113

114114
/**
115115
* @return a global main executor, all runnable will run in main thread

sdk-library/src/main/java/pers/pslilysm/sdk_library/util/reflection/ReflectionUtil.kt

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ import java.util.*
1515
* @since 1.0.0
1616
*/
1717
object ReflectionUtil {
18-
private val sConstructors: MutableMap<ConstructorKey, Constructor<*>> = ArrayMap()
19-
private val sFields: MutableMap<FieldKey, Field> = ArrayMap()
20-
private val sMethods: MutableMap<MethodKey, Method> = ArrayMap()
18+
private val sConstructors: MutableMap<ConstructorKey, Constructor<*>> by lazy { ArrayMap() }
19+
private val sFields: MutableMap<FieldKey, Field> by lazy { ArrayMap() }
20+
private val sMethods: MutableMap<MethodKey, Method> by lazy { ArrayMap() }
2121
private val sEmptyParameterTypesAndArgs = emptyArray<Any>()
2222

2323
/**
@@ -51,9 +51,11 @@ object ReflectionUtil {
5151
is String -> {
5252
parameterTypes[i] = classLoader.loadClass(pt)
5353
}
54+
5455
is Class<*> -> {
5556
parameterTypes[i] = pt
5657
}
58+
5759
else -> {
5860
throw IllegalArgumentException("check your parameterTypes at pos " + i + ", type is " + pt!!.javaClass)
5961
}

0 commit comments

Comments
 (0)