asset.read()
把 manifest 声明的只读包内资源写入 ByteSink。assetId 是清单键,不是路径。
Syntax
const result = await host.asset.read(assetId, output, offset?);
Parameters
| 参数 | 类型 | 说明 |
|---|---|---|
assetId |
string |
manifest 中的稳定资源 ID |
output |
ByteSink |
接收字节的有界输出 |
offset |
u64 |
可选起始位置,默认 0 |
Return value
{ written: u32, totalSize: u64, eof: boolean }
totalSize 始终是当前应用版本中该资源的完整字节数。written = min(output.capacity, asset.maxReadBytes, totalSize - offset),eof 当且仅当 offset + written == totalSize。offset === totalSize 合法并返回 0 字节和 eof: true;更大的 offset 非法。
Examples
const chunk = new Uint8Array(64 * 1024);
let offset = 0;
for (;;) {
const result = await host.asset.read('levels/main', chunk, offset);
decoder.push(chunk.subarray(0, result.written));
offset += result.written;
if (result.eof) break;
}
Errors
特有错误:denied、not-found、invalid-offset、limit-exceeded。空 sink 使用版本级公共 invalid-argument;宿主不得把部分数据截断后错误标为 eof: true。
Requirements
- 能力:
host:asset.read - 用户激活:不需要
- 来源:app-context
- 信任档位:green