generated from mingliqiye/lib-tem
更新了Base91解码表的初始化方式,并添加了一个示例方法来演示编码和解码功能。 feat(Base256): 添加Base256编解码器 新增了Base256类,实现了Base256编解码功能,并在BaseUtils中添加了相应的实例。 refactor(BaseCodec): 增加字符串编解码方法 在BaseCodec接口中增加了对字符串进行Base64编码和解码的方法。refactor(OsPath): 简化路径处理逻辑 移除了OsPath类中不必要的方法实现,简化了路径处理逻辑。 feat(UUID): 支持Base256短字符串 在UUID类中添加了对Base256短字符串的支持,包括编码和解码方法。
174 lines
4.8 KiB
Kotlin
174 lines
4.8 KiB
Kotlin
/*
|
||
* 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 BaseCodec.kt
|
||
* LastUpdate 2025-09-18 14:07:35
|
||
* UpdateUser MingLiPro
|
||
*/
|
||
|
||
package com.mingliqiye.utils.base
|
||
|
||
import java.io.File
|
||
import java.io.IOException
|
||
import java.nio.file.Path
|
||
|
||
interface BaseCodec {
|
||
/**
|
||
* 将字节数组编码为Base64字符串
|
||
*
|
||
* @param bytes 需要编码的字节数组
|
||
* @return 编码后的Base64字符串
|
||
*/
|
||
fun encode(bytes: ByteArray): String
|
||
|
||
/**
|
||
* 将Base64字符串解码为字节数组
|
||
*
|
||
* @param string 需要解码的Base64字符串
|
||
* @return 解码后的字节数组
|
||
*/
|
||
fun decode(string: String): ByteArray
|
||
|
||
/**
|
||
* 将文件内容编码为Base64字符串
|
||
*
|
||
* @param file 需要编码的文件
|
||
* @return 文件内容的Base64编码字符串
|
||
* @throws IOException 当文件读取失败时抛出
|
||
*/
|
||
@Throws(IOException::class)
|
||
fun encode(file: File): String {
|
||
return encode(file.readBytes())
|
||
}
|
||
|
||
/**
|
||
* 将Base64字符串解码并写入文件
|
||
*
|
||
* @param file 目标文件
|
||
* @param string 需要解码的Base64字符串
|
||
* @throws IOException 当文件写入失败时抛出
|
||
*/
|
||
@Throws(IOException::class)
|
||
fun decode(file: File, string: String) {
|
||
file.writeBytes(decode(string))
|
||
}
|
||
|
||
/**
|
||
* 安全地将文件内容编码为Base64字符串,出现异常时返回null
|
||
*
|
||
* @param file 需要编码的文件
|
||
* @return 文件内容的Base64编码字符串,失败时返回null
|
||
*/
|
||
fun encodeSafe(file: File): String? {
|
||
return try {
|
||
encode(file)
|
||
} catch (_: Exception) {
|
||
null
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 安全地将Base64字符串解码并写入文件,返回操作是否成功
|
||
*
|
||
* @param file 目标文件
|
||
* @param string 需要解码的Base64字符串
|
||
* @return 操作成功返回true,失败返回false
|
||
*/
|
||
fun decodeSafe(file: File, string: String): Boolean {
|
||
return try {
|
||
decode(file, string)
|
||
true
|
||
} catch (_: Exception) {
|
||
false
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 将路径对应的文件内容编码为Base64字符串
|
||
*
|
||
* @param path 需要编码的文件路径
|
||
* @return 文件内容的Base64编码字符串
|
||
* @throws IOException 当文件读取失败时抛出
|
||
*/
|
||
@Throws(IOException::class)
|
||
fun encode(path: Path): String {
|
||
return encode(path.toFile().readBytes())
|
||
}
|
||
|
||
/**
|
||
* 将Base64字符串解码并写入路径指定的文件
|
||
*
|
||
* @param path 目标文件路径
|
||
* @param string 需要解码的Base64字符串
|
||
* @throws IOException 当文件写入失败时抛出
|
||
*/
|
||
@Throws(IOException::class)
|
||
fun decode(path: Path, string: String) {
|
||
path.toFile().writeBytes(decode(string))
|
||
}
|
||
|
||
/**
|
||
* 安全地将路径对应的文件内容编码为Base64字符串,出现异常时返回null
|
||
*
|
||
* @param path 需要编码的文件路径
|
||
* @return 文件内容的Base64编码字符串,失败时返回null
|
||
*/
|
||
fun encodeSafe(path: Path): String? {
|
||
return try {
|
||
encode(path)
|
||
} catch (_: Exception) {
|
||
null
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 安全地将Base64字符串解码并写入路径指定的文件,返回操作是否成功
|
||
*
|
||
* @param path 目标文件路径
|
||
* @param string 需要解码的Base64字符串
|
||
* @return 操作成功返回true,失败返回false
|
||
*/
|
||
fun decodeSafe(path: Path, string: String): Boolean {
|
||
return try {
|
||
decode(path, string)
|
||
true
|
||
} catch (_: Exception) {
|
||
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)
|
||
}
|
||
}
|