refactor(Base91): 更新Base91解码表并添加示例

更新了Base91解码表的初始化方式,并添加了一个示例方法来演示编码和解码功能。

feat(Base256): 添加Base256编解码器

新增了Base256类,实现了Base256编解码功能,并在BaseUtils中添加了相应的实例。

refactor(BaseCodec): 增加字符串编解码方法

在BaseCodec接口中增加了对字符串进行Base64编码和解码的方法。refactor(OsPath): 简化路径处理逻辑

移除了OsPath类中不必要的方法实现,简化了路径处理逻辑。

feat(UUID): 支持Base256短字符串

在UUID类中添加了对Base256短字符串的支持,包括编码和解码方法。
This commit is contained in:
Armamem0t 2025-09-18 14:43:01 +08:00
parent 4b187f3774
commit 23548c0c3d
Signed by: minglipro
GPG Key ID: 5F355A77B22AA93B
9 changed files with 633 additions and 68 deletions

View File

@ -16,7 +16,7 @@
* ProjectName mingli-utils
* ModuleName mingli-utils.main
* CurrentFile Main.kt
* LastUpdate 2025-09-17 19:07:01
* LastUpdate 2025-09-18 14:39:24
* UpdateUser MingLiPro
*/
@file:JvmName("Main")
@ -24,8 +24,10 @@
package com.mingliqiye.utils
import com.mingliqiye.utils.springboot.autoconfigure.AutoConfiguration
import com.mingliqiye.utils.uuid.UUID
fun main() {
AutoConfiguration.printBanner()
println(UUID.getV7().getBase256ShortString())
}

View File

@ -0,0 +1,317 @@
/*
* Copyright 2025 mingliqiye
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* ProjectName mingli-utils
* ModuleName mingli-utils.main
* CurrentFile Base256.kt
* LastUpdate 2025-09-18 14:36:40
* UpdateUser MingLiPro
*/
package com.mingliqiye.utils.base
/**
* Base256 字符集 256
*
* 256个字符 要字符集的下面复制
*
* !#$%&()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[]^_`abcdefghijklmnopqrstuvwxyz{|}~§±×÷
*
*/
class Base256 : BaseCodec {
companion object {
val code = arrayOf(
'!',
'#',
'$',
'%',
'&',
'(',
')',
'*',
'+',
',',
'-',
'.',
'/',
'0',
'1',
'2',
'3',
'4',
'5',
'6',
'7',
'8',
'9',
':',
';',
'<',
'=',
'>',
'?',
'@',
'A',
'B',
'C',
'D',
'E',
'F',
'G',
'H',
'I',
'J',
'K',
'L',
'M',
'N',
'O',
'P',
'Q',
'R',
'S',
'T',
'U',
'V',
'W',
'X',
'Y',
'Z',
'[',
']',
'^',
'_',
'`',
'a',
'b',
'c',
'd',
'e',
'f',
'g',
'h',
'i',
'j',
'k',
'l',
'm',
'n',
'o',
'p',
'q',
'r',
's',
't',
'u',
'v',
'w',
'x',
'y',
'z',
'{',
'|',
'}',
'~',
'§',
'±',
'×',
'÷',
'←',
'↑',
'→',
'↓',
'⇒',
'⇔',
'∀',
'∃',
'∅',
'∆',
'∇',
'∈',
'∉',
'∋',
'∌',
'∏',
'∑',
'',
'∓',
'',
'',
'∘',
'∙',
'√',
'∛',
'∜',
'∞',
'∟',
'∠',
'',
'∥',
'∧',
'',
'∩',
'',
'∫',
'∬',
'∭',
'∮',
'∯',
'∰',
'∱',
'∲',
'∳',
'∴',
'∵',
'',
'∷',
'≈',
'≠',
'≡',
'≤',
'≥',
'≦',
'≧',
'≪',
'≫',
'≺',
'≻',
'⊂',
'⊃',
'⊆',
'⊇',
'⊈',
'⊉',
'⊊',
'⊋',
'⊕',
'⊖',
'⊗',
'⊘',
'⊙',
'⊚',
'⊛',
'⊜',
'⊝',
'⊞',
'⊟',
'⊠',
'⊡',
'⊢',
'⊣',
'',
'⊥',
'⊦',
'⊧',
'⊨',
'⊩',
'⊪',
'⊫',
'⊬',
'⊭',
'⊮',
'⊯',
'⋀',
'',
'⋂',
'',
'⋄',
'⋅',
'⋆',
'⋇',
'⋈',
'⋉',
'⋊',
'⋋',
'⋌',
'⋍',
'⋎',
'⋏',
'⋐',
'⋑',
'⋒',
'⋓',
'⋔',
'⋕',
'⋖',
'⋗',
'⋘',
'⋙',
'⋚',
'⋛',
'⋜',
'⋝',
'⋞',
'⋟',
'⋠',
'⋡',
'⋢',
'⋣',
'⋤',
'⋥',
'⋦',
'⋧',
'⋨',
'⋩',
'▁',
'▂',
'▃',
'▄',
'▅',
'▆',
'▇',
'█',
'▉',
'▊',
'▋',
'▌',
'▍',
'▎',
'▏',
'▐',
'░',
'▒',
'▓',
'▔',
'▕',
'▖',
'▗',
'▘',
'▙'
)
val codeMap = code.mapIndexed { index, c -> c to index }.toMap()
}
override fun encode(bytes: ByteArray): String {
val result = CharArray(bytes.size)
for (i in bytes.indices) {
val unsignedByte = bytes[i].toInt() and 0xFF
result[i] = code[unsignedByte]
}
return String(result)
}
override fun decode(string: String): ByteArray {
val result = ByteArray(string.length)
for (i in string.indices) {
result[i] = codeMap[string[i]]!!.toByte()
}
return result
}
}
fun main() {
}

View File

@ -16,14 +16,12 @@
* ProjectName mingli-utils
* ModuleName mingli-utils.main
* CurrentFile Base91.kt
* LastUpdate 2025-09-17 10:57:36
* LastUpdate 2025-09-18 14:42:59
* UpdateUser MingLiPro
*/
package com.mingliqiye.utils.base
import java.util.*
/**
* Base91 编解码工具类用于将字节数组编码为 Base91 字符串或将 Base91 字符串解码为原始字节数组
*
@ -49,16 +47,264 @@ class Base91 : BaseCodec {
* Base91 解码表大小为 256用于快速查找字符对应的数值
* 初始化时将所有元素设为 -1表示无效字符然后根据 ENCODING_TABLE 填充有效字符的索引
*/
val DECODING_TABLE: IntArray = IntArray(256)
init {
// 初始化解码表,默认值为 -1 表示该字符不在编码表中
Arrays.fill(DECODING_TABLE, -1);
// 构建解码映射表
for (i in 0..<ENCODING_TABLE.size) {
DECODING_TABLE[ENCODING_TABLE[i].code] = i;
}
}
val DECODING_TABLE: Array<Int> = arrayOf(
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
62,
90,
63,
64,
65,
66,
-1,
67,
68,
69,
70,
71,
-1,
72,
73,
52,
53,
54,
55,
56,
57,
58,
59,
60,
61,
74,
75,
76,
77,
78,
79,
80,
0,
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,
81,
-1,
82,
83,
84,
85,
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,
86,
87,
88,
89,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1
)
}
/**
@ -156,3 +402,10 @@ class Base91 : BaseCodec {
return buffer.copyOf(index)
}
}
fun main() {
val base91 = Base91()
val bytes = "Hello, World!".toByteArray()
val encoded = base91.encode(bytes)
println(encoded)
}

View File

@ -16,7 +16,7 @@
* ProjectName mingli-utils
* ModuleName mingli-utils.main
* CurrentFile BaseCodec.kt
* LastUpdate 2025-09-17 10:35:23
* LastUpdate 2025-09-18 14:07:35
* UpdateUser MingLiPro
*/
@ -150,4 +150,24 @@ interface BaseCodec {
false
}
}
/**
* 将字符串编码为Base64字符串
*
* @param string 需要编码的字符串
* @return 编码后的Base64字符串
*/
fun encode(string: String): String {
return encode(string.toByteArray())
}
/**
* 将Base64字符串解码为字符串
*
* @param string 需要解码的Base64字符串
* @return 解码后的字符串
*/
fun decodetoString(string: String): String {
return decode(string).toString(Charsets.UTF_8)
}
}

View File

@ -16,7 +16,7 @@
* ProjectName mingli-utils
* ModuleName mingli-utils.main
* CurrentFile BaseUtils.kt
* LastUpdate 2025-09-17 10:54:46
* LastUpdate 2025-09-18 14:36:40
* UpdateUser MingLiPro
*/
@ -51,4 +51,12 @@ val BASE16: BaseCodec by lazy {
Base16()
}
/**
* Base256编解码器实例
* 使用懒加载方式初始化Base256编解码器对象
* 保证线程安全且只在首次访问时创建实例
*/
val BASE256: BaseCodec by lazy {
Base256()
}

View File

@ -16,7 +16,7 @@
* ProjectName mingli-utils
* ModuleName mingli-utils.main
* CurrentFile BCrypt.kt
* LastUpdate 2025-09-17 16:20:30
* LastUpdate 2025-09-18 09:46:16
* UpdateUser MingLiPro
*/

View File

@ -16,7 +16,7 @@
* ProjectName mingli-utils
* ModuleName mingli-utils.main
* CurrentFile Loggers.kt
* LastUpdate 2025-09-15 22:32:50
* LastUpdate 2025-09-18 09:30:48
* UpdateUser MingLiPro
*/
@ -416,6 +416,6 @@ class MingLiLoggerFactory {
}
}
val mingLiLoggerFactory = MingLiLoggerFactory()
val mingLiLoggerFactory: MingLiLoggerFactory by lazy { MingLiLoggerFactory() }

View File

@ -16,7 +16,7 @@
* ProjectName mingli-utils
* ModuleName mingli-utils.main
* CurrentFile OsPath.kt
* LastUpdate 2025-09-15 08:59:15
* LastUpdate 2025-09-18 09:47:43
* UpdateUser MingLiPro
*/
@ -26,9 +26,8 @@ package com.mingliqiye.utils.path
import java.io.File
import java.net.URI
import java.nio.file.*
import java.util.*
import java.util.function.Consumer
import java.nio.file.Path
import java.nio.file.Paths
class OsPath private constructor(private val path: Path) : Path by path {
@ -58,48 +57,4 @@ class OsPath private constructor(private val path: Path) : Path by path {
return OsPath(Paths.get(""))
}
}
override fun getParent(): Path? {
var parent = path.parent
if (parent == null) {
parent = path.toAbsolutePath().parent
}
return parent
}
override fun toRealPath(vararg options: LinkOption): Path {
return OsPath(path.toRealPath(*options))
}
override fun register(watcher: WatchService, vararg events: WatchEvent.Kind<*>): WatchKey {
return path.register(watcher, *events)
}
override fun register(
watcher: WatchService,
events: Array<out WatchEvent.Kind<*>>,
vararg modifiers: WatchEvent.Modifier
): WatchKey {
return path.register(watcher, events, *modifiers)
}
override fun iterator(): MutableIterator<Path> {
return path.iterator()
}
override fun compareTo(other: Path): Int {
return path.compareTo(other)
}
override fun toString(): String {
return path.toString()
}
override fun forEach(action: Consumer<in Path>) {
path.forEach(action)
}
override fun spliterator(): Spliterator<Path> {
return path.spliterator()
}
}

View File

@ -16,12 +16,13 @@
* ProjectName mingli-utils
* ModuleName mingli-utils.main
* CurrentFile UUID.kt
* LastUpdate 2025-09-17 16:27:32
* LastUpdate 2025-09-18 14:39:00
* UpdateUser MingLiPro
*/
package com.mingliqiye.utils.uuid
import com.mingliqiye.utils.base.BASE256
import com.mingliqiye.utils.base.BASE64
import com.mingliqiye.utils.base.BASE91
import com.mingliqiye.utils.random.randomByteSecure
@ -66,6 +67,11 @@ class UUID : Serializable {
return UUID(BASE64.decode(baseShortString))
}
@JvmStatic
fun ofBase256ShortString(baseShortString: String): UUID {
return UUID(BASE256.decode(baseShortString))
}
@JvmStatic
fun ofBase91ShortString(baseShortString: String): UUID {
return UUID(BASE91.decode(baseShortString))
@ -656,6 +662,10 @@ class UUID : Serializable {
return BASE91.encode(data)
}
fun getBase256ShortString(): String {
return BASE256.encode(data)
}
/**
* 提取 UUID V1 中的 MAC 地址
*