Class CleanableFileChannel

  • All Implemented Interfaces:
    java.io.Closeable, java.lang.AutoCloseable, java.nio.channels.ByteChannel, java.nio.channels.Channel, java.nio.channels.GatheringByteChannel, java.nio.channels.ReadableByteChannel, java.nio.channels.ScatteringByteChannel, java.nio.channels.SeekableByteChannel, java.nio.channels.WritableByteChannel

    public class CleanableFileChannel
    extends java.lang.Object
    implements java.lang.AutoCloseable, java.nio.channels.SeekableByteChannel, java.nio.channels.GatheringByteChannel, java.nio.channels.ScatteringByteChannel
    Wrapper around standard FileChannel with additional safety net for closing if user does not close and ability to run custom function during cleanup/closing. Users should either use with try-with-resources or ensure they manually call .close() once finished. As a safety net, .close() will be called by a cleaner if and when the instance of CleanableFileChannel is going to be collected by GC.
    • Constructor Summary

      Constructors 
      Constructor Description
      CleanableFileChannel​(java.nio.channels.FileChannel fileChannel, java.lang.Runnable runOnClose)
      Create a filechannel with a runnable closer to perform the close action.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void close()  
      void force​(boolean metaData)  
      boolean isOpen()  
      java.nio.channels.FileLock lock​(long position, long size, boolean shared)  
      java.nio.MappedByteBuffer map​(java.nio.channels.FileChannel.MapMode mode, long position, long size)  
      long position()  
      CleanableFileChannel position​(long newPosition)  
      int read​(java.nio.ByteBuffer dst)  
      long read​(java.nio.ByteBuffer[] dsts)  
      long read​(java.nio.ByteBuffer[] dsts, int offset, int length)  
      int read​(java.nio.ByteBuffer dst, long position)  
      long size()  
      long transferFrom​(java.nio.channels.ReadableByteChannel src, long position, long count)  
      long transferTo​(long position, long count, java.nio.channels.WritableByteChannel target)  
      CleanableFileChannel truncate​(long size)  
      java.nio.channels.FileLock tryLock​(long position, long size, boolean shared)  
      int write​(java.nio.ByteBuffer src)  
      long write​(java.nio.ByteBuffer[] srcs)  
      long write​(java.nio.ByteBuffer[] srcs, int offset, int length)  
      int write​(java.nio.ByteBuffer src, long position)  
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • CleanableFileChannel

        public CleanableFileChannel​(java.nio.channels.FileChannel fileChannel,
                                    java.lang.Runnable runOnClose)
        Create a filechannel with a runnable closer to perform the close action.
        Parameters:
        fileChannel - the fileChannel to wrap around.
        runOnClose - the runnable to run upon close.
    • Method Detail

      • read

        public int read​(java.nio.ByteBuffer dst)
                 throws java.io.IOException
        Specified by:
        read in interface java.nio.channels.ReadableByteChannel
        Specified by:
        read in interface java.nio.channels.SeekableByteChannel
        Throws:
        java.io.IOException
      • read

        public long read​(java.nio.ByteBuffer[] dsts,
                         int offset,
                         int length)
                  throws java.io.IOException
        Specified by:
        read in interface java.nio.channels.ScatteringByteChannel
        Throws:
        java.io.IOException
      • read

        public long read​(java.nio.ByteBuffer[] dsts)
                  throws java.io.IOException
        Specified by:
        read in interface java.nio.channels.ScatteringByteChannel
        Throws:
        java.io.IOException
      • write

        public int write​(java.nio.ByteBuffer src)
                  throws java.io.IOException
        Specified by:
        write in interface java.nio.channels.SeekableByteChannel
        Specified by:
        write in interface java.nio.channels.WritableByteChannel
        Throws:
        java.io.IOException
      • write

        public long write​(java.nio.ByteBuffer[] srcs,
                          int offset,
                          int length)
                   throws java.io.IOException
        Specified by:
        write in interface java.nio.channels.GatheringByteChannel
        Throws:
        java.io.IOException
      • write

        public long write​(java.nio.ByteBuffer[] srcs)
                   throws java.io.IOException
        Specified by:
        write in interface java.nio.channels.GatheringByteChannel
        Throws:
        java.io.IOException
      • position

        public long position()
                      throws java.io.IOException
        Specified by:
        position in interface java.nio.channels.SeekableByteChannel
        Throws:
        java.io.IOException
      • position

        public CleanableFileChannel position​(long newPosition)
                                      throws java.io.IOException
        Specified by:
        position in interface java.nio.channels.SeekableByteChannel
        Throws:
        java.io.IOException
      • size

        public long size()
                  throws java.io.IOException
        Specified by:
        size in interface java.nio.channels.SeekableByteChannel
        Throws:
        java.io.IOException
      • truncate

        public CleanableFileChannel truncate​(long size)
                                      throws java.io.IOException
        Specified by:
        truncate in interface java.nio.channels.SeekableByteChannel
        Throws:
        java.io.IOException
      • force

        public void force​(boolean metaData)
                   throws java.io.IOException
        Throws:
        java.io.IOException
      • transferTo

        public long transferTo​(long position,
                               long count,
                               java.nio.channels.WritableByteChannel target)
                        throws java.io.IOException
        Throws:
        java.io.IOException
      • transferFrom

        public long transferFrom​(java.nio.channels.ReadableByteChannel src,
                                 long position,
                                 long count)
                          throws java.io.IOException
        Throws:
        java.io.IOException
      • read

        public int read​(java.nio.ByteBuffer dst,
                        long position)
                 throws java.io.IOException
        Throws:
        java.io.IOException
      • write

        public int write​(java.nio.ByteBuffer src,
                         long position)
                  throws java.io.IOException
        Throws:
        java.io.IOException
      • map

        public java.nio.MappedByteBuffer map​(java.nio.channels.FileChannel.MapMode mode,
                                             long position,
                                             long size)
                                      throws java.io.IOException
        Throws:
        java.io.IOException
      • lock

        public java.nio.channels.FileLock lock​(long position,
                                               long size,
                                               boolean shared)
                                        throws java.io.IOException
        Throws:
        java.io.IOException
      • tryLock

        public java.nio.channels.FileLock tryLock​(long position,
                                                  long size,
                                                  boolean shared)
                                           throws java.io.IOException
        Throws:
        java.io.IOException
      • isOpen

        public boolean isOpen()
        Specified by:
        isOpen in interface java.nio.channels.Channel
      • close

        public void close()
        Specified by:
        close in interface java.lang.AutoCloseable
        Specified by:
        close in interface java.nio.channels.Channel
        Specified by:
        close in interface java.io.Closeable