@@ -16,7 +16,7 @@
|
||||
* ProjectName mingli-utils
|
||||
* ModuleName mingli-utils.main
|
||||
* CurrentFile BaseCodec.kt
|
||||
* LastUpdate 2026-02-09 19:58:29
|
||||
* LastUpdate 2026-10-11 16:52:16
|
||||
* UpdateUser MingLiPro
|
||||
*/
|
||||
|
||||
@@ -43,7 +43,7 @@ interface BaseCodec {
|
||||
fun catchEncode(bytes: ByteArray) = try {
|
||||
encode(bytes)
|
||||
} catch (e: Exception) {
|
||||
throw CodecException(e)
|
||||
throw CodecException(throwable = e)
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -56,7 +56,7 @@ interface BaseCodec {
|
||||
fun catchDecode(string: String) = try {
|
||||
decode(string)
|
||||
} catch (e: Exception) {
|
||||
throw CodecException(e)
|
||||
throw CodecException(throwable = e)
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -16,7 +16,7 @@
|
||||
* ProjectName mingli-utils
|
||||
* ModuleName mingli-utils.main
|
||||
* CurrentFile ByteBufferUtils.kt
|
||||
* LastUpdate 2026-03-11 08:46:40
|
||||
* LastUpdate 2026-07-02 09:31:01
|
||||
* UpdateUser MingLiPro
|
||||
*/
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2025 mingliqiye
|
||||
* Copyright 2026 mingliqiye
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -16,7 +16,7 @@
|
||||
* ProjectName mingli-utils
|
||||
* ModuleName mingli-utils.main
|
||||
* CurrentFile ConfigurationProp.kt
|
||||
* LastUpdate 2025-09-19 11:30:04
|
||||
* LastUpdate 2026-07-01 12:37:05
|
||||
* UpdateUser MingLiPro
|
||||
*/
|
||||
|
||||
@@ -156,7 +156,7 @@ open class ConfigurationProps {
|
||||
private fun <T : ConfigurationProps> processFields(
|
||||
clazz: Class<T>,
|
||||
instance: T,
|
||||
mapsArgs: Map<String, List<String>>
|
||||
mapsArgs: Map<String, List<String>>,
|
||||
) {
|
||||
val fields = clazz.declaredFields
|
||||
|
||||
@@ -194,7 +194,7 @@ open class ConfigurationProps {
|
||||
private fun convertValue(
|
||||
type: Class<*>,
|
||||
values: List<String>,
|
||||
annotation: ConfigurationProp
|
||||
annotation: ConfigurationProp,
|
||||
): Any? {
|
||||
val lastValue = values.lastOrNull() ?: return null
|
||||
|
||||
@@ -248,9 +248,9 @@ open class ConfigurationProps {
|
||||
val configurationProp = field.getAnnotation(ConfigurationProp::class.java)
|
||||
if (configurationProp != null && configurationProp.showHelper) {
|
||||
val fieldName = configurationProp.name.ifEmpty { field.name }
|
||||
help.append("$fieldName -> 类型: ${field.type.simpleName}")
|
||||
help.append("$fieldName -> Type: ${field.type.simpleName}")
|
||||
if (configurationProp.description.isNotEmpty()) {
|
||||
help.append(" 描述: ${configurationProp.description}")
|
||||
help.append(" Description: ${configurationProp.description}")
|
||||
}
|
||||
help.append("\n")
|
||||
}
|
||||
|
||||
@@ -16,21 +16,15 @@
|
||||
* ProjectName mingli-utils
|
||||
* ModuleName mingli-utils.main
|
||||
* CurrentFile CodecException.kt
|
||||
* LastUpdate 2026-02-09 19:52:21
|
||||
* LastUpdate 2026-10-11 16:51:36
|
||||
* UpdateUser MingLiPro
|
||||
*/
|
||||
|
||||
package com.mingliqiye.utils.exception
|
||||
|
||||
class CodecException : MingLiUtilsBaseException {
|
||||
constructor()
|
||||
constructor(message: String) : super(message)
|
||||
constructor(throwable: Throwable) : super(throwable)
|
||||
constructor(message: String, throwable: Throwable) : super(message, throwable)
|
||||
constructor(
|
||||
message: String? = null,
|
||||
throwable: Throwable? = null,
|
||||
enableSuppression: Boolean = false,
|
||||
writableStackTrace: Boolean = false
|
||||
) : super(message, throwable, enableSuppression, writableStackTrace)
|
||||
}
|
||||
class CodecException @JvmOverloads constructor(
|
||||
message: String? = null,
|
||||
throwable: Throwable? = null,
|
||||
enableSuppression: Boolean = false,
|
||||
writableStackTrace: Boolean = true,
|
||||
) : MingLiUtilsBaseException(message, throwable, enableSuppression, writableStackTrace)
|
||||
|
||||
@@ -16,21 +16,15 @@
|
||||
* ProjectName mingli-utils
|
||||
* ModuleName mingli-utils.main
|
||||
* CurrentFile EmptyJsonException.kt
|
||||
* LastUpdate 2026-02-07 14:45:12
|
||||
* LastUpdate 2026-10-11 16:53:26
|
||||
* UpdateUser MingLiPro
|
||||
*/
|
||||
|
||||
package com.mingliqiye.utils.exception
|
||||
|
||||
class EmptyJsonException : JsonException {
|
||||
constructor()
|
||||
constructor(message: String) : super(message)
|
||||
constructor(throwable: Throwable) : super(throwable)
|
||||
constructor(message: String, throwable: Throwable) : super(message, throwable)
|
||||
constructor(
|
||||
message: String? = null,
|
||||
throwable: Throwable? = null,
|
||||
enableSuppression: Boolean = false,
|
||||
writableStackTrace: Boolean = false
|
||||
) : super(message, throwable, enableSuppression, writableStackTrace)
|
||||
}
|
||||
class EmptyJsonException @JvmOverloads constructor(
|
||||
message: String? = null,
|
||||
throwable: Throwable? = null,
|
||||
enableSuppression: Boolean = false,
|
||||
writableStackTrace: Boolean = true,
|
||||
) : JsonException(message, throwable, enableSuppression, writableStackTrace)
|
||||
|
||||
@@ -16,21 +16,15 @@
|
||||
* ProjectName mingli-utils
|
||||
* ModuleName mingli-utils.main
|
||||
* CurrentFile HttpException.kt
|
||||
* LastUpdate 2026-02-05 14:44:15
|
||||
* LastUpdate 2026-10-11 16:52:48
|
||||
* UpdateUser MingLiPro
|
||||
*/
|
||||
|
||||
package com.mingliqiye.utils.exception
|
||||
|
||||
open class HttpException : MingLiUtilsBaseException {
|
||||
constructor()
|
||||
constructor(message: String) : super(message)
|
||||
constructor(throwable: Throwable) : super(throwable)
|
||||
constructor(message: String, throwable: Throwable) : super(message, throwable)
|
||||
constructor(
|
||||
message: String? = null,
|
||||
throwable: Throwable? = null,
|
||||
enableSuppression: Boolean = false,
|
||||
writableStackTrace: Boolean = false
|
||||
) : super(message, throwable, enableSuppression, writableStackTrace)
|
||||
}
|
||||
open class HttpException @JvmOverloads constructor(
|
||||
message: String? = null,
|
||||
throwable: Throwable? = null,
|
||||
enableSuppression: Boolean = false,
|
||||
writableStackTrace: Boolean = true,
|
||||
) : MingLiUtilsBaseException(message, throwable, enableSuppression, writableStackTrace)
|
||||
|
||||
@@ -16,7 +16,7 @@
|
||||
* ProjectName mingli-utils
|
||||
* ModuleName mingli-utils.main
|
||||
* CurrentFile HttpStatusException.kt
|
||||
* LastUpdate 2026-02-10 01:12:53
|
||||
* LastUpdate 2026-10-11 17:02:06
|
||||
* UpdateUser MingLiPro
|
||||
*/
|
||||
|
||||
@@ -29,8 +29,10 @@ package com.mingliqiye.utils.exception
|
||||
* @param message 异常信息,默认为 null
|
||||
* @param cause 异常原因,默认为 null
|
||||
*/
|
||||
open class HttpStatusException(
|
||||
open val statusCode: Int,
|
||||
override val message: String? = null,
|
||||
override val cause: Throwable? = null
|
||||
) : MingLiUtilsBaseException(message, cause)
|
||||
open class HttpStatusException @JvmOverloads constructor(
|
||||
val statusCode: Int,
|
||||
message: String? = null,
|
||||
throwable: Throwable? = null,
|
||||
enableSuppression: Boolean = false,
|
||||
writableStackTrace: Boolean = true,
|
||||
) : MingLiUtilsBaseException(message, throwable, enableSuppression, writableStackTrace)
|
||||
|
||||
@@ -16,21 +16,15 @@
|
||||
* ProjectName mingli-utils
|
||||
* ModuleName mingli-utils.main
|
||||
* CurrentFile HttpUrlException.kt
|
||||
* LastUpdate 2026-03-11 08:46:40
|
||||
* LastUpdate 2026-10-11 16:53:00
|
||||
* UpdateUser MingLiPro
|
||||
*/
|
||||
|
||||
package com.mingliqiye.utils.exception
|
||||
|
||||
open class HttpUrlException : MingLiUtilsBaseException {
|
||||
constructor()
|
||||
constructor(message: String) : super(message)
|
||||
constructor(throwable: Throwable) : super(throwable)
|
||||
constructor(message: String, throwable: Throwable) : super(message, throwable)
|
||||
constructor(
|
||||
message: String? = null,
|
||||
throwable: Throwable? = null,
|
||||
enableSuppression: Boolean = false,
|
||||
writableStackTrace: Boolean = false,
|
||||
) : super(message, throwable, enableSuppression, writableStackTrace)
|
||||
}
|
||||
open class HttpUrlException @JvmOverloads constructor(
|
||||
message: String? = null,
|
||||
throwable: Throwable? = null,
|
||||
enableSuppression: Boolean = false,
|
||||
writableStackTrace: Boolean = true,
|
||||
) : MingLiUtilsBaseException(message, throwable, enableSuppression, writableStackTrace)
|
||||
|
||||
@@ -16,21 +16,15 @@
|
||||
* ProjectName mingli-utils
|
||||
* ModuleName mingli-utils.main
|
||||
* CurrentFile InvalidJsonException.kt
|
||||
* LastUpdate 2026-02-07 13:18:32
|
||||
* LastUpdate 2026-10-11 16:53:31
|
||||
* UpdateUser MingLiPro
|
||||
*/
|
||||
|
||||
package com.mingliqiye.utils.exception
|
||||
|
||||
class InvalidJsonException : JsonException {
|
||||
constructor()
|
||||
constructor(message: String) : super(message)
|
||||
constructor(throwable: Throwable) : super(throwable)
|
||||
constructor(message: String, throwable: Throwable) : super(message, throwable)
|
||||
constructor(
|
||||
message: String? = null,
|
||||
throwable: Throwable? = null,
|
||||
enableSuppression: Boolean = false,
|
||||
writableStackTrace: Boolean = false
|
||||
) : super(message, throwable, enableSuppression, writableStackTrace)
|
||||
}
|
||||
class InvalidJsonException @JvmOverloads constructor(
|
||||
message: String? = null,
|
||||
throwable: Throwable? = null,
|
||||
enableSuppression: Boolean = false,
|
||||
writableStackTrace: Boolean = true,
|
||||
) : JsonException(message, throwable, enableSuppression, writableStackTrace)
|
||||
|
||||
@@ -16,7 +16,7 @@
|
||||
* ProjectName mingli-utils
|
||||
* ModuleName mingli-utils.main
|
||||
* CurrentFile JsonException.kt
|
||||
* LastUpdate 2026-02-07 13:17:50
|
||||
* LastUpdate 2026-10-11 16:53:07
|
||||
* UpdateUser MingLiPro
|
||||
*/
|
||||
|
||||
@@ -30,15 +30,9 @@ package com.mingliqiye.utils.exception
|
||||
* 2. 包含错误信息和原因(Throwable)的构造函数。
|
||||
* 3. 仅包含原因(Throwable)的构造函数。
|
||||
*/
|
||||
open class JsonException : MingLiUtilsBaseException {
|
||||
constructor()
|
||||
constructor(message: String) : super(message)
|
||||
constructor(throwable: Throwable) : super(throwable)
|
||||
constructor(message: String, throwable: Throwable) : super(message, throwable)
|
||||
constructor(
|
||||
message: String? = null,
|
||||
throwable: Throwable? = null,
|
||||
enableSuppression: Boolean = false,
|
||||
writableStackTrace: Boolean = false
|
||||
) : super(message, throwable, enableSuppression, writableStackTrace)
|
||||
}
|
||||
open class JsonException @JvmOverloads constructor(
|
||||
message: String? = null,
|
||||
throwable: Throwable? = null,
|
||||
enableSuppression: Boolean = false,
|
||||
writableStackTrace: Boolean = true,
|
||||
) : MingLiUtilsBaseException(message, throwable, enableSuppression, writableStackTrace)
|
||||
|
||||
@@ -16,21 +16,15 @@
|
||||
* ProjectName mingli-utils
|
||||
* ModuleName mingli-utils.main
|
||||
* CurrentFile JsonObjectCastException.kt
|
||||
* LastUpdate 2026-02-09 19:52:21
|
||||
* LastUpdate 2026-10-11 16:53:36
|
||||
* UpdateUser MingLiPro
|
||||
*/
|
||||
|
||||
package com.mingliqiye.utils.exception
|
||||
|
||||
class JsonObjectCastException : JsonException {
|
||||
constructor()
|
||||
constructor(message: String) : super(message)
|
||||
constructor(throwable: Throwable) : super(throwable)
|
||||
constructor(message: String, throwable: Throwable) : super(message, throwable)
|
||||
constructor(
|
||||
message: String? = null,
|
||||
throwable: Throwable? = null,
|
||||
enableSuppression: Boolean = false,
|
||||
writableStackTrace: Boolean = false
|
||||
) : super(message, throwable, enableSuppression, writableStackTrace)
|
||||
}
|
||||
class JsonObjectCastException @JvmOverloads constructor(
|
||||
message: String? = null,
|
||||
throwable: Throwable? = null,
|
||||
enableSuppression: Boolean = false,
|
||||
writableStackTrace: Boolean = true,
|
||||
) : JsonException(message, throwable, enableSuppression, writableStackTrace)
|
||||
|
||||
@@ -16,21 +16,15 @@
|
||||
* ProjectName mingli-utils
|
||||
* ModuleName mingli-utils.main
|
||||
* CurrentFile JsonParserException.kt
|
||||
* LastUpdate 2026-02-07 14:45:12
|
||||
* LastUpdate 2026-10-11 16:53:41
|
||||
* UpdateUser MingLiPro
|
||||
*/
|
||||
|
||||
package com.mingliqiye.utils.exception
|
||||
|
||||
class JsonParserException : JsonException {
|
||||
constructor()
|
||||
constructor(message: String) : super(message)
|
||||
constructor(throwable: Throwable) : super(throwable)
|
||||
constructor(message: String, throwable: Throwable) : super(message, throwable)
|
||||
constructor(
|
||||
message: String? = null,
|
||||
throwable: Throwable? = null,
|
||||
enableSuppression: Boolean = false,
|
||||
writableStackTrace: Boolean = false
|
||||
) : super(message, throwable, enableSuppression, writableStackTrace)
|
||||
}
|
||||
class JsonParserException @JvmOverloads constructor(
|
||||
message: String? = null,
|
||||
throwable: Throwable? = null,
|
||||
enableSuppression: Boolean = false,
|
||||
writableStackTrace: Boolean = true,
|
||||
) : JsonException(message, throwable, enableSuppression, writableStackTrace)
|
||||
|
||||
@@ -16,21 +16,15 @@
|
||||
* ProjectName mingli-utils
|
||||
* ModuleName mingli-utils.main
|
||||
* CurrentFile JsonTypeException.kt
|
||||
* LastUpdate 2026-02-07 13:28:02
|
||||
* LastUpdate 2026-10-11 16:53:45
|
||||
* UpdateUser MingLiPro
|
||||
*/
|
||||
|
||||
package com.mingliqiye.utils.exception
|
||||
|
||||
class JsonTypeException : JsonException {
|
||||
constructor()
|
||||
constructor(message: String) : super(message)
|
||||
constructor(throwable: Throwable) : super(throwable)
|
||||
constructor(message: String, throwable: Throwable) : super(message, throwable)
|
||||
constructor(
|
||||
message: String? = null,
|
||||
throwable: Throwable? = null,
|
||||
enableSuppression: Boolean = false,
|
||||
writableStackTrace: Boolean = false
|
||||
) : super(message, throwable, enableSuppression, writableStackTrace)
|
||||
}
|
||||
class JsonTypeException @JvmOverloads constructor(
|
||||
message: String? = null,
|
||||
throwable: Throwable? = null,
|
||||
enableSuppression: Boolean = false,
|
||||
writableStackTrace: Boolean = true,
|
||||
) : JsonException(message, throwable, enableSuppression, writableStackTrace)
|
||||
|
||||
@@ -16,22 +16,15 @@
|
||||
* ProjectName mingli-utils
|
||||
* ModuleName mingli-utils.main
|
||||
* CurrentFile MingLiUtilsBaseException.kt
|
||||
* LastUpdate 2026-02-05 14:41:27
|
||||
* LastUpdate 2026-10-11 16:39:21
|
||||
* UpdateUser MingLiPro
|
||||
*/
|
||||
|
||||
package com.mingliqiye.utils.exception
|
||||
|
||||
open class MingLiUtilsBaseException : RuntimeException {
|
||||
|
||||
constructor()
|
||||
constructor(message: String) : super(message)
|
||||
constructor(throwable: Throwable) : super(throwable)
|
||||
constructor(message: String, throwable: Throwable) : super(message, throwable)
|
||||
constructor(
|
||||
message: String? = null,
|
||||
throwable: Throwable? = null,
|
||||
enableSuppression: Boolean = false,
|
||||
writableStackTrace: Boolean = false
|
||||
) : super(message, throwable, enableSuppression, writableStackTrace)
|
||||
}
|
||||
open class MingLiUtilsBaseException @JvmOverloads constructor(
|
||||
message: String? = null,
|
||||
cause: Throwable? = null,
|
||||
enableSuppression: Boolean = false,
|
||||
writableStackTrace: Boolean = true,
|
||||
) : RuntimeException(message, cause, enableSuppression, writableStackTrace)
|
||||
|
||||
@@ -16,21 +16,15 @@
|
||||
* ProjectName mingli-utils
|
||||
* ModuleName mingli-utils.main
|
||||
* CurrentFile NotFoundResourceException.kt
|
||||
* LastUpdate 2026-05-22 13:20:00
|
||||
* LastUpdate 2026-10-11 16:53:58
|
||||
* UpdateUser MingLiPro
|
||||
*/
|
||||
|
||||
package com.mingliqiye.utils.exception
|
||||
|
||||
class NotFoundResourceException : MingLiUtilsBaseException {
|
||||
constructor()
|
||||
constructor(message: String) : super(message)
|
||||
constructor(throwable: Throwable) : super(throwable)
|
||||
constructor(message: String, throwable: Throwable) : super(message, throwable)
|
||||
constructor(
|
||||
message: String? = null,
|
||||
throwable: Throwable? = null,
|
||||
enableSuppression: Boolean = false,
|
||||
writableStackTrace: Boolean = false,
|
||||
) : super(message, throwable, enableSuppression, writableStackTrace)
|
||||
}
|
||||
class NotFoundResourceException @JvmOverloads constructor(
|
||||
message: String? = null,
|
||||
throwable: Throwable? = null,
|
||||
enableSuppression: Boolean = false,
|
||||
writableStackTrace: Boolean = true,
|
||||
) : MingLiUtilsBaseException(message, throwable, enableSuppression, writableStackTrace)
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2025 mingliqiye
|
||||
* Copyright 2026 mingliqiye
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -16,7 +16,7 @@
|
||||
* ProjectName mingli-utils
|
||||
* ModuleName mingli-utils.main
|
||||
* CurrentFile FieldStructure.kt
|
||||
* LastUpdate 2025-09-15 22:32:50
|
||||
* LastUpdate 2026-07-02 10:22:42
|
||||
* UpdateUser MingLiPro
|
||||
*/
|
||||
|
||||
@@ -30,7 +30,7 @@ import java.lang.reflect.Modifier
|
||||
* 该类继承自JNA的Structure类,通过反射自动获取子类的公共非静态字段,
|
||||
* 并按声明顺序返回字段名列表,用于JNA结构体的字段映射。
|
||||
*/
|
||||
class FieldStructure : Structure() {
|
||||
open class FieldStructure : Structure() {
|
||||
/**
|
||||
* 获取结构体字段顺序列表
|
||||
* 通过反射遍历当前类及其父类的所有声明字段,过滤出公共非静态字段,
|
||||
|
||||
@@ -16,7 +16,7 @@
|
||||
* ProjectName mingli-utils
|
||||
* ModuleName mingli-utils.main
|
||||
* CurrentFile JacksonJsonApi.kt
|
||||
* LastUpdate 2026-06-21 10:40:58
|
||||
* LastUpdate 2026-10-11 16:54:15
|
||||
* UpdateUser MingLiPro
|
||||
*/
|
||||
|
||||
@@ -117,7 +117,7 @@ class JacksonJsonApi : JsonApi {
|
||||
return try {
|
||||
objectMapper.writer().with(JsonGenerator.Feature.ESCAPE_NON_ASCII).writeValueAsString(obj)
|
||||
} catch (e: Exception) {
|
||||
throw JsonException(e)
|
||||
throw JsonException(throwable = e)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,74 @@
|
||||
/*
|
||||
* Copyright 2026 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 ByteArrayCodec.kt
|
||||
* LastUpdate 2026-06-27 21:43:25
|
||||
* UpdateUser MingLiPro
|
||||
*/
|
||||
|
||||
package com.mingliqiye.utils.netty
|
||||
|
||||
import io.netty.buffer.ByteBuf
|
||||
import io.netty.channel.ChannelHandlerContext
|
||||
|
||||
/**
|
||||
* ByteBuf 与 ByteArray 互转的编解码器
|
||||
*
|
||||
* 入站:将 Netty 的 ByteBuf 解码为 Kotlin 的 ByteArray
|
||||
* 出站:将 Kotlin 的 ByteArray 编码为 Netty 的 ByteBuf
|
||||
*
|
||||
* @see NettyBaseCodec
|
||||
*/
|
||||
class ByteArrayCodec : NettyBaseCodec<ByteBuf, ByteArray>() {
|
||||
|
||||
/**
|
||||
* 解码:ByteBuf → ByteArray
|
||||
*
|
||||
* 将 Netty 的 ByteBuf 转换为 Kotlin 的 ByteArray
|
||||
*
|
||||
* @param inData 输入的 ByteBuf
|
||||
* @param channelHandlerContext 上下文
|
||||
* @param outList 输出列表(未使用)
|
||||
* @return 转换后的 ByteArray
|
||||
*/
|
||||
override fun decode(
|
||||
inData: ByteBuf,
|
||||
channelHandlerContext: ChannelHandlerContext,
|
||||
outList: MutableList<Any>,
|
||||
): ByteArray {
|
||||
return inData.toByteArray()
|
||||
}
|
||||
|
||||
/**
|
||||
* 编码:ByteArray → ByteBuf
|
||||
*
|
||||
* 将 Kotlin 的 ByteArray 转换为 Netty 的 ByteBuf
|
||||
*
|
||||
* @param inData 输入的 ByteArray
|
||||
* @param channelHandlerContext 上下文
|
||||
* @param outList 输出列表(未使用)
|
||||
* @return 转换后的 ByteBuf
|
||||
*
|
||||
*/
|
||||
override fun encode(
|
||||
inData: ByteArray,
|
||||
channelHandlerContext: ChannelHandlerContext,
|
||||
outList: MutableList<Any>,
|
||||
): ByteBuf {
|
||||
return inData.toByteBuf()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,116 @@
|
||||
/*
|
||||
* Copyright 2026 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 NettyBaseCodec.kt
|
||||
* LastUpdate 2026-06-27 21:50:22
|
||||
* UpdateUser MingLiPro
|
||||
*/
|
||||
|
||||
package com.mingliqiye.utils.netty
|
||||
|
||||
import io.netty.channel.ChannelHandlerContext
|
||||
import io.netty.handler.codec.MessageToMessageCodec
|
||||
|
||||
/**
|
||||
* 基础编解码器抽象类
|
||||
*
|
||||
* 同时处理入站解码(I → O)和出站编码(O → I)
|
||||
* 继承自 Netty 的 MessageToMessageCodec,用于消息类型之间的转换
|
||||
*
|
||||
* @param I 入站消息类型(解码前的类型)
|
||||
* @param O 出站消息类型(解码后的类型/编码前的类型)
|
||||
*
|
||||
* 使用示例:
|
||||
* ```
|
||||
* class StringToIntCodec : NettyBaseCodec<String, Int>() {
|
||||
* override fun decode(inData: String, ctx: ChannelHandlerContext, outList: MutableList<Any>): Int {
|
||||
* return inData.toInt()
|
||||
* }
|
||||
* override fun encode(inData: Int, ctx: ChannelHandlerContext, outList: MutableList<Any>): String {
|
||||
* return inData.toString()
|
||||
* }
|
||||
* }
|
||||
* ```
|
||||
*/
|
||||
abstract class NettyBaseCodec<I : Any, O : Any> : MessageToMessageCodec<I, O>() {
|
||||
|
||||
/**
|
||||
* 解码方法(Netty 框架回调)
|
||||
*
|
||||
* 将入站消息 I 转换为 O,并添加到输出列表
|
||||
*
|
||||
* @param ctx ChannelHandler 上下文,可用于获取 Channel 等信息
|
||||
* @param msg 入站消息(类型为 I)
|
||||
* @param out 输出列表,解码后的消息(类型为 O)将被添加到此列表
|
||||
*
|
||||
* @see decode 抽象方法,由子类实现具体的解码逻辑
|
||||
*/
|
||||
override fun decode(ctx: ChannelHandlerContext, msg: I, out: MutableList<Any>) {
|
||||
// 调用子类实现的抽象解码方法,将结果添加到输出列表
|
||||
out.add(decode(msg, ctx, out))
|
||||
}
|
||||
|
||||
/**
|
||||
* 编码方法(Netty 框架回调)
|
||||
*
|
||||
* 将出站消息 O 转换为 I,并添加到输出列表
|
||||
*
|
||||
* @param ctx ChannelHandler 上下文
|
||||
* @param msg 出站消息(类型为 O)
|
||||
* @param out 输出列表,编码后的消息(类型为 I)将被添加到此列表
|
||||
*
|
||||
* @see encode 抽象方法,由子类实现具体的编码逻辑
|
||||
*/
|
||||
override fun encode(ctx: ChannelHandlerContext, msg: O, out: MutableList<Any>) {
|
||||
// 调用子类实现的抽象编码方法,将结果添加到输出列表
|
||||
out.add(encode(msg, ctx, out))
|
||||
}
|
||||
|
||||
/**
|
||||
* 抽象解码方法
|
||||
*
|
||||
* 子类必须实现此方法,定义具体的解码逻辑
|
||||
* 将类型 I 的消息转换为类型 O
|
||||
*
|
||||
* @param inData 输入数据(类型 I),需要被解码
|
||||
* @param channelHandlerContext ChannelHandler 上下文
|
||||
* @param outList 输出列表,可用于在解码过程中添加多个消息(目前设计为单一返回值,outList 暂未使用)
|
||||
* @return 解码后的数据(类型 O)
|
||||
*/
|
||||
abstract fun decode(
|
||||
inData: I,
|
||||
channelHandlerContext: ChannelHandlerContext,
|
||||
outList: MutableList<Any>,
|
||||
): O
|
||||
|
||||
/**
|
||||
* 抽象编码方法
|
||||
*
|
||||
* 子类必须实现此方法,定义具体的编码逻辑
|
||||
* 将类型 O 的消息转换为类型 I
|
||||
*
|
||||
* @param inData 输入数据(类型 O),需要被编码
|
||||
* @param channelHandlerContext ChannelHandler 上下文
|
||||
* @param outList 输出列表,可用于在编码过程中添加多个消息(目前设计为单一返回值,outList 暂未使用)
|
||||
* @return 编码后的数据(类型 I)
|
||||
*/
|
||||
abstract fun encode(
|
||||
inData: O,
|
||||
channelHandlerContext: ChannelHandlerContext,
|
||||
outList: MutableList<Any>,
|
||||
): I
|
||||
}
|
||||
@@ -0,0 +1,114 @@
|
||||
/*
|
||||
* Copyright 2026 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 NettyBaseDecoder.kt
|
||||
* LastUpdate 2026-06-27 21:51:10
|
||||
* UpdateUser MingLiPro
|
||||
*/
|
||||
|
||||
package com.mingliqiye.utils.netty
|
||||
|
||||
import io.netty.channel.ChannelHandlerContext
|
||||
import io.netty.handler.codec.MessageToMessageDecoder
|
||||
|
||||
/**
|
||||
* Netty 基础解码器抽象类
|
||||
*
|
||||
* 用于将入站消息从类型 I 解码为类型 O
|
||||
* 继承自 Netty 的 MessageToMessageDecoder,适用于消息类型之间的转换
|
||||
*
|
||||
* 适用场景:
|
||||
* - 将一种业务对象转换为另一种业务对象
|
||||
* - 将字符串解析为 JSON 对象
|
||||
* - 将字节数组反序列化为 POJO
|
||||
*
|
||||
* @param I 输入消息类型(解码前的类型)
|
||||
* @param O 输出消息类型(解码后的类型)
|
||||
*
|
||||
* 使用示例:
|
||||
* ```
|
||||
* class StringToIntDecoder : NettyBaseDecoder<String, Int>() {
|
||||
* override fun decode(inData: String, ctx: ChannelHandlerContext, outList: MutableList<Any>): Int {
|
||||
* return inData.toInt()
|
||||
* }
|
||||
* }
|
||||
* ```
|
||||
*
|
||||
* @see io.netty.handler.codec.MessageToMessageDecoder
|
||||
* @see io.netty.handler.codec.ByteToMessageDecoder
|
||||
*/
|
||||
abstract class NettyBaseDecoder<I : Any, O : Any> : MessageToMessageDecoder<I>() {
|
||||
|
||||
/**
|
||||
* 解码方法(Netty 框架回调)
|
||||
*
|
||||
* 由 Netty 在收到入站消息时自动调用
|
||||
* 将入站消息 I 转换为 O,并添加到输出列表
|
||||
*
|
||||
* @param ctx ChannelHandler 上下文
|
||||
* - 可用于获取 Channel 信息
|
||||
* - 可用于触发后续事件
|
||||
* - 可用于获取 ChannelPipeline 引用
|
||||
* @param msg 入站消息(类型为 I)
|
||||
* - 由 Netty 从 Channel 中读取
|
||||
* - 可能是 ByteBuf、String 或其他类型
|
||||
* @param out 输出列表(类型为 MutableList<Any>)
|
||||
* - 解码后的消息(类型为 O)将被添加到此列表
|
||||
* - 列表中的消息会传递给下一个 InboundHandler
|
||||
* - 可以添加多个消息,实现一对多的转换
|
||||
*
|
||||
* @see #decode 抽象方法,由子类实现具体的解码逻辑
|
||||
*/
|
||||
override fun decode(ctx: ChannelHandlerContext, msg: I, out: MutableList<Any>) {
|
||||
// 调用子类实现的抽象解码方法,将结果添加到输出列表
|
||||
// 子类通过抽象方法返回解码后的对象,然后添加到 out 中
|
||||
out.add(decode(msg, ctx, out))
|
||||
}
|
||||
|
||||
/**
|
||||
* 抽象解码方法
|
||||
*
|
||||
* 子类必须实现此方法,定义从 I 到 O 的具体转换逻辑
|
||||
*
|
||||
* 实现注意事项:
|
||||
* 1. 此方法会在 Netty 的 EventLoop 线程中执行,避免阻塞操作
|
||||
* 2. 如果输入数据不完整,应该抛出异常或返回 null
|
||||
* 3. 如果 outList 可以接收多个消息,可以多次调用 outList.add()
|
||||
* 4. 注意管理 ByteBuf 的引用计数(如果 I 是 ByteBuf)
|
||||
*
|
||||
* @param inData 输入数据(类型 I),需要被解码
|
||||
* - 由调用方(Netty 框架)传入
|
||||
* - 不应为 null(泛型约束为 Any)
|
||||
* @param channelHandlerContext ChannelHandler 上下文
|
||||
* - 可用于获取 ChannelHandlerContext 相关信息
|
||||
* - 可用于在解码过程中触发额外操作
|
||||
* @param outList 输出列表
|
||||
* - 可用于在解码过程中添加多个消息
|
||||
* - 当前设计为单一返回值,outList 暂作为扩展预留
|
||||
* - 如果需要在解码过程中输出多个消息,可以直接操作此列表并返回 null
|
||||
* @return 解码后的数据(类型 O)
|
||||
* - 不能返回 null(泛型约束为 Any)
|
||||
* - 返回值会被自动添加到 outList 中
|
||||
*
|
||||
* @see #decode 包装方法,由框架调用
|
||||
*/
|
||||
abstract fun decode(
|
||||
inData: I,
|
||||
channelHandlerContext: ChannelHandlerContext,
|
||||
outList: MutableList<Any>,
|
||||
): O
|
||||
}
|
||||
@@ -0,0 +1,130 @@
|
||||
/*
|
||||
* Copyright 2026 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 NettyBaseEncoder.kt
|
||||
* LastUpdate 2026-06-27 21:51:10
|
||||
* UpdateUser MingLiPro
|
||||
*/
|
||||
|
||||
package com.mingliqiye.utils.netty
|
||||
|
||||
import io.netty.channel.ChannelHandlerContext
|
||||
import io.netty.handler.codec.MessageToMessageEncoder
|
||||
|
||||
/**
|
||||
* Netty 基础编码器抽象类
|
||||
*
|
||||
* 用于将出站消息从类型 I 编码为类型 O
|
||||
* 继承自 Netty 的 MessageToMessageEncoder,适用于消息类型之间的转换
|
||||
*
|
||||
* 适用场景:
|
||||
* - 将业务对象序列化为传输格式
|
||||
* - 将 JSON 对象转换为字符串
|
||||
* - 将 POJO 序列化为字节数组
|
||||
*
|
||||
* ⚠️ 注意:如果需要将对象编码为 ByteBuf,应使用 MessageToByteEncoder
|
||||
*
|
||||
* @param I 输入消息类型(编码前的类型)
|
||||
* @param O 输出消息类型(编码后的类型)
|
||||
*
|
||||
* 使用示例:
|
||||
* ```
|
||||
* class IntToStringEncoder : NettyBaseEncoder<Int, String>() {
|
||||
* override fun encode(inData: Int, ctx: ChannelHandlerContext, outList: MutableList<Any>): String {
|
||||
* return inData.toString()
|
||||
* }
|
||||
* }
|
||||
* ```
|
||||
*
|
||||
* @see io.netty.handler.codec.MessageToMessageEncoder
|
||||
* @see MessageToByteEncoder
|
||||
*/
|
||||
abstract class NettyBaseEncoder<I : Any, O : Any> : MessageToMessageEncoder<I>() {
|
||||
|
||||
/**
|
||||
* 编码方法(Netty 框架回调)
|
||||
*
|
||||
* 由 Netty 在发送出站消息时自动调用
|
||||
* 将出站消息 I 编码为 O,并添加到输出列表
|
||||
*
|
||||
* 执行流程:
|
||||
* 1. 用户调用 Channel.write(msg) 发送消息
|
||||
* 2. Netty 将消息传递给 Pipeline 中的 OutboundHandler
|
||||
* 3. 此方法被调用,执行编码逻辑
|
||||
* 4. 编码结果添加到 out 列表
|
||||
* 5. Netty 将 out 列表中的消息传递给下一个 OutboundHandler
|
||||
*
|
||||
* @param ctx ChannelHandler 上下文
|
||||
* - 可用于获取 Channel 信息
|
||||
* - 可用于触发后续写操作
|
||||
* - 可用于获取 ChannelPipeline 引用
|
||||
* @param msg 出站消息(类型为 I)
|
||||
* - 由业务层调用 write 方法时传入
|
||||
* - 可能是任意类型
|
||||
* @param out 输出列表(类型为 MutableList<Any>)
|
||||
* - 编码后的消息(类型为 O)将被添加到此列表
|
||||
* - 列表中的消息会传递给下一个 OutboundHandler
|
||||
* - 可以添加多个消息,实现一对多的转换
|
||||
*
|
||||
* @see #encode 抽象方法,由子类实现具体的编码逻辑
|
||||
*/
|
||||
override fun encode(ctx: ChannelHandlerContext, msg: I, out: MutableList<Any>) {
|
||||
// 调用子类实现的抽象编码方法,将结果添加到输出列表
|
||||
// 子类通过抽象方法返回编码后的对象,然后添加到 out 中
|
||||
out.add(encode(msg, ctx, out))
|
||||
}
|
||||
|
||||
/**
|
||||
* 抽象编码方法
|
||||
*
|
||||
* 子类必须实现此方法,定义从 I 到 O 的具体转换逻辑
|
||||
*
|
||||
* 实现注意事项:
|
||||
* 1. 此方法会在 Netty 的 EventLoop 线程中执行,避免阻塞操作
|
||||
* 2. 编码后的对象应该是线程安全的(可能会被多个线程访问)
|
||||
* 3. 如果 outList 可以接收多个消息,可以多次调用 outList.add()
|
||||
* 4. 如果 I 是 ByteBuf,注意引用计数管理
|
||||
* 5. 编码结果应该是完整的消息,不要依赖后续 handler 补充
|
||||
*
|
||||
* 性能建议:
|
||||
* - 尽量复用对象池中的资源(如 ByteBuf)
|
||||
* - 避免在编码过程中进行大量数据复制
|
||||
* - 考虑使用 DirectBuffer 减少内存拷贝
|
||||
*
|
||||
* @param inData 输入数据(类型 I),需要被编码
|
||||
* - 由调用方(业务层)传入
|
||||
* - 不应为 null(泛型约束为 Any)
|
||||
* @param channelHandlerContext ChannelHandler 上下文
|
||||
* - 可用于获取 ChannelHandlerContext 相关信息
|
||||
* - 可用于在编码过程中触发额外操作
|
||||
* - 可通过 ctx.alloc() 获取 ByteBufAllocator 分配内存
|
||||
* @param outList 输出列表
|
||||
* - 可用于在编码过程中添加多个消息
|
||||
* - 当前设计为单一返回值,outList 暂作为扩展预留
|
||||
* - 如果需要在编码过程中输出多个消息,可以直接操作此列表并返回 null
|
||||
* @return 编码后的数据(类型 O)
|
||||
* - 不能返回 null(泛型约束为 Any)
|
||||
* - 返回值会被自动添加到 outList 中
|
||||
*
|
||||
* @see #encode 包装方法,由框架调用
|
||||
*/
|
||||
abstract fun encode(
|
||||
inData: I,
|
||||
channelHandlerContext: ChannelHandlerContext,
|
||||
outList: MutableList<Any>,
|
||||
): O
|
||||
}
|
||||
@@ -16,7 +16,7 @@
|
||||
* ProjectName mingli-utils
|
||||
* ModuleName mingli-utils.main
|
||||
* CurrentFile NettyUtils.kt
|
||||
* LastUpdate 2026-06-20 22:18:39
|
||||
* LastUpdate 2026-06-27 21:40:00
|
||||
* UpdateUser MingLiPro
|
||||
*/
|
||||
|
||||
@@ -28,12 +28,11 @@ import com.mingliqiye.utils.functions.P1Function
|
||||
import io.netty.bootstrap.Bootstrap
|
||||
import io.netty.bootstrap.ServerBootstrap
|
||||
import io.netty.buffer.ByteBuf
|
||||
import io.netty.buffer.ByteBufUtil
|
||||
import io.netty.buffer.Unpooled
|
||||
import io.netty.channel.Channel
|
||||
import io.netty.channel.ChannelFuture
|
||||
import io.netty.channel.ChannelInitializer
|
||||
import io.netty.channel.ChannelPipeline
|
||||
import io.netty.util.concurrent.Future
|
||||
import java.nio.ByteBuffer
|
||||
import java.nio.charset.Charset
|
||||
|
||||
@@ -71,9 +70,7 @@ fun ByteBuf.toByteBuffer(): ByteBuffer {
|
||||
* @return ByteArray 转换后的字节数组,包含ByteBuf中所有可读的字节数据
|
||||
*/
|
||||
fun ByteBuf.toByteArray(): ByteArray {
|
||||
val array = ByteArray(this.readableBytes())
|
||||
this.readBytes(array)
|
||||
return array
|
||||
return ByteBufUtil.getBytes(this)
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -331,8 +328,3 @@ fun ByteBuf.tryRelease(): Boolean {
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
fun ChannelFuture.addChannelListener(
|
||||
listener: (Future<Channel>) -> Unit,
|
||||
) {
|
||||
}
|
||||
|
||||
@@ -0,0 +1,144 @@
|
||||
/*
|
||||
* Copyright 2026 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 ObjectPool.kt
|
||||
* LastUpdate 2026-10-02 16:19:05
|
||||
* UpdateUser MingLiPro
|
||||
*/
|
||||
|
||||
package com.mingliqiye.utils.pool
|
||||
|
||||
import com.mingliqiye.utils.require.Require.require
|
||||
import java.util.concurrent.ConcurrentLinkedQueue
|
||||
import java.util.concurrent.TimeUnit
|
||||
import java.util.concurrent.atomic.AtomicBoolean
|
||||
import java.util.concurrent.atomic.AtomicInteger
|
||||
import java.util.concurrent.locks.Condition
|
||||
import java.util.concurrent.locks.ReentrantLock
|
||||
import kotlin.concurrent.withLock
|
||||
|
||||
@Deprecated("未实现")
|
||||
abstract class ObjectPool<T>(
|
||||
private val maxSize: Int = Int.MAX_VALUE,
|
||||
private val minSize: Int = 0,
|
||||
private val initialSize: Int = 1,
|
||||
) : Pool<T> {
|
||||
|
||||
|
||||
private val initialCount = AtomicInteger(0)
|
||||
private val pool = ConcurrentLinkedQueue<ObjectPoolData<T>>()
|
||||
private val isClosed = AtomicBoolean(false)
|
||||
private val lock = ReentrantLock()
|
||||
private val notEmpty: Condition = lock.newCondition()
|
||||
|
||||
init {
|
||||
require(maxSize >= minSize)
|
||||
require(initialSize >= minSize)
|
||||
require(maxSize > 0)
|
||||
repeat(initialSize) {
|
||||
pool.add(init(initialCount.getAndIncrement()))
|
||||
}
|
||||
}
|
||||
|
||||
override fun close() {
|
||||
for (poolData in pool) {
|
||||
destroy(poolData)
|
||||
}
|
||||
}
|
||||
|
||||
override fun borrow(timeoutMillis: Long): ObjectPoolData<T> {
|
||||
lock.withLock {
|
||||
if (isClosed.get()) {
|
||||
throw IllegalStateException("Object pool closed")
|
||||
}
|
||||
pool.poll()?.let { return it }
|
||||
if (initialCount.get() < maxSize) {
|
||||
return init(initialCount.getAndIncrement())
|
||||
}
|
||||
|
||||
// 3. 池已满且无空闲,等待归还
|
||||
if (timeoutMillis <= 0) {
|
||||
// 无限等待
|
||||
while (pool.isEmpty()) {
|
||||
notEmpty.await() // 释放锁并等待
|
||||
}
|
||||
return pool.poll()
|
||||
}
|
||||
|
||||
// 有限时间等待
|
||||
var remainingNanos = TimeUnit.MILLISECONDS.toNanos(timeoutMillis)
|
||||
while (pool.isEmpty()) {
|
||||
if (remainingNanos <= 0) {
|
||||
throw IllegalStateException("Timeout waiting for object from pool")
|
||||
}
|
||||
remainingNanos = notEmpty.awaitNanos(remainingNanos)
|
||||
}
|
||||
return pool.poll()
|
||||
}
|
||||
}
|
||||
|
||||
override fun returnObject(obj: ObjectPoolData<T>) {
|
||||
lock.withLock {
|
||||
if (isClosed.get()) {
|
||||
throw InterruptedException("Object pool closed")
|
||||
}
|
||||
pool.add(obj)
|
||||
notEmpty.signal()
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
interface Pool<T> {
|
||||
fun close()
|
||||
fun borrow(timeoutMillis: Long): ObjectPoolData<T>
|
||||
fun borrow(): ObjectPoolData<T> = borrow(0)
|
||||
fun returnObject(obj: ObjectPoolData<T>)
|
||||
fun use(funs: (ObjectPoolData<T>) -> Unit) = use(0, funs)
|
||||
fun init(id: Int): ObjectPoolData<T>
|
||||
fun destroy(data: ObjectPoolData<T>)
|
||||
fun use(timeoutMillis: Long, funs: (ObjectPoolData<T>) -> Unit) {
|
||||
val i = borrow(timeoutMillis)
|
||||
funs(i)
|
||||
returnObject(i)
|
||||
}
|
||||
}
|
||||
|
||||
data class ObjectPoolData<T>(
|
||||
val obj: T,
|
||||
val objectPool: ObjectPool<T>,
|
||||
val id: Int = obj.hashCode(),
|
||||
val name: String = "",
|
||||
)
|
||||
|
||||
|
||||
class TheardPool : ObjectPool<Thread>() {
|
||||
override fun init(id: Int): ObjectPoolData<Thread> {
|
||||
return ObjectPoolData(
|
||||
Thread().apply {
|
||||
name = "TheardPool $id"
|
||||
},
|
||||
this
|
||||
)
|
||||
}
|
||||
|
||||
override fun destroy(data: ObjectPoolData<Thread>) {
|
||||
data.obj.interrupt()
|
||||
}
|
||||
|
||||
}
|
||||
@@ -16,7 +16,7 @@
|
||||
* ProjectName mingli-utils
|
||||
* ModuleName mingli-utils.main
|
||||
* CurrentFile RandomBytes.kt
|
||||
* LastUpdate 2026-03-27 14:18:30
|
||||
* LastUpdate 2026-10-11 17:11:11
|
||||
* UpdateUser MingLiPro
|
||||
*/
|
||||
package com.mingliqiye.utils.random
|
||||
@@ -221,7 +221,7 @@ object Random {
|
||||
*/
|
||||
@JvmStatic
|
||||
fun String.randomString(length: Int): String = String(CharArray(length) {
|
||||
this[nextInt(0..<this.length)]
|
||||
this[nextInt(indices)]
|
||||
})
|
||||
|
||||
/**
|
||||
@@ -239,63 +239,63 @@ object Random {
|
||||
* @return 数组中的随机元素
|
||||
*/
|
||||
@JvmStatic
|
||||
fun <T> Array<T>.randomItem(): T = this[nextInt(0..<this.size)]
|
||||
fun <T> Array<T>.randomItem(): T = this[nextInt(indices)]
|
||||
|
||||
/**
|
||||
* ByteArray类型的扩展函数 - 从字节数组中随机选择一个元素
|
||||
* @return 字节数组中的随机字节
|
||||
*/
|
||||
@JvmStatic
|
||||
fun ByteArray.randomItem(): Byte = this[nextInt(0..<this.size)]
|
||||
fun ByteArray.randomItem(): Byte = this[nextInt(indices)]
|
||||
|
||||
/**
|
||||
* IntArray类型的扩展函数 - 从整数数组中随机选择一个元素
|
||||
* @return 整数数组中的随机整数
|
||||
*/
|
||||
@JvmStatic
|
||||
fun IntArray.randomItem(): Int = this[nextInt(0..<this.size)]
|
||||
fun IntArray.randomItem(): Int = this[nextInt(indices)]
|
||||
|
||||
/**
|
||||
* LongArray类型的扩展函数 - 从长整数数组中随机选择一个元素
|
||||
* @return 长整数数组中的随机长整数
|
||||
*/
|
||||
@JvmStatic
|
||||
fun LongArray.randomItem(): Long = this[nextInt(0..<this.size)]
|
||||
fun LongArray.randomItem(): Long = this[nextInt(indices)]
|
||||
|
||||
/**
|
||||
* FloatArray类型的扩展函数 - 从浮点数数组中随机选择一个元素
|
||||
* @return 浮点数数组中的随机浮点数
|
||||
*/
|
||||
@JvmStatic
|
||||
fun FloatArray.randomItem(): Float = this[nextInt(0..<this.size)]
|
||||
fun FloatArray.randomItem(): Float = this[nextInt(indices)]
|
||||
|
||||
/**
|
||||
* DoubleArray类型的扩展函数 - 从双精度浮点数数组中随机选择一个元素
|
||||
* @return 双精度浮点数数组中的随机双精度浮点数
|
||||
*/
|
||||
@JvmStatic
|
||||
fun DoubleArray.randomItem(): Double = this[nextInt(0..<this.size)]
|
||||
fun DoubleArray.randomItem(): Double = this[nextInt(indices)]
|
||||
|
||||
/**
|
||||
* BooleanArray类型的扩展函数 - 从布尔数组中随机选择一个元素
|
||||
* @return 布尔数组中的随机布尔值
|
||||
*/
|
||||
@JvmStatic
|
||||
fun BooleanArray.randomItem(): Boolean = this[nextInt(0..<this.size)]
|
||||
fun BooleanArray.randomItem(): Boolean = this[nextInt(indices)]
|
||||
|
||||
/**
|
||||
* CharArray类型的扩展函数 - 从字符数组中随机选择一个元素
|
||||
* @return 字符数组中的随机字符
|
||||
*/
|
||||
@JvmStatic
|
||||
fun CharArray.randomItem(): Char = this[nextInt(0..<this.size)]
|
||||
fun CharArray.randomItem(): Char = this[nextInt(indices)]
|
||||
|
||||
/**
|
||||
* ShortArray类型的扩展函数 - 从短整数数组中随机选择一个元素
|
||||
* @return 短整数数组中的随机短整数
|
||||
*/
|
||||
@JvmStatic
|
||||
fun ShortArray.randomItem(): Short = this[nextInt(0..<this.size)]
|
||||
fun ShortArray.randomItem(): Short = this[nextInt(indices)]
|
||||
|
||||
/**
|
||||
* Collection类型的扩展函数 - 从集合中随机选择一个元素
|
||||
@@ -303,5 +303,5 @@ object Random {
|
||||
* @return 集合中的随机元素
|
||||
*/
|
||||
@JvmStatic
|
||||
fun <T> Collection<T>.randomItem(): T = this.elementAt(nextInt(0..<this.size))
|
||||
fun <T> Collection<T>.randomItem(): T = this.elementAt(nextInt(indices))
|
||||
}
|
||||
|
||||
@@ -16,14 +16,13 @@
|
||||
* ProjectName mingli-utils
|
||||
* ModuleName mingli-utils.main
|
||||
* CurrentFile ReflectUtils.kt
|
||||
* LastUpdate 2026-06-21 10:40:58
|
||||
* LastUpdate 2026-10-11 17:11:11
|
||||
* UpdateUser MingLiPro
|
||||
*/
|
||||
|
||||
package com.mingliqiye.utils.reflect
|
||||
|
||||
import com.mingliqiye.utils.io.SIO.ifOr
|
||||
import java.lang.reflect.AnnotatedElement
|
||||
import java.lang.reflect.Method
|
||||
import kotlin.reflect.KClass
|
||||
import kotlin.reflect.KFunction
|
||||
@@ -33,13 +32,15 @@ import kotlin.reflect.jvm.javaMethod
|
||||
|
||||
object ReflectUtils {
|
||||
private val walker: StackWalker = StackWalker.getInstance(StackWalker.Option.RETAIN_CLASS_REFERENCE)
|
||||
inline fun <reified T : Annotation> AnnotatedElement.getAnnotation() = this.getAnnotation(T::class.java)
|
||||
inline fun <reified T : Any> T.ktClass(): KClass<T> = T::class
|
||||
|
||||
var <T : Any> KMutableProperty<T>.value: T
|
||||
get() = this.getter.call()
|
||||
set(t: T) = this.setter.call(t)
|
||||
|
||||
val <T : Any> KProperty<T>.javaGetter: Method
|
||||
get() = this.getter.javaMethod!!
|
||||
|
||||
val <T : Any> KMutableProperty<T>.javaSetter: Method
|
||||
get() = this.getter.javaMethod!!
|
||||
val <T : Any> KFunction<T>.java: Method
|
||||
|
||||
+3
-1
@@ -16,7 +16,7 @@
|
||||
* ProjectName mingli-utils
|
||||
* ModuleName mingli-utils.main
|
||||
* CurrentFile Jackson3AutoConfiguration.kt
|
||||
* LastUpdate 2026-03-11 08:46:40
|
||||
* LastUpdate 2026-10-02 16:11:12
|
||||
* UpdateUser MingLiPro
|
||||
*/
|
||||
|
||||
@@ -28,6 +28,7 @@ import com.mingliqiye.utils.json.converters.base.getJackson3Module
|
||||
import com.mingliqiye.utils.logger.MingLiLoggerFactory
|
||||
import org.springframework.boot.autoconfigure.AutoConfigureAfter
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean
|
||||
import org.springframework.boot.jackson.autoconfigure.JsonMapperBuilderCustomizer
|
||||
import org.springframework.context.annotation.Bean
|
||||
|
||||
@@ -38,6 +39,7 @@ open class Jackson3AutoConfiguration {
|
||||
private val logger = MingLiLoggerFactory.getLogger<Jackson3AutoConfiguration>()
|
||||
|
||||
@Bean
|
||||
@ConditionalOnMissingBean(JsonMapperBuilderCustomizer::class)
|
||||
open fun myJsonMapperCustomizer(): JsonMapperBuilderCustomizer {
|
||||
return JsonMapperBuilderCustomizer { builder ->
|
||||
builder
|
||||
|
||||
+2
-2
@@ -16,7 +16,7 @@
|
||||
* ProjectName mingli-utils
|
||||
* ModuleName mingli-utils.main
|
||||
* CurrentFile Jackson3JsonApiAutoConfiguration.kt
|
||||
* LastUpdate 2026-06-21 10:40:58
|
||||
* LastUpdate 2026-10-02 16:11:02
|
||||
* UpdateUser MingLiPro
|
||||
*/
|
||||
|
||||
@@ -39,7 +39,7 @@ open class Jackson3JsonApiAutoConfiguration {
|
||||
private val logger = MingLiLoggerFactory.getLogger<Jackson3JsonApiAutoConfiguration>()
|
||||
|
||||
@Bean
|
||||
@ConditionalOnMissingBean
|
||||
@ConditionalOnMissingBean(JsonApi::class)
|
||||
open fun jsonApi(jsonMapper: JsonMapper): JsonApi {
|
||||
return Jackson3JsonApi(
|
||||
get = { jsonMapper },
|
||||
|
||||
+2
-2
@@ -16,7 +16,7 @@
|
||||
* ProjectName mingli-utils
|
||||
* ModuleName mingli-utils.main
|
||||
* CurrentFile JacksonAutoConfiguration.kt
|
||||
* LastUpdate 2026-03-11 08:46:40
|
||||
* LastUpdate 2026-10-02 16:10:53
|
||||
* UpdateUser MingLiPro
|
||||
*/
|
||||
|
||||
@@ -91,7 +91,7 @@ open class JacksonAutoConfiguration(objectMapper: ObjectMapper) {
|
||||
*/
|
||||
@Bean
|
||||
@Primary
|
||||
@ConditionalOnMissingBean
|
||||
@ConditionalOnMissingBean(JsonApi::class)
|
||||
open fun jsonApi(objectMapper: ObjectMapper): JsonApi {
|
||||
return JacksonJsonApi(objectMapper).also {
|
||||
log.info("JsonApi Bean Auto Configuration Success")
|
||||
|
||||
+5
-3
@@ -16,7 +16,7 @@
|
||||
* ProjectName mingli-utils
|
||||
* ModuleName mingli-utils.main
|
||||
* CurrentFile Okhttp3AutoConfiguration.kt
|
||||
* LastUpdate 2026-02-27 17:40:32
|
||||
* LastUpdate 2026-10-02 16:10:15
|
||||
* UpdateUser MingLiPro
|
||||
*/
|
||||
|
||||
@@ -27,6 +27,7 @@ import org.slf4j.Logger
|
||||
import org.springframework.beans.factory.annotation.Autowired
|
||||
import org.springframework.boot.autoconfigure.AutoConfiguration
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean
|
||||
import org.springframework.context.annotation.Bean
|
||||
import org.springframework.context.event.ContextClosedEvent
|
||||
import org.springframework.context.event.EventListener
|
||||
@@ -43,14 +44,15 @@ open class Okhttp3AutoConfiguration {
|
||||
open var userDefinedBuilder: okhttp3.OkHttpClient.Builder? = null
|
||||
|
||||
open fun initOkHttpClient() {
|
||||
if (userDefinedBuilder == null) {
|
||||
okHttpClient = okhttp3.OkHttpClient()
|
||||
okHttpClient = if (userDefinedBuilder == null) {
|
||||
okhttp3.OkHttpClient()
|
||||
} else {
|
||||
userDefinedBuilder!!.build()
|
||||
}
|
||||
}
|
||||
|
||||
@Bean
|
||||
@ConditionalOnMissingBean
|
||||
open fun okHttpClient(): okhttp3.OkHttpClient {
|
||||
if (okHttpClient == null) {
|
||||
initOkHttpClient()
|
||||
|
||||
@@ -16,7 +16,7 @@
|
||||
* ProjectName mingli-utils
|
||||
* ModuleName mingli-utils.main
|
||||
* CurrentFile SpringBeanUtils.kt
|
||||
* LastUpdate 2026-02-26 14:09:02
|
||||
* LastUpdate 2026-10-02 16:18:14
|
||||
* UpdateUser MingLiPro
|
||||
*/
|
||||
|
||||
@@ -25,6 +25,7 @@ package com.mingliqiye.utils.springboot.bean
|
||||
import org.springframework.beans.BeansException
|
||||
import org.springframework.context.ApplicationContext
|
||||
import org.springframework.context.ApplicationContextAware
|
||||
import org.springframework.context.ApplicationContextException
|
||||
import org.springframework.stereotype.Component
|
||||
|
||||
/**
|
||||
@@ -37,7 +38,12 @@ class SpringBeanUtils : ApplicationContextAware {
|
||||
companion object {
|
||||
|
||||
@JvmStatic
|
||||
private var applicationContext: ApplicationContext? = null
|
||||
val applicationContext: ApplicationContext
|
||||
get() = applicationContextBase
|
||||
?: throw ApplicationContextException("applicationContext isn't ContextInitialized ?")
|
||||
|
||||
private var applicationContextBase: ApplicationContext? = null
|
||||
|
||||
|
||||
@Throws(BeansException::class)
|
||||
inline fun <reified T : Any> getBean() = getBean(T::class.java)
|
||||
@@ -53,7 +59,7 @@ class SpringBeanUtils : ApplicationContextAware {
|
||||
@JvmStatic
|
||||
@Throws(BeansException::class)
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
fun getBean(name: String): Any = applicationContext!!.getBean(name)
|
||||
fun getBean(name: String): Any = applicationContext.getBean(name)
|
||||
|
||||
/**
|
||||
* 根据Bean类型获取Bean实例
|
||||
@@ -65,7 +71,7 @@ class SpringBeanUtils : ApplicationContextAware {
|
||||
@JvmStatic
|
||||
@Throws(BeansException::class)
|
||||
fun <T : Any> getBean(clazz: Class<T>): T {
|
||||
return applicationContext!!.getBean(clazz)
|
||||
return applicationContext.getBean(clazz)
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -79,7 +85,7 @@ class SpringBeanUtils : ApplicationContextAware {
|
||||
@JvmStatic
|
||||
@Throws(BeansException::class)
|
||||
fun <T : Any> getBean(name: String, clazz: Class<T>): T {
|
||||
return applicationContext!!.getBean<T>(name, clazz)
|
||||
return applicationContext.getBean<T>(name, clazz)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -92,6 +98,6 @@ class SpringBeanUtils : ApplicationContextAware {
|
||||
*/
|
||||
@Throws(BeansException::class)
|
||||
override fun setApplicationContext(applicationContext: ApplicationContext) {
|
||||
SpringBeanUtils.applicationContext = applicationContext
|
||||
applicationContextBase = applicationContext
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user