AsyncFileChannel

abstract class AsyncFileChannel extends AutoCloseable

An asynchronous channel for reading, writing, and manipulating a file.

On the JVM this is a wrapper around java.nio.channels.AsynchronousFileChannel (class available since Java 7 for doing async I/O on files).

Example
 val out = AsyncFileChannel(File.createTempFile, StandardOpenOption.CREATE)
 val bytes = ByteBuffer.wrap("Hello world!".getBytes("UTF-8"))
 val future = out.write(bytes, 0)
 future.onComplete {
   case Success(nr) =>
     println("Bytes written: %d".format(nr))
   case Failure(exc) =>
     println(s"ERROR: " + exc.getMessage)
 }
Companion
object
trait AutoCloseable
class Object
trait Matchable
class Any

Value members

Abstract methods

def flush(writeMetaData: Boolean, cb: Callback[Throwable, Unit]): Unit

Forces any updates to this channel's file to be written to the storage device that contains it.

Forces any updates to this channel's file to be written to the storage device that contains it.

   Invoking this method might trigger an
   [[http://man7.org/linux/man-pages/man2/fdatasync.2.html fsync or fdatasync]]
   operation, which transfers all modified in-core data of
   the file to the disk device, so that all changed
   information can be retrieved even after the system crashed
   or was rebooted. If the `writeMetaData` is set to `true`,
   then this would be the equivalent of an `fsync` command,
   or `fdatasync` if set to false.

   This method is only guaranteed to force changes that were
   made to this channel's file via the methods defined in
   this class.
Value Params
cb

is a callback to be called when the asynchronous operation succeeds, or for signaling errors

writeMetaData

if true then this method is required to force changes to both the file's content and metadata to be written to storage; otherwise, it need only force content changes to be written

def isOpen: Boolean

Returns true if this channel is open, or false otherwise.

Returns true if this channel is open, or false otherwise.

def read(dst: ByteBuffer, position: Long, cb: Callback[Throwable, Int]): Unit

Reads a sequence of bytes from this channel into the given buffer, starting at the given file position.

Reads a sequence of bytes from this channel into the given buffer, starting at the given file position.

Value Params
cb

is the callback to be called with the result, once this asynchronous operation is complete . For this method it signals the number of bytes read or -1 if the given position is greater than or equal to the file's size at the time the read is attempted.

dst

is the buffer holding the bytes read on completion

position

is the position in the opened channel from where to read

def size(cb: Callback[Throwable, Long]): Unit

Returns the current size of this channel's file, measured in bytes.

Returns the current size of this channel's file, measured in bytes.

def write(src: ByteBuffer, position: Long, cb: Callback[Throwable, Int]): Unit

Writes a sequence of bytes to this channel from the given buffer, starting at the given file position.

Writes a sequence of bytes to this channel from the given buffer, starting at the given file position.

   If the given position is greater than the file's size, at
   the time that the write is attempted, 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.
Value Params
cb

is the callback to be called with the result, once this asynchronous operation is complete . For this method it signals the number of bytes that were written

position

is the position in file where to write, starts from 0, must be positive

src

is the buffer holding the sequence of bytes to write

Concrete methods

def flush(writeMetaData: Boolean): Future[Unit]

Forces any updates to this channel's file to be written to the storage device that contains it.

Forces any updates to this channel's file to be written to the storage device that contains it.

   Invoking this method might trigger an
   [[http://man7.org/linux/man-pages/man2/fdatasync.2.html fsync or fdatasync]]
   operation, which transfers all modified in-core data of
   the file to the disk device, so that all changed
   information can be retrieved even after the system crashed
   or was rebooted. If the `writeMetaData` is set to `true`,
   then this would be the equivalent of an `fsync` command,
   or `fdatasync` if set to false.

   This method is only guaranteed to force changes that were
   made to this channel's file via the methods defined in
   this class.
Value Params
writeMetaData

if true then this method is required to force changes to both the file's content and metadata to be written to storage; otherwise, it need only force content changes to be written

def read(dst: ByteBuffer, position: Long): Future[Int]

Reads a sequence of bytes from this channel into the given buffer, starting at the given file position.

Reads a sequence of bytes from this channel into the given buffer, starting at the given file position.

Value Params
dst

is the buffer holding the bytes read on completion

position

is the position in the opened channel from where to read

Returns

the number of bytes read or -1 if the given position is greater than or equal to the file's size at the time the read is attempted.

def size: Future[Long]

Returns the current size of this channel's file, measured in bytes.

Returns the current size of this channel's file, measured in bytes.

def write(src: ByteBuffer, position: Long): Future[Int]

Writes a sequence of bytes to this channel from the given buffer, starting at the given file position.

Writes a sequence of bytes to this channel from the given buffer, starting at the given file position.

   If the given position is greater than the file's size, at
   the time that the write is attempted, 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.
Value Params
position

is the position in file where to write, starts from 0, must be positive

src

is the buffer holding the sequence of bytes to write

Returns

the number of bytes that were written

Inherited methods

@throws(java.lang.Exception)
def close(): Unit
Inherited from
AutoCloseable