Package org.eclipse.rdf4j.common.io
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 theClosedByInterruptExceptionis not protected, assuming the interrupt is intended to end the thread's operation.- Author:
- Arjohn Kampman
-
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclose()booleandelete()Close any open channels and then deletes the file.voidforce(boolean metaData)Performs a protectedFileChannel.force(boolean)call.FilegetFile()booleanisClosed()Check if a file was closed explicitly.intread(ByteBuffer buf, long offset)Performs a protectedFileChannel.read(ByteBuffer, long)call.bytereadByte(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.intreadInt(long offset)Read integer value from channel starting at offset.longreadLong(long offset)Read long value from channel starting at offset.longsize()Performs a protectedFileChannel.size()call.longtransferTo(long position, long count, WritableByteChannel target)Performs a protectedFileChannel.transferTo(long, long, WritableByteChannel)call.voidtruncate(long size)Performs a protectedFileChannel.truncate(long)call.intwrite(ByteBuffer buf, long offset)Performs a protectedFileChannel.write(ByteBuffer, long)call.voidwriteByte(byte value, long offset)Write single byte to channel starting at offset.voidwriteBytes(byte[] value, long offset)Write byte array to channel starting at offset.voidwriteInt(int value, long offset)Write integer value to channel starting at offset.voidwriteLong(long value, long offset)Write long value to channel starting at offset.
-
-
-
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- filemode- file mode- Throws:
IOException
-
-
Method Detail
-
close
public void close() throws IOException- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Throws:
IOException
-
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 IOExceptionClose 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
public void force(boolean metaData) throws IOExceptionPerforms a protectedFileChannel.force(boolean)call.- Parameters:
metaData-- Throws:
IOException
-
truncate
public void truncate(long size) throws IOExceptionPerforms a protectedFileChannel.truncate(long)call.- Parameters:
size-- Throws:
IOException
-
size
public long size() throws IOExceptionPerforms a protectedFileChannel.size()call.- Returns:
- size of the file
- Throws:
IOException
-
transferTo
public long transferTo(long position, long count, WritableByteChannel target) throws IOExceptionPerforms a protectedFileChannel.transferTo(long, long, WritableByteChannel)call.- Parameters:
position- position within the filecount- number of bytes to transfertarget- target channel- Returns:
- number of bytes transferred
- Throws:
IOException
-
write
public int write(ByteBuffer buf, long offset) throws IOException
Performs a protectedFileChannel.write(ByteBuffer, long)call.- Parameters:
buf- bufferoffset- non-negative offset- Returns:
- number of bytes written
- Throws:
IOException
-
read
public int read(ByteBuffer buf, long offset) throws IOException
Performs a protectedFileChannel.read(ByteBuffer, long)call.- Parameters:
buf- buffer to readoffset- non-negative offset- Returns:
- number of bytes read
- Throws:
IOException
-
writeBytes
public void writeBytes(byte[] value, long offset) throws IOExceptionWrite byte array to channel starting at offset.- Parameters:
value- byte array to writeoffset- non-negative offset- Throws:
IOException
-
readBytes
public byte[] readBytes(long offset, int length) throws IOExceptionRead 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 IOExceptionWrite single byte to channel starting at offset.- Parameters:
value- value to writeoffset- non-negative offset- Throws:
IOException
-
readByte
public byte readByte(long offset) throws IOExceptionRead 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 IOExceptionWrite long value to channel starting at offset.- Parameters:
value- value to writeoffset- non-negative offset- Throws:
IOException
-
readLong
public long readLong(long offset) throws IOExceptionRead 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 IOExceptionWrite integer value to channel starting at offset.- Parameters:
value- value to writeoffset- non-negative offset- Throws:
IOException
-
readInt
public int readInt(long offset) throws IOExceptionRead integer value from channel starting at offset.- Parameters:
offset- non-negative offset- Returns:
- integer
- Throws:
IOException
-
-