Package org.eclipse.rdf4j.common.io
Class NioFile
- java.lang.Object
-
- org.eclipse.rdf4j.common.io.NioFile
-
- All Implemented Interfaces:
java.io.Closeable,java.lang.AutoCloseable
public final class NioFile extends java.lang.Object implements java.io.CloseableFile 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.
-
-
Field Summary
Fields Modifier and Type Field Description private booleanexplictlyClosedprivate java.nio.channels.FileChannelfcprivate java.io.Filefileprivate java.util.Set<java.nio.file.StandardOpenOption>openOptionsstatic java.util.EnumSet<java.nio.file.StandardOpenOption>Rstatic java.util.EnumSet<java.nio.file.StandardOpenOption>RWstatic java.util.EnumSet<java.nio.file.StandardOpenOption>RWDstatic 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 voidclose()booleandelete()Close any open channels and then deletes the file.voidforce(boolean metaData)Performs a protectedFileChannel.force(boolean)call.java.io.FilegetFile()booleanisClosed()Check if a file was closed explicitly.private voidopen()Open a file channel for random access.intread(java.nio.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.private voidreopen(java.nio.channels.ClosedChannelException e)Reopen a channel closed by an exception, unless it was closed explicitly.longsize()Performs a protectedFileChannel.size()call.private static java.util.Set<java.nio.file.StandardOpenOption>toOpenOptions(java.lang.String mode)longtransferTo(long position, long count, java.nio.channels.WritableByteChannel target)Performs a protectedFileChannel.transferTo(long, long, WritableByteChannel)call.voidtruncate(long size)Performs a protectedFileChannel.truncate(long)call.intwrite(java.nio.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.
-
-
-
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.IOExceptionConstructor 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.IOExceptionConstructor Opens a file in a specific mode, creating a new one if the file doesn't exist.- Parameters:
file- filemode- 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.IOExceptionOpen a file channel for random access.- Throws:
java.io.IOException
-
reopen
private void reopen(java.nio.channels.ClosedChannelException e) throws java.io.IOExceptionReopen 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:
closein interfacejava.lang.AutoCloseable- Specified by:
closein interfacejava.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.IOExceptionClose 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.IOExceptionPerforms a protectedFileChannel.force(boolean)call.- Parameters:
metaData-- Throws:
java.io.IOException
-
truncate
public void truncate(long size) throws java.io.IOExceptionPerforms a protectedFileChannel.truncate(long)call.- Parameters:
size-- Throws:
java.io.IOException
-
size
public long size() throws java.io.IOExceptionPerforms a protectedFileChannel.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.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:
java.io.IOException
-
write
public int write(java.nio.ByteBuffer buf, long offset) throws java.io.IOExceptionPerforms a protectedFileChannel.write(ByteBuffer, long)call.- Parameters:
buf- bufferoffset- 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.IOExceptionPerforms a protectedFileChannel.read(ByteBuffer, long)call.- Parameters:
buf- buffer to readoffset- non-negative offset- Returns:
- number of bytes read
- Throws:
java.io.IOException
-
writeBytes
public void writeBytes(byte[] value, long offset) throws java.io.IOExceptionWrite byte array to channel starting at offset.- Parameters:
value- byte array to writeoffset- non-negative offset- Throws:
java.io.IOException
-
readBytes
public byte[] readBytes(long offset, int length) throws java.io.IOExceptionRead 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.IOExceptionWrite single byte to channel starting at offset.- Parameters:
value- value to writeoffset- non-negative offset- Throws:
java.io.IOException
-
readByte
public byte readByte(long offset) throws java.io.IOExceptionRead 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.IOExceptionWrite long value to channel starting at offset.- Parameters:
value- value to writeoffset- non-negative offset- Throws:
java.io.IOException
-
readLong
public long readLong(long offset) throws java.io.IOExceptionRead 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.IOExceptionWrite integer value to channel starting at offset.- Parameters:
value- value to writeoffset- non-negative offset- Throws:
java.io.IOException
-
readInt
public int readInt(long offset) throws java.io.IOExceptionRead integer value from channel starting at offset.- Parameters:
offset- non-negative offset- Returns:
- integer
- Throws:
java.io.IOException
-
-