From f7abd6c271a55f5e96db1d623ec29b837b6ab2cd Mon Sep 17 00:00:00 2001 From: minglipro Date: Sun, 11 Oct 2026 17:35:58 +0800 Subject: [PATCH] no message --- build.gradle.kts | 12 +- gradle.properties | 4 +- .../mingliqiye/utils/stream/SuperStream.java | 13 +- .../utils/stream/SuprtStreamUtils.kt | 133 ++++++++++++++++ .../com/mingliqiye/utils/base/BaseCodec.kt | 6 +- .../mingliqiye/utils/bytes/ByteBufferUtils.kt | 2 +- .../utils/configuration/ConfigurationProp.kt | 12 +- .../utils/exception/CodecException.kt | 20 +-- .../utils/exception/EmptyJsonException.kt | 20 +-- .../utils/exception/HttpException.kt | 20 +-- .../utils/exception/HttpStatusException.kt | 14 +- .../utils/exception/HttpUrlException.kt | 20 +-- .../utils/exception/InvalidJsonException.kt | 20 +-- .../utils/exception/JsonException.kt | 20 +-- .../exception/JsonObjectCastException.kt | 20 +-- .../utils/exception/JsonParserException.kt | 20 +-- .../utils/exception/JsonTypeException.kt | 20 +-- .../exception/MingLiUtilsBaseException.kt | 21 +-- .../exception/NotFoundResourceException.kt | 20 +-- .../mingliqiye/utils/jna/FieldStructure.kt | 6 +- .../utils/json/api/JacksonJsonApi.kt | 4 +- .../mingliqiye/utils/netty/ByteArrayCodec.kt | 74 +++++++++ .../mingliqiye/utils/netty/NettyBaseCodec.kt | 116 ++++++++++++++ .../utils/netty/NettyBaseDecoder.kt | 114 ++++++++++++++ .../utils/netty/NettyBaseEncoder.kt | 130 ++++++++++++++++ .../com/mingliqiye/utils/netty/NettyUtils.kt | 14 +- .../com/mingliqiye/utils/pool/ObjectPool.kt | 144 ++++++++++++++++++ .../mingliqiye/utils/random/RandomBytes.kt | 24 +-- .../mingliqiye/utils/reflect/ReflectUtils.kt | 7 +- .../Jackson3AutoConfiguration.kt | 4 +- .../Jackson3JsonApiAutoConfiguration.kt | 4 +- .../autoconfigure/JacksonAutoConfiguration.kt | 4 +- .../autoconfigure/Okhttp3AutoConfiguration.kt | 8 +- .../utils/springboot/bean/SpringBeanUtils.kt | 18 ++- 34 files changed, 879 insertions(+), 209 deletions(-) create mode 100644 src/main/java/com/mingliqiye/utils/stream/SuprtStreamUtils.kt create mode 100644 src/main/kotlin/com/mingliqiye/utils/netty/ByteArrayCodec.kt create mode 100644 src/main/kotlin/com/mingliqiye/utils/netty/NettyBaseCodec.kt create mode 100644 src/main/kotlin/com/mingliqiye/utils/netty/NettyBaseDecoder.kt create mode 100644 src/main/kotlin/com/mingliqiye/utils/netty/NettyBaseEncoder.kt create mode 100644 src/main/kotlin/com/mingliqiye/utils/pool/ObjectPool.kt diff --git a/build.gradle.kts b/build.gradle.kts index 59cb4e5..ec062b1 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -16,7 +16,7 @@ * ProjectName mingli-utils * ModuleName mingli-utils * CurrentFile build.gradle.kts - * LastUpdate 2026-06-20 21:49:36 + * LastUpdate 2026-10-11 16:44:37 * UpdateUser MingLiPro */ @@ -33,6 +33,7 @@ plugins { kotlin("kapt") version "2.2.20" id("org.jetbrains.dokka") version "2.1.0" id("org.jetbrains.dokka-javadoc") version "2.1.0" + kotlin("plugin.spring") version "2.1.0" } val GROUPSID = project.properties["GROUPSID"] as String val VERSIONS = project.properties["VERSIONS"] as String @@ -56,6 +57,14 @@ java { kotlin { jvmToolchain(JDKVERSIONS) } + +repositories { + mavenLocal() + maven { + url = uri("https://maven.mingliqiye.com/") + } + mavenCentral() +} dependencies { implementation("org.slf4j:slf4j-api:2.0.17") implementation(kotlin("reflect")) @@ -85,6 +94,7 @@ dependencies { testImplementation("tools.jackson.module:jackson-module-kotlin:3.1.0") testImplementation("tools.jackson.core:jackson-databind:3.1.0") + testImplementation(kotlin("test")) } repositories { diff --git a/gradle.properties b/gradle.properties index 595c789..26c5728 100644 --- a/gradle.properties +++ b/gradle.properties @@ -16,10 +16,10 @@ # ProjectName mingli-utils # ModuleName mingli-utils # CurrentFile gradle.properties -# LastUpdate 2026-06-21 10:39:07 +# LastUpdate 2026-10-11 16:28:05 # UpdateUser MingLiPro # JDKVERSIONS=17 GROUPSID=com.mingliqiye.utils ARTIFACTID=mingli-utils -VERSIONS=5.2.6 +VERSIONS=5.3.1 diff --git a/src/main/java/com/mingliqiye/utils/stream/SuperStream.java b/src/main/java/com/mingliqiye/utils/stream/SuperStream.java index c3ac4ed..a7fda3c 100644 --- a/src/main/java/com/mingliqiye/utils/stream/SuperStream.java +++ b/src/main/java/com/mingliqiye/utils/stream/SuperStream.java @@ -16,7 +16,7 @@ * ProjectName mingli-utils * ModuleName mingli-utils.main * CurrentFile SuperStream.java - * LastUpdate 2026-04-14 12:00:05 + * LastUpdate 2026-10-11 17:23:57 * UpdateUser MingLiPro */ @@ -103,6 +103,17 @@ public class SuperStream implements Stream { return of(Collections.newArrayLists(args)); } + /** + * 创建一个包含指定布尔元素的SuperStream + * + * @param args 可变参数的布尔数组 + * @return 包含指定布尔的SuperStream实例 + */ + public static SuperStream of(boolean... args) { + return of(Collections.newArrayLists(args)); + } + + /** * 创建一个包含指定浮点数元素的SuperStream * diff --git a/src/main/java/com/mingliqiye/utils/stream/SuprtStreamUtils.kt b/src/main/java/com/mingliqiye/utils/stream/SuprtStreamUtils.kt new file mode 100644 index 0000000..9c725a8 --- /dev/null +++ b/src/main/java/com/mingliqiye/utils/stream/SuprtStreamUtils.kt @@ -0,0 +1,133 @@ +/* + * 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 SuprtStreamUtils.kt + * LastUpdate 2026-10-11 17:32:17 + * UpdateUser MingLiPro + */ + +package com.mingliqiye.utils.stream + +import java.util.stream.Stream + +/** + * [SuperStream] 的工厂工具类,提供把各类常见数据源(Java [Stream]、数组、集合、映射) + * 转换为 [SuperStream] 的扩展函数。 + * + * 所有扩展函数均以 `toSuperStream` 命名,遵循 Kotlin 的 `toXxx` 转换命名惯例。 + * + * 使用示例: + * ```kotlin + * intArrayOf(1, 2, 3).toSuperStream() + * listOf("a", "b").toSuperStream(parallel = true) + * mapOf(1 to "a").toSuperStream() + * ``` + */ +object SuperStreamUtils { + + /** + * 将 Java [Stream] 转换为 [SuperStream]。 + * + * @param T 流中元素的类型,必须是不可空类型。 + * @return 包装当前 [Stream] 的 [SuperStream]。 + */ + fun Stream.toSuperStream() = SuperStream.of(this) + + /** + * 将 [ByteArray] 转换为 [SuperStream]。 + * + * @param parallel 是否创建并行流,默认为 `false`(串行)。 + * @return 由当前数组元素构成的 [SuperStream]。 + */ + fun ByteArray.toSuperStream(parallel: Boolean = false) = SuperStream.of(this.toList(), parallel) + + /** + * 将 [ShortArray] 转换为 [SuperStream]。 + * + * @param parallel 是否创建并行流,默认为 `false`(串行)。 + * @return 由当前数组元素构成的 [SuperStream]。 + */ + fun ShortArray.toSuperStream(parallel: Boolean = false) = SuperStream.of(this.toList(), parallel) + + /** + * 将 [IntArray] 转换为 [SuperStream]。 + * + * @param parallel 是否创建并行流,默认为 `false`(串行)。 + * @return 由当前数组元素构成的 [SuperStream]。 + */ + fun IntArray.toSuperStream(parallel: Boolean = false) = SuperStream.of(this.toList(), parallel) + + /** + * 将 [LongArray] 转换为 [SuperStream]。 + * + * @param parallel 是否创建并行流,默认为 `false`(串行)。 + * @return 由当前数组元素构成的 [SuperStream]。 + */ + fun LongArray.toSuperStream(parallel: Boolean = false) = SuperStream.of(this.toList(), parallel) + + /** + * 将 [FloatArray] 转换为 [SuperStream]。 + * + * @param parallel 是否创建并行流,默认为 `false`(串行)。 + * @return 由当前数组元素构成的 [SuperStream]。 + */ + fun FloatArray.toSuperStream(parallel: Boolean = false) = SuperStream.of(this.toList(), parallel) + + /** + * 将 [DoubleArray] 转换为 [SuperStream]。 + * + * @param parallel 是否创建并行流,默认为 `false`(串行)。 + * @return 由当前数组元素构成的 [SuperStream]。 + */ + fun DoubleArray.toSuperStream(parallel: Boolean = false) = SuperStream.of(this.toList(), parallel) + + /** + * 将 [BooleanArray] 转换为 [SuperStream]。 + * + * @param parallel 是否创建并行流,默认为 `false`(串行)。 + * @return 由当前数组元素构成的 [SuperStream]。 + */ + fun BooleanArray.toSuperStream(parallel: Boolean = false) = SuperStream.of(this.toList(), parallel) + + /** + * 将对象数组 [Array] 转换为 [SuperStream]。 + * + * @param T 数组元素类型,必须是不可空类型。 + * @param parallel 是否创建并行流,默认为 `false`(串行)。 + * @return 由当前数组元素构成的 [SuperStream]。 + */ + fun Array.toSuperStream(parallel: Boolean = false) = SuperStream.of(this, parallel) + + /** + * 将 [Collection] 转换为 [SuperStream]。 + * + * @param T 集合元素类型,必须是不可空类型。 + * @param parallel 是否创建并行流,默认为 `false`(串行)。 + * @return 由当前集合元素构成的 [SuperStream]。 + */ + fun Collection.toSuperStream(parallel: Boolean = false) = SuperStream.of(this, parallel) + + /** + * 将 [Map] 转换为 [SuperStream],流中元素为键值对 [Map.Entry]。 + * + * @param K 键的类型。 + * @param V 值的类型。 + * @param parallel 是否创建并行流,默认为 `false`(串行)。 + * @return 由当前映射的条目构成的 [SuperStream]。 + */ + fun Map.toSuperStream(parallel: Boolean = false) = SuperStream.of(this, parallel) +} diff --git a/src/main/kotlin/com/mingliqiye/utils/base/BaseCodec.kt b/src/main/kotlin/com/mingliqiye/utils/base/BaseCodec.kt index 4a176bd..c5f977d 100644 --- a/src/main/kotlin/com/mingliqiye/utils/base/BaseCodec.kt +++ b/src/main/kotlin/com/mingliqiye/utils/base/BaseCodec.kt @@ -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) } /** diff --git a/src/main/kotlin/com/mingliqiye/utils/bytes/ByteBufferUtils.kt b/src/main/kotlin/com/mingliqiye/utils/bytes/ByteBufferUtils.kt index 1a3a92b..4fa620d 100644 --- a/src/main/kotlin/com/mingliqiye/utils/bytes/ByteBufferUtils.kt +++ b/src/main/kotlin/com/mingliqiye/utils/bytes/ByteBufferUtils.kt @@ -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 */ diff --git a/src/main/kotlin/com/mingliqiye/utils/configuration/ConfigurationProp.kt b/src/main/kotlin/com/mingliqiye/utils/configuration/ConfigurationProp.kt index 37d44f6..6994170 100644 --- a/src/main/kotlin/com/mingliqiye/utils/configuration/ConfigurationProp.kt +++ b/src/main/kotlin/com/mingliqiye/utils/configuration/ConfigurationProp.kt @@ -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 processFields( clazz: Class, instance: T, - mapsArgs: Map> + mapsArgs: Map>, ) { val fields = clazz.declaredFields @@ -194,7 +194,7 @@ open class ConfigurationProps { private fun convertValue( type: Class<*>, values: List, - 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") } diff --git a/src/main/kotlin/com/mingliqiye/utils/exception/CodecException.kt b/src/main/kotlin/com/mingliqiye/utils/exception/CodecException.kt index 8e90e99..abb76a2 100644 --- a/src/main/kotlin/com/mingliqiye/utils/exception/CodecException.kt +++ b/src/main/kotlin/com/mingliqiye/utils/exception/CodecException.kt @@ -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) diff --git a/src/main/kotlin/com/mingliqiye/utils/exception/EmptyJsonException.kt b/src/main/kotlin/com/mingliqiye/utils/exception/EmptyJsonException.kt index 07a7bd7..950478a 100644 --- a/src/main/kotlin/com/mingliqiye/utils/exception/EmptyJsonException.kt +++ b/src/main/kotlin/com/mingliqiye/utils/exception/EmptyJsonException.kt @@ -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) diff --git a/src/main/kotlin/com/mingliqiye/utils/exception/HttpException.kt b/src/main/kotlin/com/mingliqiye/utils/exception/HttpException.kt index 2e54cc8..d1a8e82 100644 --- a/src/main/kotlin/com/mingliqiye/utils/exception/HttpException.kt +++ b/src/main/kotlin/com/mingliqiye/utils/exception/HttpException.kt @@ -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) diff --git a/src/main/kotlin/com/mingliqiye/utils/exception/HttpStatusException.kt b/src/main/kotlin/com/mingliqiye/utils/exception/HttpStatusException.kt index ae1d106..350971a 100644 --- a/src/main/kotlin/com/mingliqiye/utils/exception/HttpStatusException.kt +++ b/src/main/kotlin/com/mingliqiye/utils/exception/HttpStatusException.kt @@ -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) diff --git a/src/main/kotlin/com/mingliqiye/utils/exception/HttpUrlException.kt b/src/main/kotlin/com/mingliqiye/utils/exception/HttpUrlException.kt index f01a091..d22c928 100644 --- a/src/main/kotlin/com/mingliqiye/utils/exception/HttpUrlException.kt +++ b/src/main/kotlin/com/mingliqiye/utils/exception/HttpUrlException.kt @@ -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) diff --git a/src/main/kotlin/com/mingliqiye/utils/exception/InvalidJsonException.kt b/src/main/kotlin/com/mingliqiye/utils/exception/InvalidJsonException.kt index b10c2aa..8fd5e4c 100644 --- a/src/main/kotlin/com/mingliqiye/utils/exception/InvalidJsonException.kt +++ b/src/main/kotlin/com/mingliqiye/utils/exception/InvalidJsonException.kt @@ -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) diff --git a/src/main/kotlin/com/mingliqiye/utils/exception/JsonException.kt b/src/main/kotlin/com/mingliqiye/utils/exception/JsonException.kt index da1b6a6..ff05603 100644 --- a/src/main/kotlin/com/mingliqiye/utils/exception/JsonException.kt +++ b/src/main/kotlin/com/mingliqiye/utils/exception/JsonException.kt @@ -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) diff --git a/src/main/kotlin/com/mingliqiye/utils/exception/JsonObjectCastException.kt b/src/main/kotlin/com/mingliqiye/utils/exception/JsonObjectCastException.kt index 569d86f..b243de1 100644 --- a/src/main/kotlin/com/mingliqiye/utils/exception/JsonObjectCastException.kt +++ b/src/main/kotlin/com/mingliqiye/utils/exception/JsonObjectCastException.kt @@ -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) diff --git a/src/main/kotlin/com/mingliqiye/utils/exception/JsonParserException.kt b/src/main/kotlin/com/mingliqiye/utils/exception/JsonParserException.kt index 641da0a..55b2a1a 100644 --- a/src/main/kotlin/com/mingliqiye/utils/exception/JsonParserException.kt +++ b/src/main/kotlin/com/mingliqiye/utils/exception/JsonParserException.kt @@ -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) diff --git a/src/main/kotlin/com/mingliqiye/utils/exception/JsonTypeException.kt b/src/main/kotlin/com/mingliqiye/utils/exception/JsonTypeException.kt index 8d9a982..ae4aec0 100644 --- a/src/main/kotlin/com/mingliqiye/utils/exception/JsonTypeException.kt +++ b/src/main/kotlin/com/mingliqiye/utils/exception/JsonTypeException.kt @@ -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) diff --git a/src/main/kotlin/com/mingliqiye/utils/exception/MingLiUtilsBaseException.kt b/src/main/kotlin/com/mingliqiye/utils/exception/MingLiUtilsBaseException.kt index d6dc063..5db6217 100644 --- a/src/main/kotlin/com/mingliqiye/utils/exception/MingLiUtilsBaseException.kt +++ b/src/main/kotlin/com/mingliqiye/utils/exception/MingLiUtilsBaseException.kt @@ -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) diff --git a/src/main/kotlin/com/mingliqiye/utils/exception/NotFoundResourceException.kt b/src/main/kotlin/com/mingliqiye/utils/exception/NotFoundResourceException.kt index 644c8f0..448096b 100644 --- a/src/main/kotlin/com/mingliqiye/utils/exception/NotFoundResourceException.kt +++ b/src/main/kotlin/com/mingliqiye/utils/exception/NotFoundResourceException.kt @@ -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) diff --git a/src/main/kotlin/com/mingliqiye/utils/jna/FieldStructure.kt b/src/main/kotlin/com/mingliqiye/utils/jna/FieldStructure.kt index c14c23f..7c565c7 100644 --- a/src/main/kotlin/com/mingliqiye/utils/jna/FieldStructure.kt +++ b/src/main/kotlin/com/mingliqiye/utils/jna/FieldStructure.kt @@ -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() { /** * 获取结构体字段顺序列表 * 通过反射遍历当前类及其父类的所有声明字段,过滤出公共非静态字段, diff --git a/src/main/kotlin/com/mingliqiye/utils/json/api/JacksonJsonApi.kt b/src/main/kotlin/com/mingliqiye/utils/json/api/JacksonJsonApi.kt index 3e06ee2..aa4e41e 100644 --- a/src/main/kotlin/com/mingliqiye/utils/json/api/JacksonJsonApi.kt +++ b/src/main/kotlin/com/mingliqiye/utils/json/api/JacksonJsonApi.kt @@ -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) } } diff --git a/src/main/kotlin/com/mingliqiye/utils/netty/ByteArrayCodec.kt b/src/main/kotlin/com/mingliqiye/utils/netty/ByteArrayCodec.kt new file mode 100644 index 0000000..14078c9 --- /dev/null +++ b/src/main/kotlin/com/mingliqiye/utils/netty/ByteArrayCodec.kt @@ -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 + * + * 将 Netty 的 ByteBuf 转换为 Kotlin 的 ByteArray + * + * @param inData 输入的 ByteBuf + * @param channelHandlerContext 上下文 + * @param outList 输出列表(未使用) + * @return 转换后的 ByteArray + */ + override fun decode( + inData: ByteBuf, + channelHandlerContext: ChannelHandlerContext, + outList: MutableList, + ): 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, + ): ByteBuf { + return inData.toByteBuf() + } +} diff --git a/src/main/kotlin/com/mingliqiye/utils/netty/NettyBaseCodec.kt b/src/main/kotlin/com/mingliqiye/utils/netty/NettyBaseCodec.kt new file mode 100644 index 0000000..d28df69 --- /dev/null +++ b/src/main/kotlin/com/mingliqiye/utils/netty/NettyBaseCodec.kt @@ -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() { + * override fun decode(inData: String, ctx: ChannelHandlerContext, outList: MutableList): Int { + * return inData.toInt() + * } + * override fun encode(inData: Int, ctx: ChannelHandlerContext, outList: MutableList): String { + * return inData.toString() + * } + * } + * ``` + */ +abstract class NettyBaseCodec : MessageToMessageCodec() { + + /** + * 解码方法(Netty 框架回调) + * + * 将入站消息 I 转换为 O,并添加到输出列表 + * + * @param ctx ChannelHandler 上下文,可用于获取 Channel 等信息 + * @param msg 入站消息(类型为 I) + * @param out 输出列表,解码后的消息(类型为 O)将被添加到此列表 + * + * @see decode 抽象方法,由子类实现具体的解码逻辑 + */ + override fun decode(ctx: ChannelHandlerContext, msg: I, out: MutableList) { + // 调用子类实现的抽象解码方法,将结果添加到输出列表 + 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) { + // 调用子类实现的抽象编码方法,将结果添加到输出列表 + 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, + ): O + + /** + * 抽象编码方法 + * + * 子类必须实现此方法,定义具体的编码逻辑 + * 将类型 O 的消息转换为类型 I + * + * @param inData 输入数据(类型 O),需要被编码 + * @param channelHandlerContext ChannelHandler 上下文 + * @param outList 输出列表,可用于在编码过程中添加多个消息(目前设计为单一返回值,outList 暂未使用) + * @return 编码后的数据(类型 I) + */ + abstract fun encode( + inData: O, + channelHandlerContext: ChannelHandlerContext, + outList: MutableList, + ): I +} diff --git a/src/main/kotlin/com/mingliqiye/utils/netty/NettyBaseDecoder.kt b/src/main/kotlin/com/mingliqiye/utils/netty/NettyBaseDecoder.kt new file mode 100644 index 0000000..fa25b65 --- /dev/null +++ b/src/main/kotlin/com/mingliqiye/utils/netty/NettyBaseDecoder.kt @@ -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() { + * override fun decode(inData: String, ctx: ChannelHandlerContext, outList: MutableList): Int { + * return inData.toInt() + * } + * } + * ``` + * + * @see io.netty.handler.codec.MessageToMessageDecoder + * @see io.netty.handler.codec.ByteToMessageDecoder + */ +abstract class NettyBaseDecoder : MessageToMessageDecoder() { + + /** + * 解码方法(Netty 框架回调) + * + * 由 Netty 在收到入站消息时自动调用 + * 将入站消息 I 转换为 O,并添加到输出列表 + * + * @param ctx ChannelHandler 上下文 + * - 可用于获取 Channel 信息 + * - 可用于触发后续事件 + * - 可用于获取 ChannelPipeline 引用 + * @param msg 入站消息(类型为 I) + * - 由 Netty 从 Channel 中读取 + * - 可能是 ByteBuf、String 或其他类型 + * @param out 输出列表(类型为 MutableList) + * - 解码后的消息(类型为 O)将被添加到此列表 + * - 列表中的消息会传递给下一个 InboundHandler + * - 可以添加多个消息,实现一对多的转换 + * + * @see #decode 抽象方法,由子类实现具体的解码逻辑 + */ + override fun decode(ctx: ChannelHandlerContext, msg: I, out: MutableList) { + // 调用子类实现的抽象解码方法,将结果添加到输出列表 + // 子类通过抽象方法返回解码后的对象,然后添加到 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, + ): O +} diff --git a/src/main/kotlin/com/mingliqiye/utils/netty/NettyBaseEncoder.kt b/src/main/kotlin/com/mingliqiye/utils/netty/NettyBaseEncoder.kt new file mode 100644 index 0000000..cdff9f0 --- /dev/null +++ b/src/main/kotlin/com/mingliqiye/utils/netty/NettyBaseEncoder.kt @@ -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() { + * override fun encode(inData: Int, ctx: ChannelHandlerContext, outList: MutableList): String { + * return inData.toString() + * } + * } + * ``` + * + * @see io.netty.handler.codec.MessageToMessageEncoder + * @see MessageToByteEncoder + */ +abstract class NettyBaseEncoder : MessageToMessageEncoder() { + + /** + * 编码方法(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) + * - 编码后的消息(类型为 O)将被添加到此列表 + * - 列表中的消息会传递给下一个 OutboundHandler + * - 可以添加多个消息,实现一对多的转换 + * + * @see #encode 抽象方法,由子类实现具体的编码逻辑 + */ + override fun encode(ctx: ChannelHandlerContext, msg: I, out: MutableList) { + // 调用子类实现的抽象编码方法,将结果添加到输出列表 + // 子类通过抽象方法返回编码后的对象,然后添加到 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, + ): O +} diff --git a/src/main/kotlin/com/mingliqiye/utils/netty/NettyUtils.kt b/src/main/kotlin/com/mingliqiye/utils/netty/NettyUtils.kt index dce47be..c2d25ab 100644 --- a/src/main/kotlin/com/mingliqiye/utils/netty/NettyUtils.kt +++ b/src/main/kotlin/com/mingliqiye/utils/netty/NettyUtils.kt @@ -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) -> Unit, -) { -} diff --git a/src/main/kotlin/com/mingliqiye/utils/pool/ObjectPool.kt b/src/main/kotlin/com/mingliqiye/utils/pool/ObjectPool.kt new file mode 100644 index 0000000..3e5a9d7 --- /dev/null +++ b/src/main/kotlin/com/mingliqiye/utils/pool/ObjectPool.kt @@ -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( + private val maxSize: Int = Int.MAX_VALUE, + private val minSize: Int = 0, + private val initialSize: Int = 1, +) : Pool { + + + private val initialCount = AtomicInteger(0) + private val pool = ConcurrentLinkedQueue>() + 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 { + 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) { + lock.withLock { + if (isClosed.get()) { + throw InterruptedException("Object pool closed") + } + pool.add(obj) + notEmpty.signal() + } + } + +} + + +interface Pool { + fun close() + fun borrow(timeoutMillis: Long): ObjectPoolData + fun borrow(): ObjectPoolData = borrow(0) + fun returnObject(obj: ObjectPoolData) + fun use(funs: (ObjectPoolData) -> Unit) = use(0, funs) + fun init(id: Int): ObjectPoolData + fun destroy(data: ObjectPoolData) + fun use(timeoutMillis: Long, funs: (ObjectPoolData) -> Unit) { + val i = borrow(timeoutMillis) + funs(i) + returnObject(i) + } +} + +data class ObjectPoolData( + val obj: T, + val objectPool: ObjectPool, + val id: Int = obj.hashCode(), + val name: String = "", +) + + +class TheardPool : ObjectPool() { + override fun init(id: Int): ObjectPoolData { + return ObjectPoolData( + Thread().apply { + name = "TheardPool $id" + }, + this + ) + } + + override fun destroy(data: ObjectPoolData) { + data.obj.interrupt() + } + +} diff --git a/src/main/kotlin/com/mingliqiye/utils/random/RandomBytes.kt b/src/main/kotlin/com/mingliqiye/utils/random/RandomBytes.kt index b1bf3a6..361e119 100644 --- a/src/main/kotlin/com/mingliqiye/utils/random/RandomBytes.kt +++ b/src/main/kotlin/com/mingliqiye/utils/random/RandomBytes.kt @@ -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.. Array.randomItem(): T = this[nextInt(0.. Array.randomItem(): T = this[nextInt(indices)] /** * ByteArray类型的扩展函数 - 从字节数组中随机选择一个元素 * @return 字节数组中的随机字节 */ @JvmStatic - fun ByteArray.randomItem(): Byte = this[nextInt(0.. Collection.randomItem(): T = this.elementAt(nextInt(0.. Collection.randomItem(): T = this.elementAt(nextInt(indices)) } diff --git a/src/main/kotlin/com/mingliqiye/utils/reflect/ReflectUtils.kt b/src/main/kotlin/com/mingliqiye/utils/reflect/ReflectUtils.kt index 24eb8a4..380963b 100644 --- a/src/main/kotlin/com/mingliqiye/utils/reflect/ReflectUtils.kt +++ b/src/main/kotlin/com/mingliqiye/utils/reflect/ReflectUtils.kt @@ -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 AnnotatedElement.getAnnotation() = this.getAnnotation(T::class.java) inline fun T.ktClass(): KClass = T::class + var KMutableProperty.value: T get() = this.getter.call() set(t: T) = this.setter.call(t) + val KProperty.javaGetter: Method get() = this.getter.javaMethod!! + val KMutableProperty.javaSetter: Method get() = this.getter.javaMethod!! val KFunction.java: Method diff --git a/src/main/kotlin/com/mingliqiye/utils/springboot/autoconfigure/Jackson3AutoConfiguration.kt b/src/main/kotlin/com/mingliqiye/utils/springboot/autoconfigure/Jackson3AutoConfiguration.kt index 56bb856..57ad9ce 100644 --- a/src/main/kotlin/com/mingliqiye/utils/springboot/autoconfigure/Jackson3AutoConfiguration.kt +++ b/src/main/kotlin/com/mingliqiye/utils/springboot/autoconfigure/Jackson3AutoConfiguration.kt @@ -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() @Bean + @ConditionalOnMissingBean(JsonMapperBuilderCustomizer::class) open fun myJsonMapperCustomizer(): JsonMapperBuilderCustomizer { return JsonMapperBuilderCustomizer { builder -> builder diff --git a/src/main/kotlin/com/mingliqiye/utils/springboot/autoconfigure/Jackson3JsonApiAutoConfiguration.kt b/src/main/kotlin/com/mingliqiye/utils/springboot/autoconfigure/Jackson3JsonApiAutoConfiguration.kt index b2e19f9..3441124 100644 --- a/src/main/kotlin/com/mingliqiye/utils/springboot/autoconfigure/Jackson3JsonApiAutoConfiguration.kt +++ b/src/main/kotlin/com/mingliqiye/utils/springboot/autoconfigure/Jackson3JsonApiAutoConfiguration.kt @@ -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() @Bean - @ConditionalOnMissingBean + @ConditionalOnMissingBean(JsonApi::class) open fun jsonApi(jsonMapper: JsonMapper): JsonApi { return Jackson3JsonApi( get = { jsonMapper }, diff --git a/src/main/kotlin/com/mingliqiye/utils/springboot/autoconfigure/JacksonAutoConfiguration.kt b/src/main/kotlin/com/mingliqiye/utils/springboot/autoconfigure/JacksonAutoConfiguration.kt index 506bedc..bebf746 100644 --- a/src/main/kotlin/com/mingliqiye/utils/springboot/autoconfigure/JacksonAutoConfiguration.kt +++ b/src/main/kotlin/com/mingliqiye/utils/springboot/autoconfigure/JacksonAutoConfiguration.kt @@ -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") diff --git a/src/main/kotlin/com/mingliqiye/utils/springboot/autoconfigure/Okhttp3AutoConfiguration.kt b/src/main/kotlin/com/mingliqiye/utils/springboot/autoconfigure/Okhttp3AutoConfiguration.kt index 2db566a..6555387 100644 --- a/src/main/kotlin/com/mingliqiye/utils/springboot/autoconfigure/Okhttp3AutoConfiguration.kt +++ b/src/main/kotlin/com/mingliqiye/utils/springboot/autoconfigure/Okhttp3AutoConfiguration.kt @@ -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() diff --git a/src/main/kotlin/com/mingliqiye/utils/springboot/bean/SpringBeanUtils.kt b/src/main/kotlin/com/mingliqiye/utils/springboot/bean/SpringBeanUtils.kt index 0f7ddb6..acc9483 100644 --- a/src/main/kotlin/com/mingliqiye/utils/springboot/bean/SpringBeanUtils.kt +++ b/src/main/kotlin/com/mingliqiye/utils/springboot/bean/SpringBeanUtils.kt @@ -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 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 getBean(clazz: Class): T { - return applicationContext!!.getBean(clazz) + return applicationContext.getBean(clazz) } /** @@ -79,7 +85,7 @@ class SpringBeanUtils : ApplicationContextAware { @JvmStatic @Throws(BeansException::class) fun getBean(name: String, clazz: Class): T { - return applicationContext!!.getBean(name, clazz) + return applicationContext.getBean(name, clazz) } } @@ -92,6 +98,6 @@ class SpringBeanUtils : ApplicationContextAware { */ @Throws(BeansException::class) override fun setApplicationContext(applicationContext: ApplicationContext) { - SpringBeanUtils.applicationContext = applicationContext + applicationContextBase = applicationContext } }