Package org.eclipse.rdf4j.common.io
Class NioFile
java.lang.Object
org.eclipse.rdf4j.common.io.NioFile
- All Implemented Interfaces:
Closeable
,AutoCloseable
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 Summary
Modifier and TypeFieldDescriptionstatic final EnumSet
<StandardOpenOption> static final EnumSet
<StandardOpenOption> static final EnumSet
<StandardOpenOption> static final EnumSet
<StandardOpenOption> -
Constructor Summary
ConstructorDescriptionConstructor Opens a file in read/write mode, creating a new one if the file doesn't exist.Constructor Opens a file in a specific mode, creating a new one if the file doesn't exist.NioFile
(File file, Set<StandardOpenOption> openOptions) NioFile
(Path path, Set<StandardOpenOption> openOptions) -
Method Summary
Modifier and TypeMethodDescriptionvoid
close()
boolean
delete()
Close any open channels and then deletes the file.void
force
(boolean metaData) Performs a protectedFileChannel.force(boolean)
call.getFile()
boolean
isClosed()
Check if a file was closed explicitly.int
read
(ByteBuffer buf, long offset) Performs a protectedFileChannel.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.long
size()
Performs a protectedFileChannel.size()
call.long
transferTo
(long position, long count, WritableByteChannel target) Performs a protectedFileChannel.transferTo(long, long, WritableByteChannel)
call.void
truncate
(long size) Performs a protectedFileChannel.truncate(long)
call.int
write
(ByteBuffer buf, long offset) Performs a protectedFileChannel.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.
-
Field Details
-
R
-
RW
-
RWS
-
RWD
-
-
Constructor Details
-
NioFile
Constructor Opens a file in read/write mode, creating a new one if the file doesn't exist.- Parameters:
file
-- Throws:
IOException
-
NioFile
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
-
NioFile
- Throws:
IOException
-
NioFile
- Throws:
IOException
-
-
Method Details
-
close
- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
- Throws:
IOException
-
isClosed
public boolean isClosed()Check if a file was closed explicitly.- Returns:
- true if it was closed explicitly
-
getFile
-
delete
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
Performs a protectedFileChannel.force(boolean)
call.- Parameters:
metaData
-- Throws:
IOException
-
truncate
Performs a protectedFileChannel.truncate(long)
call.- Parameters:
size
-- Throws:
IOException
-
size
Performs a protectedFileChannel.size()
call.- Returns:
- size of the file
- Throws:
IOException
-
transferTo
Performs 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
Performs a protectedFileChannel.write(ByteBuffer, long)
call.- Parameters:
buf
- bufferoffset
- non-negative offset- Returns:
- number of bytes written
- Throws:
IOException
-
read
Performs a protectedFileChannel.read(ByteBuffer, long)
call.- Parameters:
buf
- buffer to readoffset
- non-negative offset- Returns:
- number of bytes read
- Throws:
IOException
-
writeBytes
Write byte array to channel starting at offset.- Parameters:
value
- byte array to writeoffset
- non-negative offset- Throws:
IOException
-
readBytes
Read a byte array of a specific length from channel starting at offset.- Parameters:
offset
-length
-- Returns:
- byte array
- Throws:
IOException
-
writeByte
Write single byte to channel starting at offset.- Parameters:
value
- value to writeoffset
- non-negative offset- Throws:
IOException
-
readByte
Read single byte from channel starting at offset.- Parameters:
offset
- non-negative offset- Returns:
- byte
- Throws:
IOException
-
writeLong
Write long value to channel starting at offset.- Parameters:
value
- value to writeoffset
- non-negative offset- Throws:
IOException
-
readLong
Read long value from channel starting at offset.- Parameters:
offset
- non-negative offset- Returns:
- long
- Throws:
IOException
-
writeInt
Write integer value to channel starting at offset.- Parameters:
value
- value to writeoffset
- non-negative offset- Throws:
IOException
-
readInt
Read integer value from channel starting at offset.- Parameters:
offset
- non-negative offset- Returns:
- integer
- Throws:
IOException
-