Class NioFile

  • All Implemented Interfaces:
    java.io.Closeable, java.lang.AutoCloseable

    public final class NioFile
    extends java.lang.Object
    implements java.io.Closeable
    File wrapper that protects against concurrent file closing events due to e.g. thread interrupts. In case the file channel that is used by this class is closed due to such an event, it will try to reopen the channel. The thread that causes the ClosedByInterruptException is not protected, assuming the interrupt is intended to end the thread's operation.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private boolean explictlyClosed  
      private java.nio.channels.FileChannel fc  
      private java.io.File file  
      private java.util.Set<java.nio.file.StandardOpenOption> openOptions  
      static java.util.EnumSet<java.nio.file.StandardOpenOption> R  
      static java.util.EnumSet<java.nio.file.StandardOpenOption> RW  
      static java.util.EnumSet<java.nio.file.StandardOpenOption> RWD  
      static java.util.EnumSet<java.nio.file.StandardOpenOption> RWS  
    • Constructor Summary

      Constructors 
      Constructor Description
      NioFile​(java.io.File file)
      Constructor Opens a file in read/write mode, creating a new one if the file doesn't exist.
      NioFile​(java.io.File file, java.lang.String mode)
      Constructor Opens a file in a specific mode, creating a new one if the file doesn't exist.
      NioFile​(java.io.File file, java.util.Set<java.nio.file.StandardOpenOption> openOptions)  
      NioFile​(java.nio.file.Path path, java.util.Set<java.nio.file.StandardOpenOption> openOptions)  
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void close()  
      boolean delete()
      Close any open channels and then deletes the file.
      void force​(boolean metaData)
      Performs a protected FileChannel.force(boolean) call.
      java.io.File getFile()  
      boolean isClosed()
      Check if a file was closed explicitly.
      private void open()
      Open a file channel for random access.
      int read​(java.nio.ByteBuffer buf, long offset)
      Performs a protected FileChannel.read(ByteBuffer, long) call.
      byte readByte​(long offset)
      Read single byte from channel starting at offset.
      byte[] readBytes​(long offset, int length)
      Read a byte array of a specific length from channel starting at offset.
      int readInt​(long offset)
      Read integer value from channel starting at offset.
      long readLong​(long offset)
      Read long value from channel starting at offset.
      private void reopen​(java.nio.channels.ClosedChannelException e)
      Reopen a channel closed by an exception, unless it was closed explicitly.
      long size()
      Performs a protected FileChannel.size() call.
      private static java.util.Set<java.nio.file.StandardOpenOption> toOpenOptions​(java.lang.String mode)  
      long transferTo​(long position, long count, java.nio.channels.WritableByteChannel target)
      Performs a protected FileChannel.transferTo(long, long, WritableByteChannel) call.
      void truncate​(long size)
      Performs a protected FileChannel.truncate(long) call.
      int write​(java.nio.ByteBuffer buf, long offset)
      Performs a protected FileChannel.write(ByteBuffer, long) call.
      void writeByte​(byte value, long offset)
      Write single byte to channel starting at offset.
      void writeBytes​(byte[] value, long offset)
      Write byte array to channel starting at offset.
      void writeInt​(int value, long offset)
      Write integer value to channel starting at offset.
      void writeLong​(long value, long offset)
      Write long value to channel starting at offset.
      • Methods inherited from class java.lang.Object

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

      • R

        public static final java.util.EnumSet<java.nio.file.StandardOpenOption> R
      • RW

        public static final java.util.EnumSet<java.nio.file.StandardOpenOption> RW
      • RWS

        public static final java.util.EnumSet<java.nio.file.StandardOpenOption> RWS
      • RWD

        public static final java.util.EnumSet<java.nio.file.StandardOpenOption> RWD
      • file

        private final java.io.File file
      • openOptions

        private final java.util.Set<java.nio.file.StandardOpenOption> openOptions
      • fc

        private volatile java.nio.channels.FileChannel fc
      • explictlyClosed

        private volatile boolean explictlyClosed
    • Constructor Detail

      • NioFile

        public NioFile​(java.io.File file)
                throws java.io.IOException
        Constructor Opens a file in read/write mode, creating a new one if the file doesn't exist.
        Parameters:
        file -
        Throws:
        java.io.IOException
      • NioFile

        public NioFile​(java.io.File file,
                       java.lang.String mode)
                throws java.io.IOException
        Constructor Opens a file in a specific mode, creating a new one if the file doesn't exist.
        Parameters:
        file - file
        mode - file mode
        Throws:
        java.io.IOException
      • NioFile

        public NioFile​(java.io.File file,
                       java.util.Set<java.nio.file.StandardOpenOption> openOptions)
                throws java.io.IOException
        Throws:
        java.io.IOException
      • NioFile

        public NioFile​(java.nio.file.Path path,
                       java.util.Set<java.nio.file.StandardOpenOption> openOptions)
                throws java.io.IOException
        Throws:
        java.io.IOException
    • Method Detail

      • toOpenOptions

        private static java.util.Set<java.nio.file.StandardOpenOption> toOpenOptions​(java.lang.String mode)
      • open

        private void open()
                   throws java.io.IOException
        Open a file channel for random access.
        Throws:
        java.io.IOException
      • reopen

        private void reopen​(java.nio.channels.ClosedChannelException e)
                     throws java.io.IOException
        Reopen a channel closed by an exception, unless it was closed explicitly.
        Parameters:
        e - exception that closed the channel
        Throws:
        java.io.IOException
      • close

        public void close()
                   throws java.io.IOException
        Specified by:
        close in interface java.lang.AutoCloseable
        Specified by:
        close in interface java.io.Closeable
        Throws:
        java.io.IOException
      • isClosed

        public boolean isClosed()
        Check if a file was closed explicitly.
        Returns:
        true if it was closed explicitly
      • getFile

        public java.io.File getFile()
      • delete

        public boolean delete()
                       throws java.io.IOException
        Close any open channels and then deletes the file.
        Returns:
        true if the file has been deleted successfully, false otherwise.
        Throws:
        java.io.IOException - If there was a problem closing the open file channel.
      • force

        public void force​(boolean metaData)
                   throws java.io.IOException
        Performs a protected FileChannel.force(boolean) call.
        Parameters:
        metaData -
        Throws:
        java.io.IOException
      • truncate

        public void truncate​(long size)
                      throws java.io.IOException
        Performs a protected FileChannel.truncate(long) call.
        Parameters:
        size -
        Throws:
        java.io.IOException
      • size

        public long size()
                  throws java.io.IOException
        Performs a protected FileChannel.size() call.
        Returns:
        size of the file
        Throws:
        java.io.IOException
      • transferTo

        public long transferTo​(long position,
                               long count,
                               java.nio.channels.WritableByteChannel target)
                        throws java.io.IOException
        Performs a protected FileChannel.transferTo(long, long, WritableByteChannel) call.
        Parameters:
        position - position within the file
        count - number of bytes to transfer
        target - target channel
        Returns:
        number of bytes transferred
        Throws:
        java.io.IOException
      • write

        public int write​(java.nio.ByteBuffer buf,
                         long offset)
                  throws java.io.IOException
        Performs a protected FileChannel.write(ByteBuffer, long) call.
        Parameters:
        buf - buffer
        offset - non-negative offset
        Returns:
        number of bytes written
        Throws:
        java.io.IOException
      • read

        public int read​(java.nio.ByteBuffer buf,
                        long offset)
                 throws java.io.IOException
        Performs a protected FileChannel.read(ByteBuffer, long) call.
        Parameters:
        buf - buffer to read
        offset - non-negative offset
        Returns:
        number of bytes read
        Throws:
        java.io.IOException
      • writeBytes

        public void writeBytes​(byte[] value,
                               long offset)
                        throws java.io.IOException
        Write byte array to channel starting at offset.
        Parameters:
        value - byte array to write
        offset - non-negative offset
        Throws:
        java.io.IOException
      • readBytes

        public byte[] readBytes​(long offset,
                                int length)
                         throws java.io.IOException
        Read a byte array of a specific length from channel starting at offset.
        Parameters:
        offset -
        length -
        Returns:
        byte array
        Throws:
        java.io.IOException
      • writeByte

        public void writeByte​(byte value,
                              long offset)
                       throws java.io.IOException
        Write single byte to channel starting at offset.
        Parameters:
        value - value to write
        offset - non-negative offset
        Throws:
        java.io.IOException
      • readByte

        public byte readByte​(long offset)
                      throws java.io.IOException
        Read single byte from channel starting at offset.
        Parameters:
        offset - non-negative offset
        Returns:
        byte
        Throws:
        java.io.IOException
      • writeLong

        public void writeLong​(long value,
                              long offset)
                       throws java.io.IOException
        Write long value to channel starting at offset.
        Parameters:
        value - value to write
        offset - non-negative offset
        Throws:
        java.io.IOException
      • readLong

        public long readLong​(long offset)
                      throws java.io.IOException
        Read long value from channel starting at offset.
        Parameters:
        offset - non-negative offset
        Returns:
        long
        Throws:
        java.io.IOException
      • writeInt

        public void writeInt​(int value,
                             long offset)
                      throws java.io.IOException
        Write integer value to channel starting at offset.
        Parameters:
        value - value to write
        offset - non-negative offset
        Throws:
        java.io.IOException
      • readInt

        public int readInt​(long offset)
                    throws java.io.IOException
        Read integer value from channel starting at offset.
        Parameters:
        offset - non-negative offset
        Returns:
        integer
        Throws:
        java.io.IOException