Class NativeEnclosure

  • All Implemented Interfaces:
    java.lang.AutoCloseable
    Direct Known Subclasses:
    CKAttribute, JSSOAEPParameterSpec, KBKDFDataParameter, KBKDFDerivedKey, KBKDFParameterSpec

    public abstract class NativeEnclosure
    extends java.lang.Object
    implements java.lang.AutoCloseable
    Classes extending NativeEnclsoure wrap a single NativeProxy instance, allowing it to be accessed from the JNI layer but be allocated and scoped from the Java layer. Because this class implements AutoCloseable, it is suggested to add constructors to derived classes which call open; this'll allow a single try-with-resources block to scope the lifetime of this object:
     try (NEC obj = new NEC(...)) {
          // ... do something with obj ...
     }
     
    Extending classes implement acquireNativeResources() and releaseNativeResources(). Before this instance is passed to the JNI layer, open() should be called, allocating all necessary resources. After making all necessary JNI calls, close() should be called to free resources. Ideally, open() and close() should be called close to the JNI calls, wrapped by the developer to limit accidental memory leaks.
    • Constructor Summary

      Constructors 
      Constructor Description
      NativeEnclosure()  
    • Method Summary

      All Methods Instance Methods Abstract Methods Concrete Methods Deprecated Methods 
      Modifier and Type Method Description
      protected abstract void acquireNativeResources()
      Allocate native resources, setting mPointer and mPointerSize as appropriate.
      void close()
      Deinitialize and free mPointer.
      protected void finalize()
      Deprecated.
      void open()
      Allocate and initialize mPointer with its enclosed value.
      protected abstract void releaseNativeResources()
      Called to deallocate native resources; note that mPointer.close() is called afterwards.
      • Methods inherited from class java.lang.Object

        clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • mPointer

        public NativeProxy mPointer
        Enclosed NativeProxy reference.
      • mPointerSize

        public long mPointerSize
        Size of enclosed mPointer.
    • Constructor Detail

      • NativeEnclosure

        public NativeEnclosure()
    • Method Detail

      • open

        public final void open()
                        throws java.lang.Exception
        Allocate and initialize mPointer with its enclosed value. Note that this method prevents you from accidentally leaking memory; to call open() twice, call close() first.
        Throws:
        java.lang.Exception
      • finalize

        @Deprecated
        protected void finalize()
                         throws java.lang.Throwable
        Deprecated.
        Overrides:
        finalize in class java.lang.Object
        Throws:
        java.lang.Throwable
      • close

        public final void close()
                         throws java.lang.Exception
        Deinitialize and free mPointer. Must be called to prevent memory leaks.
        Specified by:
        close in interface java.lang.AutoCloseable
        Throws:
        java.lang.Exception
      • acquireNativeResources

        protected abstract void acquireNativeResources()
                                                throws java.lang.Exception
        Allocate native resources, setting mPointer and mPointerSize as appropriate.
        Throws:
        java.lang.Exception
      • releaseNativeResources

        protected abstract void releaseNativeResources()
                                                throws java.lang.Exception
        Called to deallocate native resources; note that mPointer.close() is called afterwards. If mPointer.close() should be a no-op, extend from StaticVoidRef and do any required cleanup here.
        Throws:
        java.lang.Exception