Class 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 of FileChannel.
    • 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.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • 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. The READ and WRITE options determine if the file should be opened for reading and/or writing. If neither option (or the APPEND 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 and WRITE, the following options may be present:

        additional options
        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 the READ or TRUNCATE_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 the close 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 file file-attributes to set atomically when creating the file.

        The new channel is created by invoking the newFileChannel method on the provider that created the Path.

        Parameters:
        path - The path of the file to open or create
        options - Options specifying how the file is opened
        ioExecutor - The thread pool or null to associate the channel with the default thread pool
        attrs - 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 options
        java.lang.UnsupportedOperationException - If the file 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 file
        java.io.IOException - If an I/O error occurs
        java.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, the SecurityManager.checkRead(String) method is invoked to check read access if the file is opened for reading. The SecurityManager.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.open(null, file, opts, new FileAttribute<?>[0]);
         
        where opts is a Set 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 create
        options - Options specifying how the file is opened
        Returns:
        A new file channel
        Throws:
        java.lang.IllegalArgumentException - If the set contains an invalid combination of options
        java.lang.UnsupportedOperationException - If the file is associated with a provider that does not support creating file channels, or an unsupported open option is specified
        java.io.IOException - If an I/O error occurs
        java.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, the SecurityManager.checkRead(String) method is invoked to check read access if the file is opened for reading. The SecurityManager.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 interface java.nio.channels.Channel
      • close

        @Suspendable
        public void close()
                   throws java.io.IOException
        Specified by:
        close in interface java.lang.AutoCloseable
        Specified by:
        close in interface java.nio.channels.Channel
        Specified by:
        close in interface java.io.Closeable
        Throws:
        java.io.IOException
      • position

        public long position()
                      throws java.io.IOException
        Specified by:
        position in interface java.nio.channels.SeekableByteChannel
        Throws:
        java.io.IOException
      • position

        public FiberFileChannel position​(long newPosition)
                                  throws java.io.IOException
        Specified by:
        position in interface java.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 transferred
        position - 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 negative
        java.nio.channels.NonReadableChannelException - If this channel was not opened for reading
        java.nio.channels.ClosedChannelException - If this channel is closed
        java.nio.channels.AsynchronousCloseException - If another thread closes this channel while the read operation is in progress
        java.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 status
        java.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 interface java.nio.channels.ReadableByteChannel
        Specified by:
        read in interface java.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 interface java.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 interface java.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 transferred
        position - 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 negative
        java.nio.channels.NonWritableChannelException - If this channel was not opened for writing
        java.nio.channels.ClosedChannelException - If this channel is closed
        java.nio.channels.AsynchronousCloseException - If another thread closes this channel while the write operation is in progress
        java.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 status
        java.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 interface java.nio.channels.SeekableByteChannel
        Specified by:
        write in interface java.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 interface java.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 interface java.nio.channels.GatheringByteChannel
        Throws:
        java.io.IOException
      • size

        public long size()
                  throws java.io.IOException
        Specified by:
        size in interface java.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 interface java.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