Class NioFile

  • All Implemented Interfaces:
    Closeable, AutoCloseable

    public final class NioFile
    extends Object
    implements 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.
    Author:
    Arjohn Kampman
    • Constructor Detail

      • NioFile

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

        public NioFile​(File file,
                       String mode)
                throws 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:
        IOException
    • Method Detail

      • isClosed

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

        public File getFile()
      • delete

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

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

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

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

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

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

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

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

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