Class NioFile

java.lang.Object
org.eclipse.rdf4j.common.io.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
  • Field Details Link icon

  • Constructor Details Link icon

  • Method Details Link icon

    • close Link icon

      public void close() throws IOException
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
      Throws:
      IOException
    • isClosed Link icon

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

      public File getFile()
    • delete Link icon

      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.
    • force Link icon

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

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

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

      public long transferTo(long position, long count, WritableByteChannel target) throws IOException
      Parameters:
      position - position within the file
      count - number of bytes to transfer
      target - target channel
      Returns:
      number of bytes transferred
      Throws:
      IOException
    • write Link icon

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

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

      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 Link icon

      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 Link icon

      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 Link icon

      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 Link icon

      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 Link icon

      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 Link icon

      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 Link icon

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