https://github.com/teragrep/buf_01
https://github.com/teragrep/buf_01
Last synced: about 1 month ago
JSON representation
- Host: GitHub
- URL: https://github.com/teragrep/buf_01
- Owner: teragrep
- License: agpl-3.0
- Created: 2026-01-16T06:42:05.000Z (5 months ago)
- Default Branch: main
- Last Pushed: 2026-05-04T05:04:05.000Z (about 1 month ago)
- Last Synced: 2026-05-04T07:07:31.952Z (about 1 month ago)
- Language: Java
- Size: 8.08 MB
- Stars: 0
- Watchers: 0
- Forks: 2
- Open Issues: 1
-
Metadata Files:
- Readme: README.adoc
- License: LICENSE
Awesome Lists containing this project
README
// Before publishing your new repository:
// 1. Write the readme file
// 2. Update the issues link in Contributing section in the readme file
// 3. Update the discussion link in config.yml file in .github/ISSUE_TEMPLATE directory
= buf_01 - Teragrep Buffer Library for Java
// Add a short description of your project. Tell what your project does and what it's used for.
A library for providing leased access to Java MemorySegments.
== Features
// List your project's features
- ArenaMemorySegmentLeaseSupplier provides the MemorySegment from the given Arena with the configured size. This is used by the Pool. Closing the supplier will also close the Arena.
- Pool consists of Parent leases.
- Each parent lease can be sliced from offset to end into a sub lease.
- Sub leases cannot be pooled, but they can be close()'d.
- Parent lease will be returned to the pool by calling the close() method.
- All subleases must be closed before the parent lease can be returned to the pool.
- The parent lease will fill the underlying MemorySegment with zeroes when returned to the pool.
- In order to retrieve n bytes worth of MemorySegments, the included LeaseMultiGet object can be used. The pool itself will provide the first one from the inner queue data structure.
- Leases must be opened before using them. This can be handled by using the OpeningPool decorator.
== Documentation
See the official documentation on https://docs.teragrep.com[docs.teragrep.com].
== How to compile
The project can be build using Maven.
You can set the Java version by specifying the path with JAVA_HOME.
[source,bash]
----
JAVA_HOME=/usr/lib/jvm/temurin-25-jdk mvn clean package
----
== How to use
A MemorySegment can be taken from the Pool in the following manner, using try-with-resources to automate closing the resources:
[source,java]
----
import com.teragrep.buf_01.buffer.lease.MemorySegmentLeaseStub;
import com.teragrep.buf_01.buffer.lease.OpenableLease;
import com.teragrep.buf_01.buffer.supply.ArenaMemorySegmentLeaseSupplier;
import com.teragrep.poj_01.pool.Pool;
import com.teragrep.poj_01.pool.UnboundPool;
import java.lang.foreign.MemorySegment;
void example() {
try (final PoolableSupplier>, OpenableLease> poolableSupplier = new ArenaMemorySegmentLeaseSupplier(Arena.ofShared(), 1024)) {
try (final Pool> leasePool = new OpeningPool(new UnboundPool<>(poolableSupplier, new MemorySegmentLeaseStub()))) {
try (final OpenableLease lease = leasePool.get()) {
final MemorySegment memorySegment = lease.leasedObject();
}
}
}
}
----
It can also be sliced, and used like another MemorySegment:
Remember to close the slice after it is no longer needed or preferably use try-with-resources.
[source,java]
----
import java.lang.foreign.MemorySegment;
void example() {
try (final Lease slice = lease.sliceAt(512)) {
final MemorySegment memorySegment = slice.leasedObject();
}
}
----
After all slices have been closed and the parent MemorySegment is no longer of use, the parent lease will be
returned to the pool automatically if using try-with-resources or manually via the close() method.
== Contributing
// Change the repository name in the issues link to match with your project's name
You can involve yourself with our project by https://github.com/teragrep/buf_01/issues/new/choose[opening an issue] or submitting a pull request.
Contribution requirements:
. *All changes must be accompanied by a new or changed test.* If you think testing is not required in your pull request, include a sufficient explanation as why you think so.
. Security checks must pass
. Pull requests must align with the principles and http://www.extremeprogramming.org/values.html[values] of extreme programming.
. Pull requests must follow the principles of Object Thinking and Elegant Objects (EO).
Read more in our https://github.com/teragrep/teragrep/blob/main/contributing.adoc[Contributing Guideline].
=== Contributor License Agreement
Contributors must sign https://github.com/teragrep/teragrep/blob/main/cla.adoc[Teragrep Contributor License Agreement] before a pull request is accepted to organization's repositories.
You need to submit the CLA only once. After submitting the CLA you can contribute to all Teragrep's repositories.