ByteBuffer, and on-heap primitive arrays.See: Description
| Interface | Description |
|---|---|
| Handle |
A handle for Memory.
|
| MemoryRequestServer |
The MemoryRequestServer is a callback interface to provide a means for a direct (off-heap),
dynamic WritableMemory object to request more memory from the owner of the
WritableDirectHandle. |
| WritableHandle |
A handle for WritableMemory
|
| Class | Description |
|---|---|
| BaseBuffer |
A new positional API.
|
| Buffer |
Provides read-only, positional primitive and primitive array methods to any of the four resources
mentioned in the package level documentation.
|
| DefaultMemoryRequestServer |
This is a simple implementation of the MemoryRequestServer that creates space on the Java heap
for the requesting application.
|
| MapHandle |
Gets a Memory for a memory-mapped, read-only file resource, It is highly recommended that this
be created inside a try-with-resources statement.
|
| Memory |
Provides read-only primitive and primitive array methods to any of the four resources
mentioned in the package level documentation.
|
| UnsafeUtil |
Provides access to the sun.misc.Unsafe class and its key static fields.
|
| Util | |
| Util.RandomCodePoints |
Creates random valid Character Code Points (as integers).
|
| WritableBuffer |
Provides read and write, positional primitive and primitive array access to any of the four
resources mentioned at the package level.
|
| WritableDirectHandle |
References a WritableMemory for a writable direct memory resource.
|
| WritableMapHandle |
Gets a WritableMemory for a writable memory-mapped file resource.
|
| WritableMemory |
Provides read and write primitive and primitive array access to any of the four resources
mentioned at the package level.
|
| Exception | Description |
|---|---|
| ReadOnlyException |
The exception thrown when attempting to write into a read-only Resource.
|
| Utf8CodingException |
This exception will be thrown for errors encountered during either the encoding of characters
to Utf8 bytes, or the decoding of Utf8 bytes to characters.
|
This package provides high performance primitive and primitive array access to direct (native),
off-heap memory and memory-mapped file resources, and consistent views into
ByteBuffer, and on-heap primitive arrays. It can be used as a more
comprehensive and flexible replacement for ByteBuffer.
In addition, this package provides:
Memory and
WritableMemory for absolute offset access,
and read-only Buffer and WritableBuffer
for relative positional access (similar to ByteBuffer).
WritableMemory wMem = ...
Memory mem = wMem;
AutoCloseable for the external resources that require it,
which enables compile-time checks for non-closed resources.More specifically, this package provides access to four different types of resources using two different access APIs. These resources are contiguous blobs of bytes that provide at least byte-level read and write access. The four resources are:
ByteBuffers, both heap-based and direct, writable and read-only.The two different access APIs are:
In addition, all combinations of access APIs and backing resources can be accessed via
multibyte primitive methods (e.g.
getLong(...), getLongArray(...), putLong(...), putLongArray(...)) as either
ByteOrder.BIG_ENDIAN or ByteOrder.LITTLE_ENDIAN.
The resources don't know or care about the access APIs, and the access
APIs don't really know or care what resource they are accessing.
An access API is joined with
a resource either with a static factory method or in combination with a
Handle, which is used exclusively for resources that are external to
the JVM, such as allocation of direct memory and memory-mapped files.
The role of a Handle is to hold onto the reference of a resource that is outside the control
of the JVM. The resource is obtained from the handle with get().
When a handle is extended for an AutoCloseable resource and then joined with an access API it becomes an implementation handle. There are 3 implementation handles:
MapHandle for read-only access to a memory-mapped fileWritableMapHandle for writable access to a memory-mapped fileWritableDirectHandle for writable access to off-heap memory.As long as the implementation handle is valid the JVM will not attempt to close the resource.
An implementation handle implements AutoCloseable,
which also enables compile-time checks for non-closed resources. If a Handle is acquired
in a try-with-resources (TWR) block, it's associated resource will be automatically closed by
the JVM at the end of the block.
The resource can also be explicitly closed by the user by calling Handle.close().
//Using try-with-resources block:
try (WritableyMapHandle handle = WritableMemory.map(File file)) {
WritableMemory wMem = handle.get();
doWork(wMem) // read and write to memory mapped file.
}
//Using explicit close():
WritableMapHandle handle = WritableMemory.map(File file);
WritableMemory wMem = handle.get();
doWork(wMem) // read and write to memory mapped file.
handle.close();
Where it is desirable to pass ownership of the resource (and the close()
responsibility) one can not use the TWR block. Instead:
WritableMapHandle handler = WritableMemory.map(File file);
doWorkAndClose(handle); //passes the handle to object that closes the resource.
Whatever part of your process is responsible for allocating a resource external
to the JVM must be responsible for closing it or making sure it gets closed.
Since only the implementation Handles implement AutoCloseable, you must not let go of the
handle reference until you are done with its associated resource.
As mentioned above, there are two ways to do this:
Moving back and forth between Memory and Buffer:
Memory mem = ...
Buffer buf = mem.asBuffer();
...
Memory mem2 = buf.asMemory();
...
Hierarchical memory regions can be easily created:
WritableMemory wMem = ...
WritableMemory wReg = wMem.writableRegion(offset, length); //OR
Memory reg = wMem.region(offset, length);
With asserts enabled in the JVM, all methods are checked for bounds and
use-after-close violations.Copyright © 2015–2018 Yahoo! Inc.. All rights reserved.