> For the complete documentation index, see [llms.txt](https://docs.zongsoft.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.zongsoft.com/framework/core/common/buffer.md).

# 缓冲工具

Buffer 内存租赁、编码解码和二进制读取工具。

`Buffer` 提供围绕 [`IMemoryOwner<T>`](https://learn.microsoft.com/zh-cn/dotnet/api/system.buffers.imemoryowner-1) [*源码*](https://source.dot.net/#System.Private.CoreLib/IMemoryOwner.cs)、数组租赁、文本编码解码和 [`ReadOnlySequence<byte>`](https://learn.microsoft.com/zh-cn/dotnet/api/system.buffers.readonlysequence-1) [*源码*](https://source.dot.net/#System.Memory/ReadOnlySequence.cs) 数值读取的工具方法。

## 内存租赁

来源：[framework/Zongsoft.Core/test/Common/BufferTest.cs](https://github.com/Zongsoft/framework/blob/main/Zongsoft.Core/test/Common/BufferTest.cs#L10)（节选；上下文见源文件）。

{% code title="BufferTest.cs" %}

```csharp
public void Lease_Array_CopiesValidPrefixAndDoesNotObserveCallerMutation()
{
	var source = new[] { 1, 2, 3, 4 };
	using var owner = source.Lease(3);

	source[0] = 10;
	source[3] = 40;

	Assert.Equal(3, owner.Memory.Length);
	Assert.Equal([1, 2, 3], owner.Memory.ToArray());
}
```

{% endcode %}

上述框架回归用例验证 Lease 复制有效前缀，不会随着原数组随后修改而变化；不能把它当成原数组的共享视图。释放时会归还内部数组池。

## 释放与所有权

来源：[framework/Zongsoft.Core/test/Common/BufferTest.cs](https://github.com/Zongsoft/framework/blob/main/Zongsoft.Core/test/Common/BufferTest.cs#L35)（节选；上下文见源文件）。

{% code title="BufferTest.cs" %}

```csharp
public void Lease_Array_DisposeIsIdempotentAndMakesOwnerInaccessible()
{
	var source = new byte[] { 1, 2, 3 };
	var owner = source.Lease();

	owner.Dispose();
	owner.Dispose();

	Assert.Equal([1, 2, 3], source);
	Assert.Throws<ObjectDisposedException>(() => _ = owner.Memory);
}
```

{% endcode %}

测试验证释放可重复调用，释放后不能继续读取租赁内存。编码和解码方法同样返回需要释放的拥有者；它们的默认文本编码是 UTF-8。

## 二进制读取

`Buffer` 提供大端和小端读取方法，例如 `TryGetInt32BigEndian`、`TryGetUInt64LittleEndian`、`TryGetDoubleBigEndian`。

这些方法适合网络协议、二进制文件和跨平台数据包解析。

## 相关资源

* [Buffer.cs](https://github.com/Zongsoft/framework/blob/main/Zongsoft.Core/src/Common/Buffer.cs)


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.zongsoft.com/framework/core/common/buffer.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
