Class FiberFileChannel
- java.lang.Object
-
- co.paralleluniverse.fibers.io.FiberFileChannel
-
- All Implemented Interfaces:
java.io.Closeable
,java.lang.AutoCloseable
,java.nio.channels.ByteChannel
,java.nio.channels.Channel
,java.nio.channels.GatheringByteChannel
,java.nio.channels.ReadableByteChannel
,java.nio.channels.ScatteringByteChannel
,java.nio.channels.SeekableByteChannel
,java.nio.channels.WritableByteChannel
public class FiberFileChannel extends java.lang.Object implements java.nio.channels.SeekableByteChannel, java.nio.channels.GatheringByteChannel, java.nio.channels.ScatteringByteChannel
A fiber-blocking version ofFileChannel
.
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description void
close()
void
force(boolean metaData)
boolean
isOpen()
java.nio.channels.FileLock
lock(long position, long size, boolean shared)
java.nio.MappedByteBuffer
map(java.nio.channels.FileChannel.MapMode mode, long position, long size)
static FiberFileChannel
open(java.nio.file.Path path, java.nio.file.OpenOption... options)
Opens or creates a file for reading and/or writing, returning a file channel to access the file.static FiberFileChannel
open(java.util.concurrent.ExecutorService ioExecutor, java.nio.file.Path path, java.util.Set<? extends java.nio.file.OpenOption> options, java.nio.file.attribute.FileAttribute<?>... attrs)
Opens or creates a file, returning a file channel to access the file.long
position()
FiberFileChannel
position(long newPosition)
int
read(java.nio.ByteBuffer dst)
long
read(java.nio.ByteBuffer[] dsts)
Reads a sequence of bytes from this channel into the given buffers.long
read(java.nio.ByteBuffer[] dsts, int offset, int length)
int
read(java.nio.ByteBuffer dst, long position)
Reads a sequence of bytes from this channel into the given buffer, starting at the given file position.long
size()
long
transferFrom(java.nio.channels.ReadableByteChannel src, long position, long count)
long
transferTo(long position, long count, java.nio.channels.WritableByteChannel target)
FiberFileChannel
truncate(long size)
java.nio.channels.FileLock
tryLock(long position, long size, boolean shared)
int
write(java.nio.ByteBuffer src)
long
write(java.nio.ByteBuffer[] srcs)
long
write(java.nio.ByteBuffer[] srcs, int offset, int length)
int
write(java.nio.ByteBuffer src, long position)
Writes a sequence of bytes to this channel from the given buffer, starting at the given file position.
-
-
-
Method Detail
-
open
@Suspendable public static FiberFileChannel open(java.util.concurrent.ExecutorService ioExecutor, java.nio.file.Path path, java.util.Set<? extends java.nio.file.OpenOption> options, java.nio.file.attribute.FileAttribute<?>... attrs) throws java.io.IOException
Opens or creates a file, returning a file channel to access the file.The
options
parameter determines how the file is opened. TheREAD
andWRITE
options determine if the file should be opened for reading and/or writing. If neither option (or theAPPEND
option) is contained in the array then the file is opened for reading. By default reading or writing commences at the beginning of the file.In the addition to
READ
andWRITE
, the following options may be present:Option Description APPEND
If this option is present then the file is opened for writing and each invocation of the channel's write
method first advances the position to the end of the file and then writes the requested data. Whether the advancement of the position and the writing of the data are done in a single atomic operation is system-dependent and therefore unspecified. This option may not be used in conjunction with theREAD
orTRUNCATE_EXISTING
options.TRUNCATE_EXISTING
If this option is present then the existing file is truncated to a size of 0 bytes. This option is ignored when the file is opened only for reading. CREATE_NEW
If this option is present then a new file is created, failing if the file already exists. When creating a file the check for the existence of the file and the creation of the file if it does not exist is atomic with respect to other file system operations. This option is ignored when the file is opened only for reading. CREATE
If this option is present then an existing file is opened if it exists, otherwise a new file is created. When creating a file the check for the existence of the file and the creation of the file if it does not exist is atomic with respect to other file system operations. This option is ignored if the CREATE_NEW
option is also present or the file is opened only for reading.DELETE_ON_CLOSE
When this option is present then the implementation makes a best effort attempt to delete the file when closed by the close
method. If theclose
method is not invoked then a best effort attempt is made to delete the file when the Java virtual machine terminates.SPARSE
When creating a new file this option is a hint that the new file will be sparse. This option is ignored when not creating a new file. SYNC
Requires that every update to the file's content or metadata be written synchronously to the underlying storage device. (see Synchronized I/O file integrity). DSYNC
Requires that every update to the file's content be written synchronously to the underlying storage device. (see Synchronized I/O file integrity). An implementation may also support additional options.
The
attrs
parameter is an optional array of filefile-attributes
to set atomically when creating the file.The new channel is created by invoking the
newFileChannel
method on the provider that created thePath
.- Parameters:
path
- The path of the file to open or createoptions
- Options specifying how the file is openedioExecutor
- The thread pool ornull
to associate the channel with the default thread poolattrs
- An optional list of file attributes to set atomically when creating the file- Returns:
- A new file channel
- Throws:
java.lang.IllegalArgumentException
- If the set contains an invalid combination of optionsjava.lang.UnsupportedOperationException
- If thefile
is associated with a provider that does not support creating asynchronous file channels, or an unsupported open option is specified, or the array contains an attribute that cannot be set atomically when creating the filejava.io.IOException
- If an I/O error occursjava.lang.SecurityException
- If a security manager is installed and it denies an unspecified permission required by the implementation. In the case of the default provider, theSecurityManager.checkRead(String)
method is invoked to check read access if the file is opened for reading. TheSecurityManager.checkWrite(String)
method is invoked to check write access if the file is opened for writing
-
open
@Suspendable public static FiberFileChannel open(java.nio.file.Path path, java.nio.file.OpenOption... options) throws java.io.IOException
Opens or creates a file for reading and/or writing, returning a file channel to access the file.An invocation of this method behaves in exactly the same way as the invocation
ch.
whereopen
(null, file, opts, new FileAttribute<?>[0]);opts
is aSet
containing the options specified to this method.The resulting channel is associated with default thread pool to which tasks are submitted to handle I/O events and dispatch to completion handlers that consume the result of asynchronous operations performed on the resulting channel.
- Parameters:
path
- The path of the file to open or createoptions
- Options specifying how the file is opened- Returns:
- A new file channel
- Throws:
java.lang.IllegalArgumentException
- If the set contains an invalid combination of optionsjava.lang.UnsupportedOperationException
- If thefile
is associated with a provider that does not support creating file channels, or an unsupported open option is specifiedjava.io.IOException
- If an I/O error occursjava.lang.SecurityException
- If a security manager is installed and it denies an unspecified permission required by the implementation. In the case of the default provider, theSecurityManager.checkRead(String)
method is invoked to check read access if the file is opened for reading. TheSecurityManager.checkWrite(String)
method is invoked to check write access if the file is opened for writing
-
isOpen
public final boolean isOpen()
- Specified by:
isOpen
in interfacejava.nio.channels.Channel
-
close
@Suspendable public void close() throws java.io.IOException
- Specified by:
close
in interfacejava.lang.AutoCloseable
- Specified by:
close
in interfacejava.nio.channels.Channel
- Specified by:
close
in interfacejava.io.Closeable
- Throws:
java.io.IOException
-
position
public long position() throws java.io.IOException
- Specified by:
position
in interfacejava.nio.channels.SeekableByteChannel
- Throws:
java.io.IOException
-
position
public FiberFileChannel position(long newPosition) throws java.io.IOException
- Specified by:
position
in interfacejava.nio.channels.SeekableByteChannel
- Throws:
java.io.IOException
-
read
@Suspendable public int read(java.nio.ByteBuffer dst, long position) throws java.io.IOException
Reads a sequence of bytes from this channel into the given buffer, starting at the given file position.This method works in the same manner as the
read(ByteBuffer)
method, except that bytes are read starting at the given file position rather than at the channel's current position. This method does not modify this channel's position. If the given position is greater than the file's current size then no bytes are read.- Parameters:
dst
- The buffer into which bytes are to be transferredposition
- The file position at which the transfer is to begin; must be non-negative- Returns:
- The number of bytes read, possibly zero, or
-1
if the given position is greater than or equal to the file's current size - Throws:
java.lang.IllegalArgumentException
- If the position is negativejava.nio.channels.NonReadableChannelException
- If this channel was not opened for readingjava.nio.channels.ClosedChannelException
- If this channel is closedjava.nio.channels.AsynchronousCloseException
- If another thread closes this channel while the read operation is in progressjava.nio.channels.ClosedByInterruptException
- If another thread interrupts the current thread while the read operation is in progress, thereby closing the channel and setting the current thread's interrupt statusjava.io.IOException
- If some other I/O error occurs
-
read
@Suspendable public int read(java.nio.ByteBuffer dst) throws java.io.IOException
- Specified by:
read
in interfacejava.nio.channels.ReadableByteChannel
- Specified by:
read
in interfacejava.nio.channels.SeekableByteChannel
- Throws:
java.io.IOException
-
read
@Suspendable public final long read(java.nio.ByteBuffer[] dsts) throws java.io.IOException
Reads a sequence of bytes from this channel into the given buffers.Bytes are read starting at this channel's current file position, and then the file position is updated with the number of bytes actually read. Otherwise this method behaves exactly as specified in the
ScatteringByteChannel
interface.- Specified by:
read
in interfacejava.nio.channels.ScatteringByteChannel
- Throws:
java.io.IOException
-
read
@Suspendable public long read(java.nio.ByteBuffer[] dsts, int offset, int length) throws java.io.IOException
- Specified by:
read
in interfacejava.nio.channels.ScatteringByteChannel
- Throws:
java.io.IOException
-
write
@Suspendable public int write(java.nio.ByteBuffer src, long position) throws java.io.IOException
Writes a sequence of bytes to this channel from the given buffer, starting at the given file position.This method works in the same manner as the
write(ByteBuffer)
method, except that bytes are written starting at the given file position rather than at the channel's current position. This method does not modify this channel's position. If the given position is greater than the file's current size then the file will be grown to accommodate the new bytes; the values of any bytes between the previous end-of-file and the newly-written bytes are unspecified.- Parameters:
src
- The buffer from which bytes are to be transferredposition
- The file position at which the transfer is to begin; must be non-negative- Returns:
- The number of bytes written, possibly zero
- Throws:
java.lang.IllegalArgumentException
- If the position is negativejava.nio.channels.NonWritableChannelException
- If this channel was not opened for writingjava.nio.channels.ClosedChannelException
- If this channel is closedjava.nio.channels.AsynchronousCloseException
- If another thread closes this channel while the write operation is in progressjava.nio.channels.ClosedByInterruptException
- If another thread interrupts the current thread while the write operation is in progress, thereby closing the channel and setting the current thread's interrupt statusjava.io.IOException
- If some other I/O error occurs
-
write
@Suspendable public int write(java.nio.ByteBuffer src) throws java.io.IOException
- Specified by:
write
in interfacejava.nio.channels.SeekableByteChannel
- Specified by:
write
in interfacejava.nio.channels.WritableByteChannel
- Throws:
java.io.IOException
-
write
@Suspendable public long write(java.nio.ByteBuffer[] srcs, int offset, int length) throws java.io.IOException
- Specified by:
write
in interfacejava.nio.channels.GatheringByteChannel
- Throws:
java.io.IOException
-
write
@Suspendable public final long write(java.nio.ByteBuffer[] srcs) throws java.io.IOException
- Specified by:
write
in interfacejava.nio.channels.GatheringByteChannel
- Throws:
java.io.IOException
-
size
public long size() throws java.io.IOException
- Specified by:
size
in interfacejava.nio.channels.SeekableByteChannel
- Throws:
java.io.IOException
-
lock
@Suspendable public java.nio.channels.FileLock lock(long position, long size, boolean shared) throws java.io.IOException
- Throws:
java.io.IOException
-
force
public void force(boolean metaData) throws java.io.IOException
- Throws:
java.io.IOException
-
truncate
public FiberFileChannel truncate(long size) throws java.io.IOException
- Specified by:
truncate
in interfacejava.nio.channels.SeekableByteChannel
- Throws:
java.io.IOException
-
tryLock
public java.nio.channels.FileLock tryLock(long position, long size, boolean shared) throws java.io.IOException
- Throws:
java.io.IOException
-
transferTo
public long transferTo(long position, long count, java.nio.channels.WritableByteChannel target) throws java.io.IOException
- Throws:
java.io.IOException
-
transferFrom
public long transferFrom(java.nio.channels.ReadableByteChannel src, long position, long count) throws java.io.IOException
- Throws:
java.io.IOException
-
map
public java.nio.MappedByteBuffer map(java.nio.channels.FileChannel.MapMode mode, long position, long size) throws java.io.IOException
- Throws:
java.io.IOException
-
-