File tree Expand file tree Collapse file tree
acidify-core/src/commonMain/kotlin/org/ntqqrev/acidify/common Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -2,11 +2,19 @@ package org.ntqqrev.acidify.common
22
33import kotlinx.io.Buffer
44import kotlinx.io.RawSource
5-
5+ import kotlin.js.JsExport
6+
7+ /* *
8+ * 通过 [ByteArray] 构造的 [MediaSource] 实现。
9+ * 该实现会直接使用提供的 [ByteArray] 作为数据源,因此在构造时需要保证该 [ByteArray] 不会被修改,以免导致数据不一致。
10+ * 在调用 [readByteArray] 时会直接返回该 [ByteArray]。
11+ */
12+ @JsExport
613class ByteArrayMediaSource (private val data : ByteArray ) : MediaSource() {
714 override val size: Long
815 get() = data.size.toLong()
916
17+ @JsExport.Ignore
1018 override fun openRawSource (): RawSource {
1119 return ByteArrayRawSource (data)
1220 }
Original file line number Diff line number Diff line change @@ -5,20 +5,37 @@ import kotlinx.io.buffered
55import kotlinx.io.readByteArray
66import kotlin.js.JsExport
77
8+ /* *
9+ * 表示可重复使用、可释放的媒体资源
10+ */
811@JsExport
912abstract class MediaSource {
13+ /* *
14+ * 资源的体积
15+ */
1016 abstract val size: Long
1117
18+ /* *
19+ * 打开一个全新的、用于访问该资源的 [RawSource] 实例
20+ */
21+ @JsExport.Ignore
1222 abstract fun openRawSource (): RawSource
1323
14- abstract fun dispose ()
15-
24+ /* *
25+ * 直接将该 [MediaSource] 的内容读取到一个 [ByteArray] 中。
26+ */
1627 open fun readByteArray (): ByteArray {
1728 return openRawSource().buffered().use { source ->
1829 source.readByteArray()
1930 }
2031 }
2132
33+ /* *
34+ * 在资源使用完毕后释放该资源。
35+ * 注意 `acidify-core` 的各个 API 都不会主动调用该方法,因此需要调用方主动释放资源。
36+ */
37+ abstract fun dispose ()
38+
2239 companion object {
2340 @JsExport.Ignore
2441 fun ByteArray.toMediaSource (): MediaSource = ByteArrayMediaSource (this )
You can’t perform that action at this time.
0 commit comments