> 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/documentation/en/framework/core/common/buffer.md).

# Buffer Utilities

Buffer memory leasing, encoding, decoding and binary reading tools.

`Buffer` provides tool methods around [`IMemoryOwner<T>`](https://learn.microsoft.com/en-us/dotnet/api/system.buffers.imemoryowner-1) [*Source*](https://source.dot.net/#System.Private.CoreLib/IMemoryOwner.cs), array leasing, text encoding decoding, and [`ReadOnlySequence<byte>`](https://learn.microsoft.com/en-us/dotnet/api/system.buffers.readonlysequence-1) [*Source*](https://source.dot.net/#System.Memory/ReadOnlySequence.cs) numeric reading.

## Memory Leasing

Source: [framework/Zongsoft.Core/test/Common/BufferTest.cs](https://github.com/Zongsoft/framework/blob/main/Zongsoft.Core/test/Common/BufferTest.cs#L10) (excerpt; see source for context).

{% 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 %}

The above framework regression use case verifies that Lease copies a valid prefix that will not change as the original array is subsequently modified; it cannot be regarded as a shared view of the original array. The internal array pool is returned when freed.

## Release and Ownership

Source: [framework/Zongsoft.Core/test/Common/BufferTest.cs](https://github.com/Zongsoft/framework/blob/main/Zongsoft.Core/test/Common/BufferTest.cs#L35) (excerpt; see source for context).

{% 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 %}

The test verifies that the release can be called repeatedly, and the leased memory cannot be read after it is released. The encoding and decoding methods also return the owner to be released; their default text encoding is UTF-8.

## Binary Read

`Buffer` provides big-endian and little-endian read methods, such as `TryGetInt32BigEndian`, `TryGetUInt64LittleEndian`, `TryGetDoubleBigEndian`.

These methods are suitable for network protocols, binaries, and cross-platform packet parsing.

## Related Resources

* [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 by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

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

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

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.
