mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-11-05 13:26:48 +08:00
binary sync read/write added
This commit is contained in:
@@ -224,6 +224,24 @@ export var testFileRead = function () {
|
||||
// </snippet>
|
||||
};
|
||||
|
||||
export var testFileReadWriteBinary = function () {
|
||||
// <snippet module="file-system" title="file-system">
|
||||
// ### Reading/writing binary data from/to a File
|
||||
// ``` JavaScript
|
||||
var fileName = "logo.png";
|
||||
|
||||
var sourceFile = fs.knownFolders.currentApp().getFile(fileName);
|
||||
var destinationFile = fs.knownFolders.temp().getFile(fileName);
|
||||
|
||||
destinationFile.writeSync(sourceFile.readSync());
|
||||
|
||||
TKUnit.assertEqual(sourceFile.readSync(), destinationFile.readSync());
|
||||
|
||||
destinationFile.removeSync();
|
||||
// ```
|
||||
// </snippet>
|
||||
};
|
||||
|
||||
export var testGetKnownFolders = function () {
|
||||
// <snippet module="file-system" title="file-system">
|
||||
// ### Getting the Known Folders
|
||||
|
||||
@@ -192,6 +192,35 @@ export class FileSystemAccess {
|
||||
return dir.getAbsolutePath();
|
||||
}
|
||||
|
||||
public read(path: string, onError?: (error: any) => any) {
|
||||
try {
|
||||
var javaFile = new java.io.File(path);
|
||||
var stream = new java.io.FileInputStream(javaFile);
|
||||
var bytes = new byte[javaFile.length()];
|
||||
var dataInputStream = new java.io.DataInputStream(stream);
|
||||
dataInputStream.readFully(bytes);
|
||||
return bytes;
|
||||
} catch (exception) {
|
||||
if (onError) {
|
||||
onError(exception);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public write(path: string, content: java.io.ByteArrayOutputStream, onError?: (error: any) => any) {
|
||||
try {
|
||||
var javaFile = new java.io.File(path);
|
||||
var stream = new java.io.FileOutputStream(javaFile);
|
||||
var bytes = content.toByteArray();
|
||||
stream.write(bytes, 0, bytes.length);
|
||||
stream.close();
|
||||
} catch (exception) {
|
||||
if (onError) {
|
||||
onError(exception);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public readText(path: string, onError?: (error: any) => any, encoding?: any) {
|
||||
try {
|
||||
var types: typeof typesModule = require("utils/types");
|
||||
|
||||
17
file-system/file-system-access.d.ts
vendored
17
file-system/file-system-access.d.ts
vendored
@@ -108,6 +108,14 @@
|
||||
*/
|
||||
readText(path: string, onError?: (error: any) => any, encoding?: any): string;
|
||||
|
||||
/**
|
||||
* Reads a binary content from a file with a given path.
|
||||
* @param path The path to the source file.
|
||||
* @param onSuccess A callback function which is called when a text is red.
|
||||
* @param onError (optional) A callback function to use if any error occurs.
|
||||
*/
|
||||
read(path: string, onError?: (error: any) => any): any;
|
||||
|
||||
/**
|
||||
* Writes a text to a file with a given path.
|
||||
* @param path The path to the source file.
|
||||
@@ -118,6 +126,15 @@
|
||||
*/
|
||||
writeText(path: string, content: string, onError?: (error: any) => any, encoding?: any);
|
||||
|
||||
/**
|
||||
* Writes a binary to a file with a given path.
|
||||
* @param path The path to the source file.
|
||||
* @param content The content which will be written to the file.
|
||||
* @param onSuccess (optional) A callback function which is called when a text is written.
|
||||
* @param onError (optional) A callback function to use if any error occurs.
|
||||
*/
|
||||
write(path: string, content: any, onError?: (error: any) => any);
|
||||
|
||||
/**
|
||||
* Gets extension of the file with a given path.
|
||||
* @param path A path to the file.
|
||||
|
||||
@@ -239,6 +239,17 @@ export class FileSystemAccess {
|
||||
}
|
||||
}
|
||||
|
||||
public read(path: string, onError?: (error: any) => any) : NSData {
|
||||
try {
|
||||
return NSData.dataWithContentsOfFile(path);
|
||||
}
|
||||
catch (ex) {
|
||||
if (onError) {
|
||||
onError(new Error("Failed to read file at path '" + path + "': " + ex));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public writeText(path: string, content: string, onError?: (error: any) => any, encoding?: any) {
|
||||
var nsString = NSString.alloc().initWithString(content);
|
||||
|
||||
@@ -258,6 +269,17 @@ export class FileSystemAccess {
|
||||
}
|
||||
}
|
||||
|
||||
public write(path: string, content: NSData, onError?: (error: any) => any) {
|
||||
try {
|
||||
content.writeToFileAtomically(path, true);
|
||||
}
|
||||
catch (ex) {
|
||||
if (onError) {
|
||||
onError(new Error("Failed to write to file '" + path + "': " + ex));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private getKnownPath(folderType: number): string {
|
||||
var fileManager = NSFileManager.defaultManager();
|
||||
var paths = fileManager.URLsForDirectoryInDomains(folderType, this.userDomain);
|
||||
|
||||
13
file-system/file-system.d.ts
vendored
13
file-system/file-system.d.ts
vendored
@@ -91,6 +91,12 @@ declare module "file-system" {
|
||||
*/
|
||||
readTextSync(onError?: (error: any) => any, encoding?: string): string;
|
||||
|
||||
/**
|
||||
* Reads the binary content of the file synchronously.
|
||||
* @param onError An optional function to be called if some IO-error occurs.
|
||||
*/
|
||||
readSync(onError?: (error: any) => any): any;
|
||||
|
||||
/**
|
||||
* Writes the provided string to the file, using the specified encoding (defaults to UTF-8).
|
||||
* @param content The content to be saved to the file.
|
||||
@@ -105,6 +111,13 @@ declare module "file-system" {
|
||||
* @param encoding An optional value specifying the preferred encoding (defaults to UTF-8).
|
||||
*/
|
||||
writeTextSync(content: string, onError?: (error: any) => any, encoding?: string): void;
|
||||
|
||||
/**
|
||||
* Writes the provided binary content to the file synchronously.
|
||||
* @param content The binary content to be saved to the file.
|
||||
* @param onError An optional function to be called if some IO-error occurs.
|
||||
*/
|
||||
writeSync(content: any, onError?: (error: any) => any): void;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -193,6 +193,47 @@ export class File extends FileSystemEntity {
|
||||
return !!this[fileLockedProperty];
|
||||
}
|
||||
|
||||
public readSync(onError?: (error: any) => any): any {
|
||||
this.checkAccess();
|
||||
|
||||
this[fileLockedProperty] = true;
|
||||
|
||||
var that = this;
|
||||
var localError = (error) => {
|
||||
that[fileLockedProperty] = false;
|
||||
if (onError) {
|
||||
onError(error);
|
||||
}
|
||||
};
|
||||
|
||||
var content = getFileAccess().read(this.path, localError);
|
||||
|
||||
this[fileLockedProperty] = false;
|
||||
|
||||
return content;
|
||||
|
||||
}
|
||||
|
||||
public writeSync(content: any, onError?: (error: any) => any): void {
|
||||
this.checkAccess();
|
||||
|
||||
try {
|
||||
this[fileLockedProperty] = true;
|
||||
|
||||
var that = this;
|
||||
var localError = function (error) {
|
||||
that[fileLockedProperty] = false;
|
||||
if (onError) {
|
||||
onError(error);
|
||||
}
|
||||
};
|
||||
|
||||
getFileAccess().write(this.path, content, localError);
|
||||
} finally {
|
||||
this[fileLockedProperty] = false;
|
||||
}
|
||||
}
|
||||
|
||||
public readText(encoding?: string): Promise<string> {
|
||||
return new Promise((resolve, reject) => {
|
||||
var hasError = false;
|
||||
|
||||
Reference in New Issue
Block a user