src/main/java/net/kolotyluk/java/files/Files.java
public static void cleanDirectByteBuffer(final ByteBuffer byteBuffer)
  {
	  
	if (byteBuffer == null) return;

    if (!byteBuffer.isDirect())
    	throw new IllegalArgumentException("byteBuffer isn't direct!");
    
    AccessController.doPrivileged(new PrivilegedAction<Void>() {
        public Void run() {
        	try
        	{
        		Method cleanerMethod = byteBuffer.getClass().getMethod("cleaner");
        	    cleanerMethod.setAccessible(true);
        	    Object cleaner = cleanerMethod.invoke(byteBuffer);
        	    Method cleanMethod = cleaner.getClass().getMethod("clean");
        	    cleanMethod.setAccessible(true);
        	    cleanMethod.invoke(cleaner);
        	}
        	catch (NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e)
        	{
        		throw new RuntimeException("Could not clean MappedByteBuffer -- File may still be locked!");
        	}
           return null; // nothing to return
        }
    });


  }
public static void cleanDirectByteBuffer(final ByteBuffer byteBuffer)
  {
	  
	if (byteBuffer == null) return;

    if (!byteBuffer.isDirect())
    	throw new IllegalArgumentException("byteBuffer isn't direct!");
    
    AccessController.doPrivileged(new PrivilegedAction<Void>() {
        public Void run() {
        	try
        	{
        		Method cleanerMethod = byteBuffer.getClass().getMethod("cleaner");
        	    cleanerMethod.setAccessible(true);
        	    Object cleaner = cleanerMethod.invoke(byteBuffer);
        	    Method cleanMethod = cleaner.getClass().getMethod("clean");
        	    cleanMethod.setAccessible(true);
        	    cleanMethod.invoke(cleaner);
        	}
        	catch (NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e)
        	{
        		throw new RuntimeException("Could not clean MappedByteBuffer -- File may still be locked!");
        	}
           return null; // nothing to return
        }
    });


  }
public static void cleanDirectByteBuffer ( final ByteBuffer byteBuffer ) { if ( byteBuffer == null ) return ; if ( ! byteBuffer . isDirect ( ) ) throw new IllegalArgumentException ( "byteBuffer isn't direct!" ) ; AccessController . doPrivileged ( new PrivilegedAction < Void > ( ) { public Void run ( ) { try { Method cleanerMethod = byteBuffer . getClass ( ) . getMethod ( "cleaner" ) ; cleanerMethod . setAccessible ( true ) ; Object cleaner = cleanerMethod . invoke ( byteBuffer ) ; Method cleanMethod = cleaner . getClass ( ) . getMethod ( "clean" ) ; cleanMethod . setAccessible ( true ) ; cleanMethod . invoke ( cleaner ) ; } catch ( NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e ) { throw new RuntimeException ( "Could not clean MappedByteBuffer -- File may still be locked!" ) ; } return null ; // nothing to return } } ) ; }
Clean or unmap a direct ByteBuffer

<p>DirectByteBuffers are garbage collected by using a phantom reference and a
reference queue. Every once a while, the JVM checks the reference queue and
cleans the DirectByteBuffers. However, as this doesn't happen immediately
after discarding all references to a DirectByteBuffer, it's easy get
{@link OutOfMemoryError} problems using direct ByteBuffers.</p>

<p>Also, if a file is still mapped, via {@link MappedByteBuffer}, then it is
locked and cannot be destroyed or possibly written to if it was previously
mapped read. Trying to destroy or write to a locked file will result in an
{@link IOException}</p>

<p>This function explicitly calls the cleaner method of a {@link ByteBuffer} using
reflection because it is not publicly accessible.</p>

@param byteBuffer The DirectByteBuffer that will be "cleaned". Returns immediately if
the argument is null.
@throws IllegalArgumentException if byteBuffer isn't direct
@throws RuntimeException if cleaning may have failed
Clean or unmap a direct ByteBuffer