mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-11-05 13:26:48 +08:00
Merge pull request #1433 from NativeScript/file-binary-read-write
binary sync read/write added
This commit is contained in:
@@ -10,6 +10,7 @@ import fs = require("file-system");
|
|||||||
|
|
||||||
import TKUnit = require("./TKUnit");
|
import TKUnit = require("./TKUnit");
|
||||||
import appModule = require("application");
|
import appModule = require("application");
|
||||||
|
import platform = require("platform");
|
||||||
|
|
||||||
// <snippet module="file-system" title="file-system">
|
// <snippet module="file-system" title="file-system">
|
||||||
// ## Path
|
// ## Path
|
||||||
@@ -224,6 +225,34 @@ export var testFileRead = function () {
|
|||||||
// </snippet>
|
// </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 error;
|
||||||
|
|
||||||
|
var sourceFile = fs.knownFolders.currentApp().getFile(fileName);
|
||||||
|
var destinationFile = fs.knownFolders.documents().getFile(fileName);
|
||||||
|
|
||||||
|
var source = sourceFile.readSync(e=> { error = e; });
|
||||||
|
|
||||||
|
destinationFile.writeSync(source, e=> { error = e; });
|
||||||
|
|
||||||
|
// <hide>
|
||||||
|
var destination = destinationFile.readSync(e=> { error = e; });
|
||||||
|
TKUnit.assertNull(error);
|
||||||
|
if (platform.device.os === platform.platformNames.ios) {
|
||||||
|
TKUnit.assertTrue(source.isEqualToData(destination));
|
||||||
|
} else {
|
||||||
|
TKUnit.assertEqual(source, destination);
|
||||||
|
}
|
||||||
|
destinationFile.removeSync();
|
||||||
|
// </hide>
|
||||||
|
// ```
|
||||||
|
// </snippet>
|
||||||
|
};
|
||||||
|
|
||||||
export var testGetKnownFolders = function () {
|
export var testGetKnownFolders = function () {
|
||||||
// <snippet module="file-system" title="file-system">
|
// <snippet module="file-system" title="file-system">
|
||||||
// ### Getting the Known Folders
|
// ### Getting the Known Folders
|
||||||
|
|||||||
@@ -197,6 +197,35 @@ export class FileSystemAccess {
|
|||||||
return dir.getAbsolutePath();
|
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) {
|
public readText(path: string, onError?: (error: any) => any, encoding?: any) {
|
||||||
try {
|
try {
|
||||||
var types: typeof typesModule = require("utils/types");
|
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
@@ -113,6 +113,14 @@
|
|||||||
*/
|
*/
|
||||||
readText(path: string, onError?: (error: any) => any, encoding?: any): string;
|
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.
|
* Writes a text to a file with a given path.
|
||||||
* @param path The path to the source file.
|
* @param path The path to the source file.
|
||||||
@@ -123,6 +131,15 @@
|
|||||||
*/
|
*/
|
||||||
writeText(path: string, content: string, onError?: (error: any) => any, encoding?: any);
|
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.
|
* Gets extension of the file with a given path.
|
||||||
* @param path A path to the file.
|
* @param path A path to the file.
|
||||||
|
|||||||
@@ -243,6 +243,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) {
|
public writeText(path: string, content: string, onError?: (error: any) => any, encoding?: any) {
|
||||||
var nsString = NSString.alloc().initWithString(content);
|
var nsString = NSString.alloc().initWithString(content);
|
||||||
|
|
||||||
@@ -262,6 +273,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 {
|
private getKnownPath(folderType: number): string {
|
||||||
var fileManager = NSFileManager.defaultManager();
|
var fileManager = NSFileManager.defaultManager();
|
||||||
var paths = fileManager.URLsForDirectoryInDomains(folderType, this.userDomain);
|
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;
|
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).
|
* 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.
|
* @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).
|
* @param encoding An optional value specifying the preferred encoding (defaults to UTF-8).
|
||||||
*/
|
*/
|
||||||
writeTextSync(content: string, onError?: (error: any) => any, encoding?: string): void;
|
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -194,6 +194,47 @@ export class File extends FileSystemEntity {
|
|||||||
return !!this[fileLockedProperty];
|
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> {
|
public readText(encoding?: string): Promise<string> {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
var hasError = false;
|
var hasError = false;
|
||||||
|
|||||||
Reference in New Issue
Block a user