Skip to content

Commit 9dbb7ef

Browse files
committed
optimize: 优化改善与 SourceResource 相关的API
Signed-off-by: ForteScarlet <[email protected]>
1 parent 80733d3 commit 9dbb7ef

File tree

6 files changed

+64
-32
lines changed

6 files changed

+64
-32
lines changed

buildSrc/src/main/kotlin/P.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ fun isSnapshot(): Boolean = _isSnapshot
4747
@Suppress("MemberVisibilityCanBePrivate")
4848
sealed class P(override val group: String) : ProjectDetail() {
4949
companion object {
50-
const val VERSION = "4.6.1"
51-
const val NEXT_VERSION = "4.7.0"
50+
const val VERSION = "4.7.0-beta1"
51+
const val NEXT_VERSION = "4.7.0-beta2"
5252
const val SNAPSHOT_VERSION = "$VERSION-SNAPSHOT"
5353
const val NEXT_SNAPSHOT_VERSION = "$NEXT_VERSION-SNAPSHOT"
5454

gradle.properties

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ org.gradle.jvmargs=-Xmx4G -XX:MaxMetaspaceSize=1G -Dfile.encoding=UTF-8
4141
#org.gradle.workers.max=8
4242
org.gradle.parallel=true
4343
org.gradle.caching=true
44+
#org.gradle.configuration-cache=true
45+
#org.gradle.configuration-cache.problems=warn
4446

4547

4648
# https://kotlinlang.org/docs/gradle-compilation-and-caches.html#the-new-kotlin-compiler

simbot-api/build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ kotlin {
8383
api(project(":simbot-commons:simbot-common-collection"))
8484
api(libs.kotlinx.coroutines.core)
8585
api(libs.kotlinx.serialization.core)
86-
api(libs.kotlinx.io.core)
86+
implementation(libs.kotlinx.io.core)
8787
implementation(libs.kotlinx.serialization.json)
8888
// suspend reversal annotations
8989

simbot-api/src/commonMain/kotlin/love/forte/simbot/resource/IOResources.kt

Lines changed: 46 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,12 @@
2525

2626
package love.forte.simbot.resource
2727

28-
import kotlinx.io.*
29-
import kotlinx.io.files.FileNotFoundException
28+
import kotlinx.io.RawSource
29+
import kotlinx.io.Source
30+
import kotlinx.io.buffered
3031
import kotlinx.io.files.Path
3132
import kotlinx.io.files.SystemFileSystem
33+
import kotlinx.io.readByteArray
3234
import kotlin.annotation.AnnotationTarget.*
3335
import kotlin.jvm.JvmMultifileClass
3436
import kotlin.jvm.JvmName
@@ -61,20 +63,21 @@ public annotation class ExperimentalIOResourceAPI
6163
/**
6264
* 根据完整的文件路径 [filePath] 得到一个基于对应文件的 [Resource]。
6365
*
64-
* 文件会在通过 [Resource.data] 读取数据时才会校验存在性。届时如果文件不存在,
65-
* 则会得到 [IllegalStateException] 异常。
66-
*
6766
* 如果不确定文件系统使用的路径分隔符,或可能在多个使用不同路径分隔符的系统上使用,
6867
* 则考虑使用 [fileResource(base, ...parts)][fileResource]。
6968
*
7069
* @param filePath 文件路径,是使用 _路径分隔符_ 的多个片段。
7170
* 其中, _路径分隔符_ 在不同的文件系统中可能是不同的,例如在 Unit 中的 `/`
7271
* 和在 Windows 的 `\`。
7372
*
73+
* @throws kotlinx.io.files.FileNotFoundException see [kotlinx.io.files.FileSystem.source].
74+
* @throws kotlinx.io.IOException see [kotlinx.io.files.FileSystem.source].
75+
*
7476
* @since 4.7.0
7577
*/
7678
@JvmName("valueOfPath")
7779
@ExperimentalIOResourceAPI
80+
@Throws(Exception::class)
7881
public fun fileResource(filePath: String): Resource {
7982
val path = Path(filePath)
8083
return FilePathResource(path)
@@ -83,54 +86,69 @@ public fun fileResource(filePath: String): Resource {
8386
/**
8487
* 根据文件路径片段集得到一个基于对应文件的 [Resource]。
8588
*
89+
* 文件会先在初始化时构造 [RawSource], 而后在读取 [Resource.data]
90+
* 时使用 [Source]. 因此对文件存在性的校验和错误报告可能不会立即报告,
91+
* 而是被推迟到真正读取数据时。
92+
*
8693
* 文件会在通过 [Resource.data] 读取数据时才会校验存在性。届时如果文件不存在,
8794
* 则会得到 [IllegalStateException] 异常。
8895
* 此异常的 [IllegalStateException.cause] 可能是:
8996
* - [kotlinx.io.files.FileNotFoundException]
9097
* - [kotlinx.io.IOException]
9198
* 如果是这两个类型,则成因参考 [kotlinx.io.files.FileSystem.source]。
9299
*
100+
* @throws kotlinx.io.files.FileNotFoundException see [kotlinx.io.files.FileSystem.source].
101+
* @throws kotlinx.io.IOException see [kotlinx.io.files.FileSystem.source].
102+
*
93103
* @since 4.7.0
94104
*/
95105
@JvmName("valueOfPath")
96106
@ExperimentalIOResourceAPI
107+
@Throws(Exception::class)
97108
public fun fileResource(base: String, vararg parts: String): Resource {
98109
val path = Path(base, *parts)
99110
return FilePathResource(path)
100111
}
101112

102113
/**
103-
* 一个可以得到 [kotlinx.io.RawSource] 的 [Resource]。
114+
* 一个可以得到 [kotlinx.io.Source] 的 [Resource]。
104115
*
105116
* @since 4.7.0
106117
*/
107118
@ExperimentalIOResourceAPI
108-
public interface RawSourceResource : Resource {
109-
public fun source(): RawSource
119+
public interface SourceResource : Resource {
120+
/**
121+
* 得到一个用于本次数据读取的 [Source].
122+
* @throws kotlinx.io.files.FileNotFoundException
123+
* see [kotlinx.io.files.FileSystem.source], [RawSource.buffered]
124+
* @throws kotlinx.io.IOException
125+
* see [kotlinx.io.files.FileSystem.source], [RawSource.buffered]
126+
*
127+
* @see kotlinx.io.files.FileSystem.source
128+
* @see RawSource.buffered
129+
*/
130+
@Throws(Exception::class)
131+
public fun source(): Source
110132

111-
override fun data(): ByteArray {
112-
return source().buffered().use { it.readByteArray() }
113-
}
133+
/**
134+
* 使用 [source] 并读取其中全部的字节数据。
135+
*
136+
* @throws IllegalStateException
137+
* see [Source.readByteArray]
138+
* @throws kotlinx.io.IOException
139+
* see [Source.readByteArray]
140+
*
141+
* @see source
142+
*/
143+
@Throws(Exception::class)
144+
override fun data(): ByteArray = source().use { it.readByteArray() }
114145
}
115146

116-
/**
117-
* 一个可以得到 [kotlinx.io.Source] 的 [Resource]。
118-
*
119-
* @since 4.7.0
120-
*/
121147
@ExperimentalIOResourceAPI
122-
public interface SourceResource : RawSourceResource {
123-
override fun source(): Source
124-
}
148+
private data class FilePathResource(val path: Path) : SourceResource {
149+
private val source = SystemFileSystem.source(path)
125150

126-
@ExperimentalIOResourceAPI
127-
private data class FilePathResource(val path: Path) : RawSourceResource {
128-
override fun source(): RawSource = try {
129-
SystemFileSystem.source(path)
130-
} catch (fnf: FileNotFoundException) {
131-
throw IllegalStateException(fnf.message, fnf)
132-
} catch (io: IOException) {
133-
throw IllegalStateException(io.message, io)
134-
}
151+
@Throws(Exception::class)
152+
override fun source(): Source = source.buffered()
135153
}
136154

simbot-commons/simbot-common-annotations/src/commonMain/kotlin/love/forte/simbot/annotations/annotations.kt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,18 @@ public annotation class ExperimentalSimbotAPI
4747
@MustBeDocumented
4848
public annotation class InternalSimbotAPI
4949

50+
/**
51+
* 实现了一个应当仅供内部实现的API。虽然超类对外可能稳定,但是不保证第三方实现稳定。
52+
* 通常被使用在 [SubclassOptInRequired] 中。
53+
* @since 4.7.0
54+
*/
55+
@Retention(AnnotationRetention.BINARY)
56+
@RequiresOptIn(
57+
message = "实现了一个应当仅供内部实现的API。虽然超类对外可能稳定,但是不保证第三方实现稳定"
58+
)
59+
@MustBeDocumented
60+
public annotation class InternalInheritanceAPI
61+
5062
/**
5163
* 一个脆弱的、或者具有复杂的须知、备注、条件的API。
5264
* 这类API通常有很多前提条件以及注意事项,需要谨慎使用。

0 commit comments

Comments
 (0)