Class NativeResource
- java.lang.Object
-
- ej.sni.NativeResource
-
public final class NativeResource extends Object
ANativeResourceis an object representing a runtime element managed by the native side (such as a file, a socket, an image, ...).A
NativeResourceis registered using the C functionSNI_registerResource()and unregistered using the C functionSNI_unregisterResource().NativeResourcecan be listed using thelistRegisteredNativeResources()method.
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static ObjectclearCloseOnGC(long resource, long closeFunction)Cancels an automatic close configuration done previously withcloseOnGC(long, long, Object).static voidcloseOnGC(long resource, long closeFunction, Object obj)Registers the given native resource to be closed automatically when the given object is garbage collected.StringgetDescription()Gets a string describing this resource.longgetId()Gets the integer value of the resource pointer given toSNI_registerResource().static NativeResourcegetNativeResource(long resource, long closeFunction)Gets the native resource registered with the givenresourceandcloseFunctionpair.ObjectgetOwner()Gets an object representing the owner of the resource.static intgetRegisteredNativeResourcesCount()Gets the number of registered native resources.static Iterable<NativeResource>listRegisteredNativeResources()Gets the list of registered native resources.static voidprintRegisteredNativeResources(PrintStream out)Prints a description of the registered native resources to the specified print stream.
-
-
-
Method Detail
-
getOwner
public Object getOwner()
Gets an object representing the owner of the resource.- Returns:
- an object representing the owner of the resource.
-
getId
public long getId()
Gets the integer value of the resource pointer given toSNI_registerResource().- Returns:
- the integer value of the resource pointer given to
SNI_registerResource().
-
getDescription
public String getDescription()
Gets a string describing this resource.- Returns:
- a string describing this resource.
-
closeOnGC
public static void closeOnGC(long resource, long closeFunction, Object obj)Registers the given native resource to be closed automatically when the given object is garbage collected.The native resource is identified by the pair
resource,closeFunction.The native resource must have been registered previously in C using the SNI function
SNI_registerResource(). If later the native resource is explicitly closed using the SNI functionSNI_unregisterResource()then the automatic close configuration is cancelled.At most one object can be registered for the given native resource. The same object can be registered for several native resources.
- Parameters:
resource- theresourceargument given toSNI_registerResource()when the native resource has been registered.closeFunction- thecloseargument given toSNI_registerResource()when the native resource has been registered.obj- when this object is garbage collected, the native resource is closed.- Throws:
IllegalArgumentException- if the given native resource is not registered or ifcloseOnGC(long, long, Object)has already been called for the given native resource.NullPointerException- ifobjis null.
-
clearCloseOnGC
@Nullable public static Object clearCloseOnGC(long resource, long closeFunction)
Cancels an automatic close configuration done previously withcloseOnGC(long, long, Object).This method doesn't need to be called if the native resource will be closed using the SNI function
SNI_unregisterResource().- Parameters:
resource- theresourceargument given tocloseOnGC(long, long, Object).closeFunction- thecloseFunctionargument given tocloseOnGC(long, long, Object).- Returns:
- the object that has been passed to
closeOnGC(long, long, Object)or null if no automatic close is configured for the given native resource or if the object has been garbage collected. - Throws:
IllegalArgumentException- if the given native resource is not registered or has already been closed.
-
listRegisteredNativeResources
public static Iterable<NativeResource> listRegisteredNativeResources()
Gets the list of registered native resources.A native resources is registered using the C function
SNI_registerResource().- Returns:
- an
Iterableof registeredNativeResourceobjects.
-
printRegisteredNativeResources
public static void printRegisteredNativeResources(PrintStream out)
Prints a description of the registered native resources to the specified print stream.A native resources is registered using the C function
SNI_registerResource().- Parameters:
out-PrintStreamto use for output.
-
getRegisteredNativeResourcesCount
public static int getRegisteredNativeResourcesCount()
Gets the number of registered native resources.- Returns:
- the number of registered native resources.
-
getNativeResource
@Nullable public static NativeResource getNativeResource(long resource, long closeFunction)
Gets the native resource registered with the givenresourceandcloseFunctionpair.- Parameters:
resource- theresourceargument given toSNI_registerResource()when the native resource has been registered.closeFunction- thecloseargument given toSNI_registerResource()when the native resource has been registered.- Returns:
- a
NativeResourceor null if noNativeResourcehas been registered for the givenresourceandcloseFunctionpair.
-
-