This class implements AutoCloseable so that the pinning can be managed conveniently with
a try-with-resource block that releases the pinning automatically:
int[] array = ...
try (PinnedObject pin = PinnedObject.create(array)) {
CIntPointer rawData = pin.addressOfArrayElement(0);
// it is safe to pass rawData to a C function.
}
// it is no longer safe to access rawData.
| Modifier and Type | Method and Description |
|---|---|
| public <T extends PointerBase> T | |
| public PointerBase | |
| public void | |
| public static PinnedObject | |
| public Object |
| addressOfArrayElement | back to summary |
|---|---|
| public <T extends PointerBase> T addressOfArrayElement(int index) Returns a pointer to the array element with the specified index. The object must be an array. No array bounds check for the index is performed.
| |
| addressOfObject | back to summary |
|---|---|
| public PointerBase addressOfObject() Returns the raw address of the pinned object. The object layout is not specified, but usually the address of an object is a pointer to to the first header word. In particular, the result is not a pointer to the first array element when the object is an array.
| |
| close | back to summary |
|---|---|
| public void close() Redeclares java. Releases the pin for the object. After this call, the object can be moved or discarded by the garbage collector.
| |
| create | back to summary |
|---|---|
| public static PinnedObject create(Object object) Create an open PinnedObject.
| |
| getObject | back to summary |
|---|---|
| public Object getObject() Returns the Object that is the referent of this PinnedObject.
| |