FixIO Documentation

Back to summary

import "io/file";

File class

File class represents an opened file. The reading and writing is unbuffered. You may want to wrap it in BufferedStream to get a better performance for small reads and writes.

Inherits from Stream.

Initialization

static function create(): File
Creates a new instance of file interface, intended for subclassing only.
static function open(path: Path or String, mode: Integer): File
Opens a file with given mode. The mode is consisting of these flags:
FILE_READ - open the file for reading
FILE_WRITE - open the file for writing
FILE_CREATE - create the file if it doesn't exist (requires write access)
FILE_TRUNCATE - truncate the file if it exists (requires write access)
FILE_APPEND - open for atomic writes at the end of the file (requires write access only)
You can also use these common values:
FILE_OPEN_EXISTING - open the file for reading/writing, give an error when it doesn't exist
FILE_OPEN_ALWAYS - open the file for reading/writing, create it when it doesn't exist
FILE_OPEN_NEW - open the file for reading/writing, create when it doesn't exist and truncate when it exists
FILE_OPEN_APPEND - open the file for atomic writing at the end, create when it doesn't exist

Common functions

static function read_whole(path: Path or String): Byte[]
static function read_whole(path: Path or String, buf: Byte[]): Byte[]
Reads the whole file into provided buffer (or a created one).
static function write_whole(path: Path or String, buf: Byte[])
static function write_whole(path: Path or String, buf: Byte[], off: Integer, len: Integer)
Writes the whole buffer (or portion) to the file.

File interface

virtual function get_length(): Long
Returns the length of the file.
virtual function set_length(len: Long)
Sets the length of the file.
virtual function get_position(): Long
Returns the current position in the file.
virtual function set_position(pos: Long)
Sets the current position in the file.
virtual function sync()
Synchronizes unwritten data with the data on the disk. This is implemented for native files but may do nothing for virtual files (that implements this interface).
virtual function lock(exclusive: Boolean, timeout: Integer): Boolean
virtual function unlock()
Acquires a lock on the file, either in a shared mode (allowing multiple readers) or in an exclusive mode (only a single writer). You can provide a timeout for waiting (negative value means no time limit, the default when the timeout is not provided). To change between modes the lock must be unlocked first (atomic upgrading/downgrading is not supported). Returns true when the lock was acquired.
Note: The lock is advisory on all platforms, meaning inappropriate accesses are not prevented. On Windows where locks are mandatory (enforced) this is emulated by locking the very last byte way outside of any supported file sizes.

Functions

function seek_rel(offset: Integer)
Sets the current position relative to current position.
function seek_set(offset: Integer)
Sets the current position relative to beginning.
function seek_end(offset: Integer)
Sets the current position relative to end.
function lock(exclusive: Boolean)
Short version of lock function without a timeout.

Native access

Note: these functions are optional (FixNative must be present) and they work on native files only.

function get_native_descriptor(): Integer
Returns the native file descriptor for this file.
function get_native_handle(): Pointer
Returns the native handle for this file.