FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Merge pull request #1433 from NativeScript/file-binary-read-write · NativeScript/NativeScript@2c0501d · GitHub

Commit 2c0501d

Browse files
Vladimir Enchev
committed
Merge pull request #1433 from NativeScript/file-binary-read-write
binary sync read/write added
2 parents a2fd4d1 + acddd42 commit 2c0501d

6 files changed

Lines changed: 184 additions & 33 deletions

File tree

‎apps/tests/file-system-tests.ts‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import fs = require("file-system");
1010

1111
import TKUnit = require("./TKUnit");
1212
import appModule = require("application");
13+
import platform = require("platform");
1314

1415
// <snippet module="file-system" title="file-system">
1516
// ## Path
@@ -224,6 +225,34 @@ export var testFileRead = function () {
224225
// </snippet>
225226
};
226227

228+
export var testFileReadWriteBinary = function () {
229+
// <snippet module="file-system" title="file-system">
230+
// ### Reading/writing binary data from/to a File
231+
// ``` JavaScript
232+
var fileName = "logo.png";
233+
var error;
234+
235+
var sourceFile = fs.knownFolders.currentApp().getFile(fileName);
236+
var destinationFile = fs.knownFolders.documents().getFile(fileName);
237+
238+
var source = sourceFile.readSync(e=> { error = e; });
239+
240+
destinationFile.writeSync(source, e=> { error = e; });
241+
242+
// <hide>
243+
var destination = destinationFile.readSync(e=> { error = e; });
244+
TKUnit.assertNull(error);
245+
if (platform.device.os === platform.platformNames.ios) {
246+
TKUnit.assertTrue(source.isEqualToData(destination));
247+
} else {
248+
TKUnit.assertEqual(source, destination);
249+
}
250+
destinationFile.removeSync();
251+
// </hide>
252+
// ```
253+
// </snippet>
254+
};
255+
227256
export var testGetKnownFolders = function () {
228257
// <snippet module="file-system" title="file-system">
229258
// ### Getting the Known Folders

‎file-system/file-system-access.android.ts‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,35 @@ export class FileSystemAccess {
197197
return dir.getAbsolutePath();
198198
}
199199

200+
public read(path: string, onError?: (error: any) => any) {
201+
try {
202+
var javaFile = new java.io.File(path);
203+
var stream = new java.io.FileInputStream(javaFile);
204+
var bytes = new byte[javaFile.length()];
205+
var dataInputStream = new java.io.DataInputStream(stream);
206+
dataInputStream.readFully(bytes);
207+
return bytes;
208+
} catch (exception) {
209+
if (onError) {
210+
onError(exception);
211+
}
212+
}
213+
}
214+
215+
public write(path: string, content: java.io.ByteArrayOutputStream, onError?: (error: any) => any) {
216+
try {
217+
var javaFile = new java.io.File(path);
218+
var stream = new java.io.FileOutputStream(javaFile);
219+
var bytes = content.toByteArray();
220+
stream.write(bytes, 0, bytes.length);
221+
stream.close();
222+
} catch (exception) {
223+
if (onError) {
224+
onError(exception);
225+
}
226+
}
227+
}
228+
200229
public readText(path: string, onError?: (error: any) => any, encoding?: any) {
201230
try {
202231
var types: typeof typesModule = require("utils/types");

‎file-system/file-system-access.d.ts‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,14 @@
113113
*/
114114
readText(path: string, onError?: (error: any) => any, encoding?: any): string;
115115

116+
/**
117+
* Reads a binary content from a file with a given path.
118+
* @param path The path to the source file.
119+
* @param onSuccess A callback function which is called when a text is red.
120+
* @param onError (optional) A callback function to use if any error occurs.
121+
*/
122+
read(path: string, onError?: (error: any) => any): any;
123+
116124
/**
117125
* Writes a text to a file with a given path.
118126
* @param path The path to the source file.
@@ -123,6 +131,15 @@
123131
*/
124132
writeText(path: string, content: string, onError?: (error: any) => any, encoding?: any);
125133

134+
/**
135+
* Writes a binary to a file with a given path.
136+
* @param path The path to the source file.
137+
* @param content The content which will be written to the file.
138+
* @param onSuccess (optional) A callback function which is called when a text is written.
139+
* @param onError (optional) A callback function to use if any error occurs.
140+
*/
141+
write(path: string, content: any, onError?: (error: any) => any);
142+
126143
/**
127144
* Gets extension of the file with a given path.
128145
* @param path A path to the file.

‎file-system/file-system-access.ios.ts‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,17 @@ export class FileSystemAccess {
243243
}
244244
}
245245

246+
public read(path: string, onError?: (error: any) => any) : NSData {
247+
try {
248+
return NSData.dataWithContentsOfFile(path);
249+
}
250+
catch (ex) {
251+
if (onError) {
252+
onError(new Error("Failed to read file at path '" + path + "': " + ex));
253+
}
254+
}
255+
}
256+
246257
public writeText(path: string, content: string, onError?: (error: any) => any, encoding?: any) {
247258
var nsString = NSString.alloc().initWithString(content);
248259

@@ -262,6 +273,17 @@ export class FileSystemAccess {
262273
}
263274
}
264275

276+
public write(path: string, content: NSData, onError?: (error: any) => any) {
277+
try {
278+
content.writeToFileAtomically(path, true);
279+
}
280+
catch (ex) {
281+
if (onError) {
282+
onError(new Error("Failed to write to file '" + path + "': " + ex));
283+
}
284+
}
285+
}
286+
265287
private getKnownPath(folderType: number): string {
266288
var fileManager = NSFileManager.defaultManager();
267289
var paths = fileManager.URLsForDirectoryInDomains(folderType, this.userDomain);

‎file-system/file-system.d.ts‎

Lines changed: 46 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
*/
44
declare module "file-system" {
55

6-
/**
7-
* Represents a single entity on the file system.
8-
*/
6+
/**
7+
* Represents a single entity on the file system.
8+
*/
99
export class FileSystemEntity {
1010
/**
1111
* Gets the Date object specifying the last time this entity was modified.
@@ -52,9 +52,9 @@ declare module "file-system" {
5252
renameSync(newName: string, onError?: (error: any) => any): void;
5353
}
5454

55-
/**
56-
* Represents a File entity on the file system.
57-
*/
55+
/**
56+
* Represents a File entity on the file system.
57+
*/
5858
export class File extends FileSystemEntity {
5959
/**
6060
* Checks whether a File with the specified path already exists.
@@ -91,6 +91,12 @@ declare module "file-system" {
9191
*/
9292
readTextSync(onError?: (error: any) => any, encoding?: string): string;
9393

94+
/**
95+
* Reads the binary content of the file synchronously.
96+
* @param onError An optional function to be called if some IO-error occurs.
97+
*/
98+
readSync(onError?: (error: any) => any): any;
99+
94100
/**
95101
* Writes the provided string to the file, using the specified encoding (defaults to UTF-8).
96102
* @param content The content to be saved to the file.
@@ -105,6 +111,13 @@ declare module "file-system" {
105111
* @param encoding An optional value specifying the preferred encoding (defaults to UTF-8).
106112
*/
107113
writeTextSync(content: string, onError?: (error: any) => any, encoding?: string): void;
114+
115+
/**
116+
* Writes the provided binary content to the file synchronously.
117+
* @param content The binary content to be saved to the file.
118+
* @param onError An optional function to be called if some IO-error occurs.
119+
*/
120+
writeSync(content: any, onError?: (error: any) => any): void;
108121
}
109122

110123
/**
@@ -176,46 +189,46 @@ declare module "file-system" {
176189
eachEntity(onEntity: (entity: FileSystemEntity) => boolean);
177190
}
178191

179-
/**
180-
* Provides access to the top-level Folders instances that are accessible from the application. Use these as entry points to access the FileSystem.
181-
*/
192+
/**
193+
* Provides access to the top-level Folders instances that are accessible from the application. Use these as entry points to access the FileSystem.
194+
*/
182195
module knownFolders {
183-
/**
184-
* Gets the Documents folder available for the current application. This Folder is private for the application and not accessible from Users/External apps.
185-
*/
196+
/**
197+
* Gets the Documents folder available for the current application. This Folder is private for the application and not accessible from Users/External apps.
198+
*/
186199
export function documents(): Folder;
187200

188-
/**
189-
* Gets the Temporary (Caches) folder available for the current application. This Folder is private for the application and not accessible from Users/External apps.
190-
*/
201+
/**
202+
* Gets the Temporary (Caches) folder available for the current application. This Folder is private for the application and not accessible from Users/External apps.
203+
*/
191204
export function temp(): Folder;
192205

193-
/**
194-
* Gets the root folder for the current application. This Folder is private for the application and not accessible from Users/External apps.
195-
* iOS - this folder is read-only and contains the app and all its resources.
196-
*/
206+
/**
207+
* Gets the root folder for the current application. This Folder is private for the application and not accessible from Users/External apps.
208+
* iOS - this folder is read-only and contains the app and all its resources.
209+
*/
197210
export function currentApp(): Folder;
198211
}
199212

200-
/**
201-
* Enables path-specific operations like join, extension, etc.
202-
*/
213+
/**
214+
* Enables path-specific operations like join, extension, etc.
215+
*/
203216
export module path {
204-
/**
205-
* Normalizes a path, taking care of occurrances like ".." and "//".
206-
* @param path The path to be normalized.
207-
*/
217+
/**
218+
* Normalizes a path, taking care of occurrances like ".." and "//".
219+
* @param path The path to be normalized.
220+
*/
208221
export function normalize(path: string): string;
209222

210-
/**
211-
* Joins all the provided string components, forming a valid and normalized path.
212-
* @param paths An array of string components to be joined.
213-
*/
223+
/**
224+
* Joins all the provided string components, forming a valid and normalized path.
225+
* @param paths An array of string components to be joined.
226+
*/
214227
export function join(...paths: string[]): string;
215228

216-
/**
217-
* Gets the string used to separate file paths.
218-
*/
229+
/**
230+
* Gets the string used to separate file paths.
231+
*/
219232
export var separator: string;
220233
}
221234
}

‎file-system/file-system.ts‎

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,47 @@ export class File extends FileSystemEntity {
194194
return !!this[fileLockedProperty];
195195
}
196196

197+
public readSync(onError?: (error: any) => any): any {
198+
this.checkAccess();
199+
200+
this[fileLockedProperty] = true;
201+
202+
var that = this;
203+
var localError = (error) => {
204+
that[fileLockedProperty] = false;
205+
if (onError) {
206+
onError(error);
207+
}
208+
};
209+
210+
var content = getFileAccess().read(this.path, localError);
211+
212+
this[fileLockedProperty] = false;
213+
214+
return content;
215+
216+
}
217+
218+
public writeSync(content: any, onError?: (error: any) => any): void {
219+
this.checkAccess();
220+
221+
try {
222+
this[fileLockedProperty] = true;
223+
224+
var that = this;
225+
var localError = function (error) {
226+
that[fileLockedProperty] = false;
227+
if (onError) {
228+
onError(error);
229+
}
230+
};
231+
232+
getFileAccess().write(this.path, content, localError);
233+
} finally {
234+
this[fileLockedProperty] = false;
235+
}
236+
}
237+
197238
public readText(encoding?: string): Promise<string> {
198239
return new Promise((resolve, reject) => {
199240
var hasError = false;

0 commit comments

Comments
 (0)

Back | FazBrowse Home | New Git URL