| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -10,6 +10,7 @@ import fs = require("file-system"); | |||
| 10 | 10 | ||
| 11 | 11 | import TKUnit = require("./TKUnit"); | |
| 12 | 12 | import appModule = require("application"); | |
| 13 | + import platform = require("platform"); | ||
| 13 | 14 | ||
| 14 | 15 | // <snippet module="file-system" title="file-system"> | |
| 15 | 16 | // ## Path | |
@@ -224,6 +225,34 @@ export var testFileRead = function () { | |||
| 224 | 225 | // </snippet> | |
| 225 | 226 | }; | |
| 226 | 227 | ||
| 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 | + | ||
| 227 | 256 | export var testGetKnownFolders = function () { | |
| 228 | 257 | // <snippet module="file-system" title="file-system"> | |
| 229 | 258 | // ### Getting the Known Folders | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -197,6 +197,35 @@ export class FileSystemAccess { | |||
| 197 | 197 | return dir.getAbsolutePath(); | |
| 198 | 198 | } | |
| 199 | 199 | ||
| 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 | + | ||
| 200 | 229 | public readText(path: string, onError?: (error: any) => any, encoding?: any) { | |
| 201 | 230 | try { | |
| 202 | 231 | var types: typeof typesModule = require("utils/types"); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -113,6 +113,14 @@ | |||
| 113 | 113 | */ | |
| 114 | 114 | readText(path: string, onError?: (error: any) => any, encoding?: any): string; | |
| 115 | 115 | ||
| 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 | + | ||
| 116 | 124 | /** | |
| 117 | 125 | * Writes a text to a file with a given path. | |
| 118 | 126 | * @param path The path to the source file. | |
@@ -123,6 +131,15 @@ | |||
| 123 | 131 | */ | |
| 124 | 132 | writeText(path: string, content: string, onError?: (error: any) => any, encoding?: any); | |
| 125 | 133 | ||
| 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 | + | ||
| 126 | 143 | /** | |
| 127 | 144 | * Gets extension of the file with a given path. | |
| 128 | 145 | * @param path A path to the file. | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -243,6 +243,17 @@ export class FileSystemAccess { | |||
| 243 | 243 | } | |
| 244 | 244 | } | |
| 245 | 245 | ||
| 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 | + | ||
| 246 | 257 | public writeText(path: string, content: string, onError?: (error: any) => any, encoding?: any) { | |
| 247 | 258 | var nsString = NSString.alloc().initWithString(content); | |
| 248 | 259 | ||
@@ -262,6 +273,17 @@ export class FileSystemAccess { | |||
| 262 | 273 | } | |
| 263 | 274 | } | |
| 264 | 275 | ||
| 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 | + | ||
| 265 | 287 | private getKnownPath(folderType: number): string { | |
| 266 | 288 | var fileManager = NSFileManager.defaultManager(); | |
| 267 | 289 | var paths = fileManager.URLsForDirectoryInDomains(folderType, this.userDomain); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -3,9 +3,9 @@ | |||
| 3 | 3 | */ | |
| 4 | 4 | declare module "file-system" { | |
| 5 | 5 | ||
| 6 | - /** | ||
| 7 | - * Represents a single entity on the file system. | ||
| 8 | - */ | ||
| 6 | + /** | ||
| 7 | + * Represents a single entity on the file system. | ||
| 8 | + */ | ||
| 9 | 9 | export class FileSystemEntity { | |
| 10 | 10 | /** | |
| 11 | 11 | * Gets the Date object specifying the last time this entity was modified. | |
@@ -52,9 +52,9 @@ declare module "file-system" { | |||
| 52 | 52 | renameSync(newName: string, onError?: (error: any) => any): void; | |
| 53 | 53 | } | |
| 54 | 54 | ||
| 55 | - /** | ||
| 56 | - * Represents a File entity on the file system. | ||
| 57 | - */ | ||
| 55 | + /** | ||
| 56 | + * Represents a File entity on the file system. | ||
| 57 | + */ | ||
| 58 | 58 | export class File extends FileSystemEntity { | |
| 59 | 59 | /** | |
| 60 | 60 | * Checks whether a File with the specified path already exists. | |
@@ -91,6 +91,12 @@ declare module "file-system" { | |||
| 91 | 91 | */ | |
| 92 | 92 | readTextSync(onError?: (error: any) => any, encoding?: string): string; | |
| 93 | 93 | ||
| 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 | + | ||
| 94 | 100 | /** | |
| 95 | 101 | * Writes the provided string to the file, using the specified encoding (defaults to UTF-8). | |
| 96 | 102 | * @param content The content to be saved to the file. | |
@@ -105,6 +111,13 @@ declare module "file-system" { | |||
| 105 | 111 | * @param encoding An optional value specifying the preferred encoding (defaults to UTF-8). | |
| 106 | 112 | */ | |
| 107 | 113 | 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; | ||
| 108 | 121 | } | |
| 109 | 122 | ||
| 110 | 123 | /** | |
@@ -176,46 +189,46 @@ declare module "file-system" { | |||
| 176 | 189 | eachEntity(onEntity: (entity: FileSystemEntity) => boolean); | |
| 177 | 190 | } | |
| 178 | 191 | ||
| 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 | + */ | ||
| 182 | 195 | 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 | + */ | ||
| 186 | 199 | export function documents(): Folder; | |
| 187 | 200 | ||
| 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 | + */ | ||
| 191 | 204 | export function temp(): Folder; | |
| 192 | 205 | ||
| 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 | + */ | ||
| 197 | 210 | export function currentApp(): Folder; | |
| 198 | 211 | } | |
| 199 | 212 | ||
| 200 | - /** | ||
| 201 | - * Enables path-specific operations like join, extension, etc. | ||
| 202 | - */ | ||
| 213 | + /** | ||
| 214 | + * Enables path-specific operations like join, extension, etc. | ||
| 215 | + */ | ||
| 203 | 216 | 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 | + */ | ||
| 208 | 221 | export function normalize(path: string): string; | |
| 209 | 222 | ||
| 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 | + */ | ||
| 214 | 227 | export function join(...paths: string[]): string; | |
| 215 | 228 | ||
| 216 | - /** | ||
| 217 | - * Gets the string used to separate file paths. | ||
| 218 | - */ | ||
| 229 | + /** | ||
| 230 | + * Gets the string used to separate file paths. | ||
| 231 | + */ | ||
| 219 | 232 | export var separator: string; | |
| 220 | 233 | } | |
| 221 | 234 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -194,6 +194,47 @@ export class File extends FileSystemEntity { | |||
| 194 | 194 | return !!this[fileLockedProperty]; | |
| 195 | 195 | } | |
| 196 | 196 | ||
| 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 | + | ||
| 197 | 238 | public readText(encoding?: string): Promise<string> { | |
| 198 | 239 | return new Promise((resolve, reject) => { | |
| 199 | 240 | var hasError = false; | |
| Back | FazBrowse Home | New Git URL |
0 commit comments