Namespace: io

airlock.io

Provides APIs for reading and writing files.
Source:

Members

(static, readonly) FileMode :Number

Enum FileMode
Type:
  • Number
Properties:
Name Type Description
CREATE_NEW Number
CREATE Number Specifies that the operating system should create a new file. If the file already exists, it will be overwritten. This requires Write permission. FileMode.Create is equivalent to requesting that if the file does not exist, use CreateNew; otherwise, use Truncate. If the file already exists but is a hidden file, an UnauthorizedAccessException exception is thrown.
OPEN Number Specifies that the operating system should open an existing file. A FileNotFoundException exception is thrown if the file does not exist.
OPEN_OR_CREATE Number Specifies that the operating system should open a file if it exists; otherwise, a new file should be created.
TRUNCATE Number Specifies that the operating system should open an existing file. When the file is opened, it should be truncated so that its size is zero bytes. This requires Write permission. Attempting to read from a file opened with FileMode.Truncate causes an ArgumentException exception.
APPEND Number Opens the file if it exists and seeks to the end of the file, or creates a new file. This requires Append permission. FileMode.Append can be used only in conjunction with FileAccess.Write. Trying to seek to a position before the end of the file throws an IOException exception, and any attempt to read fails and throws a NotSupportedException exception.
Source:
See:

Methods

(static) closeFile(handle) → {Promise}

Closes the file stream that is presently open for the file. Upon closing a file, its handle becomes invalid.
Parameters:
Name Type Description
handle number A file identifier for the file that was previously returned from openFile.
Source:
Returns:
A promise that when resolved confirms that the file has been closed.
Type
Promise
Example
airlock.io.closeFile(file1Handle)
		.then(function () {
			alert(`File closed.`);
		}).catch(function (error) {
			alert(`Error ${error}`);
		});

(static) copyFile(sourcePath, destinationPath, overwriteIfExistsopt) → {Promise}

Copies the file at the specified sourcePath to the destinationPath. Returns a Promise that when resolved indicates that the file has finished copying. If the Promise is rejected, an error is returned.
Parameters:
Name Type Attributes Description
sourcePath string The path to the file that is to be copied.
destinationPath string The location of where to copy the file to.
overwriteIfExists boolean <optional>
If this function is called and the destination file already exists, and error is raised. However, if you specify overwriteExists as true, that an exception is not raised and the destination file is replaced with the source file.
Source:
Returns:
Indicating completion or failure of the copy operation. An error is produced by the Promise if an IO Exception is raised.
Type
Promise
Example
airlock.io.copyFile(sourcePath, destinationPath)
		.then(function () {
			alert(`File copied to: ${destinationPath}`);
		}).catch(function (error) {
			alert(`Error ${error}`);
		});

(static) createDirectory(path) → {Promise}

Creates a directory at the specified path. Note that, this function has no effect, and does not raise an error, if the directory already exists.
Parameters:
Name Type Description
path string The path to the directory to be created.
Source:
Returns:
When resolved the promise confirms that the directory has been created.
Type
Promise
Example
airlock.io.createDirectory(newDir)
		.then(function () {
			alert(`Created directory ${newDir}`);
		}).catch(function (error) {
			alert(`Error ${error}`);
		});

(static) deleteDirectory(path, recursiveopt) → {Promise}

Deletes the directory at the specified location. A promise is returned indicating success or failure of the operation.
Parameters:
Name Type Attributes Default Description
path string The file system path to the directory that is to be deleted.
recursive boolean <optional>
false If true, then the directory and its contents are deleted. If false, and the directory is not empty, an IOException is raised.
Source:
Returns:
When resolved the promise confirms that the directory has been deleted.
Type
Promise
Example
airlock.io.deleteDirectory(newDir, recursive)
		.then(function () {
			alert(`Deleted directory ${newDir}`);
		}).catch(function (error) {
			alert(`Error ${error}`);
		});

(static) deleteFile(path) → {Promise}

Deletes a file at the specified location. Returns a promise indicating success or failure of the operation.
Parameters:
Name Type Description
path string The location of the file to be deleted.
Source:
Returns:
If the promise is rejected it indicates the delete was unsuccessful.
Type
Promise
Example
airlock.io.deleteFile(filePath)
	.then(function () {
		alert(`File deleted.`);
	}).catch(function (error) {
		alert(`Error ${error}`);
	});

(static) directoryExists(path) → {Promise.<boolean>}

Returns a Promise indicating if the directory at the specified location, exists.
Parameters:
Name Type Description
path string The path to the directory. Cannot be null.
Source:
Returns:
True if the file exists; false otherwise.
Type
Promise.<boolean>
Example
airlock.io.directoryExists(directoryPath)
	.then(function (exists) {
		alert(`Directory exists: ${exists}`);
	}).catch(function (error) {
		alert(`Error: ${error}`);
	});

(static) fileExists(path) → {Promise.<boolean>}

Returns a Promise indicating if the file at the specified location, exists.
Parameters:
Name Type Description
path string The path to the file. Cannot be null.
Source:
Returns:
True if the file exists; false otherwise.
Type
Promise.<boolean>
Example
airlock.io.fileExists(filePath)
	.then(function (fileExists) {
		alert(`File exists: ${fileExists}`);
	}).catch(function (error) {
		alert(`Error: ${error}`);
	});

(static) getAppFilesDirectory() → {string}

Gets Airlock Browser's files directory. This is typically located at /data/data/com.outcoder.ibrowser/files
Source:
Returns:
The path to the application's files directory.
Type
string

(static) getDirectories(directoryPath, searchPatternopt, recursiveopt) → {Promise.<Array.<string>>}

Returns a list of directory names (including their paths) via a Promise.
Parameters:
Name Type Attributes Description
directoryPath string The path of the directory to look in.
searchPattern string <optional>
The search pattern to match against the names of directories. This parameter can contain a combination of valid literal path and wildcard (* and ?) characters, but it does not support regular expressions.
recursive boolean <optional>
If true, directories in nested directories are also returned.
Source:
Returns:
The names of the files identified as matching the query.
Type
Promise.<Array.<string>>
Example
airlock.io.getDirectories(outputDir, "*.*", true)
		.then(function (result) {
			alert(`${outputDir} contains: ${result.join("\n")}`);
		}).catch(function (error) {
			alert(`Error ${error}`);
		});

(static) getExternalStorageDirectory() → {string}

Gets the path to the shared directory on the device. This may or may not be located on an SD card.
Source:
See:
Returns:
The path to the device's shared storage location for apps.
Type
string

(static) getFileInfo(path) → {airlock.io.FileInfo}

Retrieves the information pertaining to the file at the specified location.
Parameters:
Name Type Description
path string The path to the file.
Source:
Returns:
The promise resolves a FileInfo object that contains the directory, size, modified/created/accessed times.
Type
airlock.io.FileInfo
Example
airlock.io.getFileInfo(sourcePath)
		.then(function (result) {
			alert(result.sizeBytes);
		}).catch(function (error) {
			alert(`Error ${error}`);
		});

(static) getFileOffset(handle) → {number}

Gets the current location (byte offset) within a file. When a read or write operation is undertaken it will occur at the location specified by the offset parameter.
Parameters:
Name Type Description
handle number The file identifier.
Source:
Returns:
The current offset in bytes.
Type
number

(static) getFileSizeBytes(handle) → {number}

Gets the size of a file in bytes.
Parameters:
Name Type Description
handle number The file identifier.
Source:
Returns:
The size of the file in bytes.
Type
number

(static) getFiles(directoryPath, searchPatternopt, recursiveopt) → {Promise.<Array.<string>>}

Returns a list of file names (including their paths) via a Promise.
Parameters:
Name Type Attributes Description
directoryPath string The path of the directory to look in.
searchPattern string <optional>
The search pattern to match against the names of files. This parameter can contain a combination of valid literal path and wildcard (* and ?) characters, but it does not support regular expressions.
recursive boolean <optional>
If true, files in nested directories are also returned.
Source:
Returns:
The names of the files identified as matching the query.
Type
Promise.<Array.<string>>
Example
airlock.io.getFiles(directoryPath, "*.*", true)
		.then(function (result) {
			alert(`${directoryPath} contains: ${result.join("\n")}`);
		}).catch(function (error) {
			alert(`Error ${error}`);
		});

(static) moveFile(sourcePath, destinationPath) → {Promise}

Moves the file at the specified sourcePath to the destinationPath. Returns a Promise that when resolved indicates that the file has finished being moved. If the Promise is rejected, an error is returned.
Parameters:
Name Type Description
sourcePath string The path to the file that is to be move from.
destinationPath string The location of where to move the file to.
Source:
See:
Returns:
Indicating completion or failure of the move operation. An error is produced by the Promise if an IO Exception is thrown.
Type
Promise
Example
airlock.io.moveFile(sourcePath, destinationPath)
	.then(function () {
		alert(`File moved to: ${destinationPath}`);
	}).catch(function (error) {
		alert(`Error ${error}`);
	});

(static) openFile(path, fileMode) → {Promise.<number>}

Use this function to open or create a file. It returns a handle (file identifier) to the file, via a Promise. Once the handle is obtained, the file can be written to or read from using the identifier (depending on the specified fileMode).
Parameters:
Name Type Description
path string The path to the file.
fileMode airlock.io.FileMode The file mode, which determines what can be done with the file and whether the file should be opened, created or so forth.
Source:
Returns:
A promise that resolves to a file identifier.
Type
Promise.<number>
Example
var file1Handle;

airlock.io.openFile(filePath, airlock.io.FileMode.OPEN_OR_CREATE)
		.then(function (fileHandle) {
			file1Handle = fileHandle;
			alert(`Successfully opened ${filePath}`);
		}).catch(function (error) {
			alert(`Error ${error}`);
		});

(static) readAllText(filePath) → {Promise.<string>}

Reads the entire contents of a file as text.
Parameters:
Name Type Description
filePath string The path to the file.
Source:
Throws:
A sharing violation exception is thrown if you have an open file handle for the file when using this API.
Returns:
A promise that resolves a string that is the contents of the file.
Type
Promise.<string>
Example
airlock.io.readAllText(filePath)
	.then(function (text) {
		alert(`${text}`);
	}).catch(function (error) {
		alert(`Error: ${error}`);
	});

(static) readBase64(handle, length, offsetopt) → {Promise.<string>}

Reads the contents of a file as bytes and returns them as a base64 string.
Parameters:
Name Type Attributes Description
handle number The file identifier.
length number The length (in bytes) of the file to read.
offset number <optional>
The offset within the file to start reading. If not specified, the current file offset is used.
Source:
Returns:
A promise that resolves a base64 string read from the file.
Type
Promise.<string>
Example
var fileLength = airlock.io.getFileSizeBytes(binaryFile1Handle);

airlock.io.readBase64(binaryFile1Handle, fileLength, 0)
	.then(function (base64) {
		var text = atob(base64);
		alert(text);
	}).catch(function (error) {
		alert(`Error ${error}`);
	});

(static) readLine(handle) → {Promise.<string>}

Reads a line of text from a file.
Parameters:
Name Type Description
handle number The file identifier.
Source:
Returns:
A promise that resolves a line of text that was read from the file.
Type
Promise.<string>
Example
airlock.io.readLine(file1Handle)
	.then(function (text) {
		alert(`${text}`);
	}).catch(function (error) {
		alert(`Error: ${error}`);
	});

(static) readText(handle, length, offsetopt) → {Promise.<string>}

Reads the contents of a file as text.
Parameters:
Name Type Attributes Description
handle number The file identifier.
length number The length (in bytes) of the file to read.
offset number <optional>
The offset within the file to start reading. If not specified, the current file offset is used.
Source:
Returns:
A promise that resolves a string of text read from the file.
Type
Promise.<string>
Example
var fileLength = airlock.io.getFileSizeBytes(file1Handle);

airlock.io.readText(file1Handle, fileLength, 0)
	.then(function (text) {
		alert(text);
	}).catch(function (error) {
		alert(`Error ${error}`);
	});

(static) seek(handle, offset)

Seeks to a location (byte offset) within a file. After which, when a read or write operation is undertaken it will occur at the location specified by the offset parameter.
Parameters:
Name Type Description
handle number The file identifier.
offset number The offset in bytes to move to.
Source:
Example
let position = 1024; // 1024 bytes past the start of the file.
airlock.io.seek(file1Handle, position);

(static) touch(filePath)

Updates the last modified timestamp of a file to the current time. If the file does not exist, it is created.
Parameters:
Name Type Description
filePath string The file path.
Source:
Throws:
Occurs if an IO exception is raised internally.

(static) writeBase64(handle, base64String) → {Promise}

Writes the specified base64 string to a file with the specified handle (file identifier) at the file's current offset.
Parameters:
Name Type Description
handle number The file identifier.
base64String string The base64 text to write to the file.
Source:
Returns:
A promise that when resolved indicates success of the operation.
Type
Promise
Example
var textToWrite = "That rug really tied the room together.";

var base64 = btoa(textToWrite);

airlock.io.writeBase64(binaryFile1Handle, base64)
	.then(function () {
		alert("Done");
	}).catch(function (error) {
		alert(`Error ${error}`);
	});

(static) writeText(handle, text) → {Promise}

Writes the specified text to a file with the specified handle (file identifier) at the file's current offset.
Parameters:
Name Type Description
handle number The file identifier.
text string The text to write to the file.
Source:
Throws:
Occurs if text is undefined.
Returns:
A promise that when resolved indicates success of the operation.
Type
Promise
Example
airlock.io.writeText(file1Handle, "These pretzels are making me thirsty.")
	.then(function () {
		alert(textElement, "Done");
	}).catch(function (error) {
		alert(textElement, `Error ${error}`);
	});

Type Definitions

FileInfo

Properties:
Name Type Description
directory string The path to the file's directory.
sizeBytes Number The size of the file in bytes.
modifiedTime Date The date and time that the file was last modified in universal time.
modifiedTimeUtc Date The date and time that the file was last modified in universal time.
creationTime Date The date and time that the file was created.
creationTimeUtc Date The date and time that the file was created in universal time.
accessedTime Date The date and time that the file was last accessed.
accessedTimeUtc Date The date and time that the file was last accessed in universal time.
Source: