-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Expand file tree
/
Copy pathUtil.kt
More file actions
208 lines (178 loc) · 6.31 KB
/
Util.kt
File metadata and controls
208 lines (178 loc) · 6.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
package moe.matsuri.nb4a.utils
import android.annotation.SuppressLint
import android.content.Context
import android.util.Base64
import libcore.StringBox
import java.io.ByteArrayOutputStream
import java.net.URLDecoder
import java.nio.charset.StandardCharsets
import java.text.SimpleDateFormat
import java.util.*
import java.util.zip.Deflater
import java.util.zip.Inflater
object Util {
/**
* 取两个文本之间的文本值
*
* @param text 源文本 比如:欲取全文本为 12345
* @param left 文本前面
* @param right 后面文本
* @return 返回 String
*/
fun getSubString(text: String, left: String?, right: String?): String {
var zLen: Int
if (left.isNullOrEmpty()) {
zLen = 0
} else {
zLen = text.indexOf(left)
if (zLen > -1) {
zLen += left.length
} else {
zLen = 0
}
}
var yLen = if (right == null) -1 else text.indexOf(right, zLen)
if (yLen < 0 || right.isNullOrEmpty()) {
yLen = text.length
}
return text.substring(zLen, yLen)
}
// Base64 for all
fun b64EncodeUrlSafe(s: String): String {
return b64EncodeUrlSafe(s.toByteArray())
}
fun b64EncodeUrlSafe(b: ByteArray): String {
return String(Base64.encode(b, Base64.NO_PADDING or Base64.NO_WRAP or Base64.URL_SAFE))
}
// v2rayN Style
fun b64EncodeOneLine(b: ByteArray): String {
return String(Base64.encode(b, Base64.NO_WRAP))
}
fun b64EncodeDefault(b: ByteArray): String {
return String(Base64.encode(b, Base64.DEFAULT))
}
fun b64Decode(b: String): ByteArray {
var ret: ByteArray? = null
// padding 自动处理,不用理
// URLSafe 需要替换这两个,不要用 URL_SAFE 否则处理非 Safe 的时候会乱码
val str = b.replace("-", "+").replace("_", "/")
val flags = listOf(
Base64.DEFAULT, // 多行
Base64.NO_WRAP, // 单行
)
for (flag in flags) {
try {
ret = Base64.decode(str, flag)
} catch (_: Exception) {
}
if (ret != null) return ret
}
throw IllegalStateException("Cannot decode base64")
}
fun zlibCompress(input: ByteArray, level: Int): ByteArray {
// Compress the bytes
// 1 to 4 bytes/char for UTF-8
val output = ByteArray(input.size * 4)
val compressor = Deflater(level).apply {
setInput(input)
finish()
}
val compressedDataLength: Int = compressor.deflate(output)
compressor.end()
return output.copyOfRange(0, compressedDataLength)
}
fun zlibDecompress(input: ByteArray): ByteArray {
val inflater = Inflater()
val outputStream = ByteArrayOutputStream()
return outputStream.use {
val buffer = ByteArray(1024)
inflater.setInput(input)
var count = -1
while (count != 0) {
count = inflater.inflate(buffer)
outputStream.write(buffer, 0, count)
}
inflater.end()
outputStream.toByteArray()
}
}
fun map2StringMap(m: Map<*, *>): MutableMap<String, Any?> {
val o = mutableMapOf<String, Any?>()
m.forEach {
if (it.key is String) {
o[it.key as String] = it.value as Any
}
}
return o
}
fun mergeMap(dst: MutableMap<String, Any?>, src: Map<String, Any?>): MutableMap<String, Any?> {
src.forEach { (k, v) ->
if (v is Map<*, *> && dst[k] is Map<*, *>) {
val currentMap = (dst[k] as Map<*, *>).toMutableMap()
dst[k] = mergeMap(map2StringMap(currentMap), map2StringMap(v))
} else if (v is List<*>) {
if (k.startsWith("+")) { // prepend
val dstKey = k.removePrefix("+")
var currentList = (dst[dstKey] as? List<*>)?.toMutableList() ?: mutableListOf()
currentList = (v + currentList).toMutableList()
dst[dstKey] = currentList
} else if (k.endsWith("+")) { // append
val dstKey = k.removeSuffix("+")
var currentList = (dst[dstKey] as? List<*>)?.toMutableList() ?: mutableListOf()
currentList = (currentList + v).toMutableList()
dst[dstKey] = currentList
} else {
dst[k] = v
}
} else {
dst[k] = v
}
}
return dst
}
fun mergeJSON(dst: MutableMap<String, Any?>, j: String) {
if (j.isBlank()) return
val src = JavaUtil.gson.fromJson(j, dst.javaClass)
mergeMap(dst, src)
}
// Format Time
@SuppressLint("SimpleDateFormat")
val sdf1 = SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
fun timeStamp2Text(t: Long): String {
return sdf1.format(Date(t))
}
fun tryToSetField(o: Any, name: String, value: Any) {
try {
o.javaClass.getField(name).set(o, value)
} catch (_: Exception) {
}
}
@SuppressLint("WrongConstant")
fun collapseStatusBar(context: Context) {
try {
val statusBarManager = context.getSystemService("statusbar")
val collapse = statusBarManager.javaClass.getMethod("collapsePanels")
collapse.invoke(statusBarManager)
} catch (_: Exception) {
}
}
fun getStringBox(b: StringBox?): String {
if (b != null && b.value != null) {
return b.value
}
return ""
}
fun decodeFilename(headerValue: String): String {
val regex = Regex("filename\\*=[^']*''(.+)")
val match = regex.find(headerValue)
val encoded = match?.groupValues?.get(1) ?: ""
return URLDecoder.decode(encoded, StandardCharsets.UTF_8.name())
}
fun generateCryptoSecurePassword(length: Int = 10): String {
val chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*()"
val secureRandom = java.security.SecureRandom()
return (1..length)
.map { chars[secureRandom.nextInt(chars.length)] }
.joinToString("")
}
}