25
25
26
26
package love.forte.simbot.resource
27
27
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
30
31
import kotlinx.io.files.Path
31
32
import kotlinx.io.files.SystemFileSystem
33
+ import kotlinx.io.readByteArray
32
34
import kotlin.annotation.AnnotationTarget.*
33
35
import kotlin.jvm.JvmMultifileClass
34
36
import kotlin.jvm.JvmName
@@ -61,20 +63,21 @@ public annotation class ExperimentalIOResourceAPI
61
63
/* *
62
64
* 根据完整的文件路径 [filePath] 得到一个基于对应文件的 [Resource]。
63
65
*
64
- * 文件会在通过 [Resource.data] 读取数据时才会校验存在性。届时如果文件不存在,
65
- * 则会得到 [IllegalStateException] 异常。
66
- *
67
66
* 如果不确定文件系统使用的路径分隔符,或可能在多个使用不同路径分隔符的系统上使用,
68
67
* 则考虑使用 [fileResource(base, ...parts)][fileResource]。
69
68
*
70
69
* @param filePath 文件路径,是使用 _路径分隔符_ 的多个片段。
71
70
* 其中, _路径分隔符_ 在不同的文件系统中可能是不同的,例如在 Unit 中的 `/`
72
71
* 和在 Windows 的 `\`。
73
72
*
73
+ * @throws kotlinx.io.files.FileNotFoundException see [kotlinx.io.files.FileSystem.source].
74
+ * @throws kotlinx.io.IOException see [kotlinx.io.files.FileSystem.source].
75
+ *
74
76
* @since 4.7.0
75
77
*/
76
78
@JvmName(" valueOfPath" )
77
79
@ExperimentalIOResourceAPI
80
+ @Throws(Exception ::class )
78
81
public fun fileResource (filePath : String ): Resource {
79
82
val path = Path (filePath)
80
83
return FilePathResource (path)
@@ -83,54 +86,69 @@ public fun fileResource(filePath: String): Resource {
83
86
/* *
84
87
* 根据文件路径片段集得到一个基于对应文件的 [Resource]。
85
88
*
89
+ * 文件会先在初始化时构造 [RawSource], 而后在读取 [Resource.data]
90
+ * 时使用 [Source]. 因此对文件存在性的校验和错误报告可能不会立即报告,
91
+ * 而是被推迟到真正读取数据时。
92
+ *
86
93
* 文件会在通过 [Resource.data] 读取数据时才会校验存在性。届时如果文件不存在,
87
94
* 则会得到 [IllegalStateException] 异常。
88
95
* 此异常的 [IllegalStateException.cause] 可能是:
89
96
* - [kotlinx.io.files.FileNotFoundException]
90
97
* - [kotlinx.io.IOException]
91
98
* 如果是这两个类型,则成因参考 [kotlinx.io.files.FileSystem.source]。
92
99
*
100
+ * @throws kotlinx.io.files.FileNotFoundException see [kotlinx.io.files.FileSystem.source].
101
+ * @throws kotlinx.io.IOException see [kotlinx.io.files.FileSystem.source].
102
+ *
93
103
* @since 4.7.0
94
104
*/
95
105
@JvmName(" valueOfPath" )
96
106
@ExperimentalIOResourceAPI
107
+ @Throws(Exception ::class )
97
108
public fun fileResource (base : String , vararg parts : String ): Resource {
98
109
val path = Path (base, * parts)
99
110
return FilePathResource (path)
100
111
}
101
112
102
113
/* *
103
- * 一个可以得到 [kotlinx.io.RawSource ] 的 [Resource]。
114
+ * 一个可以得到 [kotlinx.io.Source ] 的 [Resource]。
104
115
*
105
116
* @since 4.7.0
106
117
*/
107
118
@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
110
132
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() }
114
145
}
115
146
116
- /* *
117
- * 一个可以得到 [kotlinx.io.Source] 的 [Resource]。
118
- *
119
- * @since 4.7.0
120
- */
121
147
@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)
125
150
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()
135
153
}
136
154
0 commit comments