> ## Documentation Index
> Fetch the complete documentation index at: https://bun.zhcndoc.com/llms.txt
> Use this file to discover all available pages before exploring further.

# 将 ReadableStream 转换为 Uint8Array

Bun 提供了若干便捷函数，用于将 [`ReadableStream`](https://developer.mozilla.org/en-US/docs/Web/API/ReadableStream) 的内容读取为不同格式。此代码片段会将 `ReadableStream` 的内容读取到 [`ArrayBuffer`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer) 中，然后创建一个指向该缓冲区的 [`Uint8Array`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array)。

```ts theme={"theme":{"light":"github-light","dark":"dracula"}}
const stream = new ReadableStream();
const buf = await Bun.readableStreamToArrayBuffer(stream);
const uint8 = new Uint8Array(buf);
```

此外，还有一个直接转换为 `Uint8Array` 的便捷方法。

```ts theme={"theme":{"light":"github-light","dark":"dracula"}}
const stream = new ReadableStream();
const uint8 = await Bun.readableStreamToBytes(stream);
```

***

有关 Bun 其他 `ReadableStream` 转换函数的文档，请参见 [文档 > API > Utils](/runtime/utils#bun-readablestreamto)。
