refactor(stream): 重构流处理相关代码
- 移除 InOutSteam、InputStreanWrapper、OutputStreamWrapper 类 - 删除 Getable、GetIdable、GetKeyable、GetNameable 接口- 更新 ByteUtils 和 Collection.kt 中的流处理逻辑 - 新增 InputStreamUtils.kt 文件,提供输入流相关扩展函数 - 更新项目版本号至 4.0.5
This commit is contained in:
@@ -16,15 +16,13 @@
|
||||
* ProjectName mingli-utils
|
||||
* ModuleName mingli-utils.main
|
||||
* CurrentFile ByteUtils.kt
|
||||
* LastUpdate 2025-09-15 00:07:22
|
||||
* LastUpdate 2025-09-15 17:26:34
|
||||
* UpdateUser MingLiPro
|
||||
*/
|
||||
@file:JvmName("ByteUtils")
|
||||
|
||||
package com.mingliqiye.utils.bytes
|
||||
|
||||
import com.mingliqiye.utils.stream.SuperStream
|
||||
|
||||
const val ESC_ASC: Byte = 0x10
|
||||
const val ESC_DESC: Byte = 0x1B
|
||||
const val ESC_NONE: Byte = 0x00
|
||||
@@ -43,6 +41,6 @@ const val ESC_RESERVED: Byte = 0x06
|
||||
fun ByteArray.getByteArrayString(): MutableList<String> {
|
||||
return this.toList().stream()
|
||||
.map { a -> String.format("0X%02X", a!!.toInt() and 0xFF) }
|
||||
.collect(SuperStream.Collectors.toList())
|
||||
.collect(com.mingliqiye.utils.stream.toList()) as MutableList<String>
|
||||
}
|
||||
|
||||
|
||||
@@ -16,7 +16,7 @@
|
||||
* ProjectName mingli-utils
|
||||
* ModuleName mingli-utils.main
|
||||
* CurrentFile Collection.kt
|
||||
* LastUpdate 2025-09-15 09:30:37
|
||||
* LastUpdate 2025-09-15 17:26:00
|
||||
* UpdateUser MingLiPro
|
||||
*/
|
||||
|
||||
@@ -24,8 +24,8 @@
|
||||
|
||||
package com.mingliqiye.utils.collection
|
||||
|
||||
import com.mingliqiye.utils.stream.SuperStream
|
||||
import java.util.*
|
||||
import java.util.stream.Collectors
|
||||
|
||||
|
||||
inline fun <reified T> Collection<T>.toArray(): Array<T> {
|
||||
@@ -34,15 +34,15 @@ inline fun <reified T> Collection<T>.toArray(): Array<T> {
|
||||
|
||||
inline fun <reified T, V> Collection<T>.toMap(noinline v: (T) -> V): Map<T, V> {
|
||||
return this.stream().collect(
|
||||
SuperStream.Collectors.toMap(
|
||||
SuperStream.Collectors::getThis, v
|
||||
com.mingliqiye.utils.stream.toMapValueThis(
|
||||
v
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
inline fun <reified T, V, K> Collection<T>.toMap(noinline k: (T) -> K, noinline v: (T) -> V): Map<K, V> {
|
||||
return this.stream().collect(
|
||||
SuperStream.Collectors.toMap(
|
||||
Collectors.toMap(
|
||||
k, v
|
||||
)
|
||||
)
|
||||
|
||||
@@ -0,0 +1,50 @@
|
||||
/*
|
||||
* Copyright 2025 mingliqiye
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
* ProjectName mingli-utils
|
||||
* ModuleName mingli-utils.main
|
||||
* CurrentFile InputStreamUtils.kt
|
||||
* LastUpdate 2025-09-15 17:26:34
|
||||
* UpdateUser MingLiPro
|
||||
*/
|
||||
@file:JvmName("InputStreamUtils")
|
||||
|
||||
package com.mingliqiye.utils.io
|
||||
|
||||
import java.io.InputStream
|
||||
import java.io.OutputStream
|
||||
import java.nio.charset.Charset
|
||||
|
||||
|
||||
fun InputStream.readAllText(charset: Charset = Charsets.UTF_8): String {
|
||||
return this.readAllBytes().toString(charset)
|
||||
}
|
||||
|
||||
fun InputStream.readAllBytes(): ByteArray {
|
||||
return this.readBytes()
|
||||
}
|
||||
|
||||
fun InputStream.exportBytes(out: OutputStream) {
|
||||
out.write(this.readAllBytes())
|
||||
out.flush()
|
||||
}
|
||||
|
||||
fun InputStream.readToList(): List<Byte> {
|
||||
return this.readBytes().toList()
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,122 @@
|
||||
/*
|
||||
* Copyright 2025 mingliqiye
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
* ProjectName mingli-utils
|
||||
* ModuleName mingli-utils.main
|
||||
* CurrentFile SuperStream.kt
|
||||
* LastUpdate 2025-09-15 17:17:48
|
||||
* UpdateUser MingLiPro
|
||||
*/
|
||||
@file:JvmName("Colls")
|
||||
|
||||
package com.mingliqiye.utils.stream
|
||||
|
||||
|
||||
import java.util.stream.Collector
|
||||
import java.util.stream.Collectors
|
||||
import java.util.stream.Stream
|
||||
|
||||
|
||||
class SuperStream<T> private constructor(val stream: Stream<T>) : Stream<T> by stream {
|
||||
companion object {
|
||||
@JvmStatic
|
||||
fun <T> of(stream: Stream<T>): SuperStream<T> {
|
||||
return SuperStream(stream)
|
||||
}
|
||||
|
||||
@JvmStatic
|
||||
fun <T> of(collection: Collection<T>): SuperStream<T> {
|
||||
return SuperStream(collection.stream())
|
||||
}
|
||||
|
||||
@JvmStatic
|
||||
fun <T : Map<K, V>, K, V> of(map: T): SuperStream<Map.Entry<K, V>> {
|
||||
return of(map.entries)
|
||||
}
|
||||
|
||||
@JvmStatic
|
||||
fun <T> of(vararg array: T): SuperStream<T> {
|
||||
return of(array.toList())
|
||||
}
|
||||
|
||||
@JvmStatic
|
||||
fun <T> of(iterator: Iterator<T>): SuperStream<T> {
|
||||
val data = ArrayList<T>(20)
|
||||
while (iterator.hasNext()) {
|
||||
data.add(iterator.next())
|
||||
}
|
||||
return of(data)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
interface Gettable<T> {
|
||||
fun get(): T
|
||||
}
|
||||
|
||||
interface KeyGettable<T> : Gettable<T> {
|
||||
|
||||
fun getKey(): T
|
||||
override fun get(): T {
|
||||
return getKey()
|
||||
}
|
||||
}
|
||||
|
||||
interface IdGettable<T> : Gettable<T> {
|
||||
fun getId(): T
|
||||
override fun get(): T {
|
||||
return getId()
|
||||
}
|
||||
}
|
||||
|
||||
fun <T> getThis(t: T): T {
|
||||
return t
|
||||
}
|
||||
|
||||
fun <T, U> toMapValueThis(valueMapper: java.util.function.Function<in T, out U>): Collector<T, *, Map<T, U>> {
|
||||
return Collectors.toMap(
|
||||
java.util.function.Function<T, T> { it },
|
||||
valueMapper
|
||||
) as Collector<T, *, Map<T, U>>
|
||||
}
|
||||
|
||||
fun <T, K> toMap(keyMapper: java.util.function.Function<in T, out K>): Collector<T, *, Map<K, T>> {
|
||||
return Collectors.toMap(
|
||||
keyMapper,
|
||||
java.util.function.Function<T, T> { it },
|
||||
) as Collector<T, *, Map<K, T>>
|
||||
}
|
||||
|
||||
fun <K> toMapGet(): Collector<Gettable<K>, *, Map<K, Gettable<K>>> {
|
||||
return Collectors.toMap(
|
||||
java.util.function.Function<Gettable<K>, K> { it.get() },
|
||||
java.util.function.Function<Gettable<K>, Gettable<K>> { it },
|
||||
) as Collector<Gettable<K>, *, Map<K, Gettable<K>>>
|
||||
}
|
||||
|
||||
fun <K, V> toMap(): Collector<Map.Entry<K, V>, *, Map<K, V>> {
|
||||
return Collectors.toMap(
|
||||
{ entry: Map.Entry<K, V> -> entry.key },
|
||||
{ entry: Map.Entry<K, V> -> entry.value }
|
||||
) as Collector<Map.Entry<K, V>, *, Map<K, V>>
|
||||
}
|
||||
|
||||
fun <T> toList(): Collector<T, *, List<T>> {
|
||||
return Collectors.toList<T>()
|
||||
}
|
||||
|
||||
fun <T> toSet(): Collector<T, *, Set<T>> {
|
||||
return Collectors.toSet<T>()
|
||||
}
|
||||
Reference in New Issue
Block a user