Module jls.io.FileDescriptor
Provides OS file descriptor abstraction.
A FileDescriptor enables to manipulate files, reading from and writing to.
The method are provided both as synchronous and asynchronous. The synchronous form is suffixed by "Sync". The asynchronous form takes an optional callback as last argument, if omitted the method returns a jls.lang.Promise.
Usage:
-- synchronous local fileDesc, err = FileDescriptor.openSync('file_name.txt', 'r') local data = fileDesc:readSync(1024) print(data) fileDesc:closeSync() -- asynchronous FileDescriptor.open('file_name.txt', 'r'):next(function(fileDesc) return fileDesc:read(256):next(function(data) print(data) fileDesc:close() end) end)
Class FileDescriptor
fileDescriptor:read (size, offset[, callback]) | Reads the specified data from this file descriptor. |
fileDescriptor:readSync (size, offset) | Reads the specified data from this file descriptor. |
fileDescriptor:write (data, offset[, callback]) | Writes the specified data to this file descriptor. |
fileDescriptor:writeSync (data, offset) | Writes the specified data to this file descriptor. |
fileDescriptor:close ([callback]) | Closes this file descriptor. |
fileDescriptor:closeSync () | Closes this file descriptor. |
fileDescriptor:flush ([callback]) | Flushes all modified data of this file descriptor to the storage device. |
fileDescriptor:flushSync () | Flushes all modified data of this file descriptor to the storage device. |
FileDescriptor.openSync (path, mode) | Returns a new FileDescriptor for the specified path name. |
FileDescriptor.open (path, mode[, callback]) | Returns a new FileDescriptor for the specified path name. |
Class FileDescriptor
A FileDescriptor class.
A FileDescriptor instance represents a file handle.
- fileDescriptor:read (size, offset[, callback])
-
Reads the specified data from this file descriptor.
Parameters:
- size number The size of the data to read.
- offset number The optional position at which the read is to be performed,
- callback function The optional callback. (optional)
Returns:
-
a Promise or nil if a callback has been specified.
Usage:
fd:read(1024):then(function(data) -- process the data end) -- or when using a callback fd:read(1024, nil, function(err, data) if not err -- process the data end end)
- fileDescriptor:readSync (size, offset)
-
Reads the specified data from this file descriptor.
Parameters:
- size number The size of the data to read.
- offset number The optional position at which the read is to be performed, -1 or nil for the current file descriptor position.
Returns:
-
the data as a string, could be less than the specified size,
nil if the end of file is reached.
- fileDescriptor:write (data, offset[, callback])
-
Writes the specified data to this file descriptor.
Parameters:
- data The data to write as a string or an array of string.
- offset number The optional position at which the write is to be performed, -1 or nil for the current file descriptor position.
- callback function an optional callback function to use in place of promise. (optional)
Returns:
-
a Promise or nil if a callback has been specified.
- fileDescriptor:writeSync (data, offset)
-
Writes the specified data to this file descriptor.
Parameters:
- data The data to write as a string or an array of string.
- offset number The optional position at which the write is to be performed, -1 or nil for the current file descriptor position.
- fileDescriptor:close ([callback])
-
Closes this file descriptor.
Parameters:
- callback function The optional callback. (optional)
Returns:
-
a Promise or nil if a callback has been specified.
- fileDescriptor:closeSync ()
- Closes this file descriptor.
- fileDescriptor:flush ([callback])
-
Flushes all modified data of this file descriptor to the storage device.
Parameters:
- callback function The optional callback. (optional)
Returns:
-
a Promise or nil if a callback has been specified.
- fileDescriptor:flushSync ()
- Flushes all modified data of this file descriptor to the storage device.
- FileDescriptor.openSync (path, mode)
-
Returns a new FileDescriptor for the specified path name.
Parameters:
- path The file path as a string or a Path.
- mode string The mode to be used to open the file 'r', 'w' or 'a' with an optional '+'.
Returns:
-
a new FileDescriptor or nil.
Usage:
local fileDesc, err = FileDescriptor.openSync(pathName, 'r')
- FileDescriptor.open (path, mode[, callback])
-
Returns a new FileDescriptor for the specified path name.
Parameters:
- path The file path as a string or a Path.
- mode string The mode to be used to open the file, 'r' or 'w'.
- callback function an optional callback function to use in place of promise. (optional)
Returns:
-
a promise that resolves to a new FileDescriptor.