Interface Disposable

  • All Known Implementing Classes:
    CaptureInputStream, CaptureOutputStream, FixedLengthInputStream, InputStreamDecorator, OutputStreamDecorator, ProgressOutputStream, TempFileInputStream, TempOutputStream

    public interface Disposable
    Indicates that an object supports explicit uninitialization at the end of its life.

    When a disposable object will no longer be used, its dispose() method should be called, and the object should not be used further. The dispose() can be called multiple times without danger. It is not defined here what component calls the dispose() method. Furthermore, the object's behavior if used further is implementation and context-specific.

    The dispose() method is considered to be a last-chance opportunity to release resources; it therefore should allow no exceptions to escape. Disposing an object is considered to be an operation more final than close() (although a close() method may call dispose() ), but less final than finalize() (although finalize() may and should call dispose()). One of its uses is in the following pattern:

     
     Disposable myDisposable=createDisposable();
     try
     {
       ...
     }
     finally
     {
       myDisposable.dispose();
     }
     
     

    Implementing classes should normally call dispose() from finalize using the following pattern:

     
     @Override
     protected void finalize() throws Throwable
     {
       try
       {
         dispose();
       }
       finally
       {
         super.finalize();
       }
     }
     
     

    Such a pattern prevents the try{} finally{ object.close() } problem in which closing the object attempts to retry an operation which caused an exception in the first place.

    Author:
    Garret Wilson
    • Method Detail

      • dispose

        void dispose()
        Uninitializes the object. After calling this method, the object should not be used further. This method can be called multiple times without danger. All exceptions should be caught and dealt with in this method. Child classes must call the parent class version.