A Task based 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 = TaskFileChannel(File.createTempFile, StandardOpenOption.CREATE) val bytes = ByteBuffer.wrap("Hello world!".getBytes("UTF-8")) out.write(bytes, 0).runAsync(new Callback[Int] { override def onError(ex: Throwable) = println(s"ERROR: " + ex.getMessage) override def onSuccess(value: Int) = println("Bytes written: %d".format(value)) })- Companion
- object
Value members
Concrete methods
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
truethen 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
Returns true if this channel is open, or false otherwise.
Returns true if this channel is open, or false otherwise.
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.
Returns the current size of this channel's file, measured in bytes.
Returns the current size of this channel's file, measured in bytes.
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