You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cordova.apache.org by br...@apache.org on 2013/03/04 23:36:05 UTC

[1/2] ios commit: Implement readAsArrayBuffer

Implement readAsArrayBuffer


Project: http://git-wip-us.apache.org/repos/asf/cordova-ios/repo
Commit: http://git-wip-us.apache.org/repos/asf/cordova-ios/commit/76a8be50
Tree: http://git-wip-us.apache.org/repos/asf/cordova-ios/tree/76a8be50
Diff: http://git-wip-us.apache.org/repos/asf/cordova-ios/diff/76a8be50

Branch: refs/heads/master
Commit: 76a8be50885da05c3f59ddfca630eb1a1fd8778d
Parents: c5bbcb8
Author: Braden Shepherdson <br...@gmail.com>
Authored: Mon Mar 4 16:08:12 2013 -0500
Committer: Braden Shepherdson <br...@gmail.com>
Committed: Mon Mar 4 17:35:18 2013 -0500

----------------------------------------------------------------------
 CordovaLib/Classes/CDVFile.h |    1 +
 CordovaLib/Classes/CDVFile.m |   84 +++++++++++++++++++++++++++++++++++++
 2 files changed, 85 insertions(+), 0 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cordova-ios/blob/76a8be50/CordovaLib/Classes/CDVFile.h
----------------------------------------------------------------------
diff --git a/CordovaLib/Classes/CDVFile.h b/CordovaLib/Classes/CDVFile.h
index 4862921..e3fdc83 100644
--- a/CordovaLib/Classes/CDVFile.h
+++ b/CordovaLib/Classes/CDVFile.h
@@ -76,6 +76,7 @@ extern NSString* const kCDVAssetsLibraryPrefix;
 
 - (void)readAsText:(CDVInvokedUrlCommand*)command;
 - (void)readAsDataURL:(CDVInvokedUrlCommand*)command;
+- (void)readAsArrayBuffer:(CDVInvokedUrlCommand*)command;
 - (NSString*)getMimeTypeFromPath:(NSString*)fullPath;
 - (void)write:(CDVInvokedUrlCommand*)command;
 - (void)testFileExists:(CDVInvokedUrlCommand*)command;

http://git-wip-us.apache.org/repos/asf/cordova-ios/blob/76a8be50/CordovaLib/Classes/CDVFile.m
----------------------------------------------------------------------
diff --git a/CordovaLib/Classes/CDVFile.m b/CordovaLib/Classes/CDVFile.m
index d52405d..e491ef8 100644
--- a/CordovaLib/Classes/CDVFile.m
+++ b/CordovaLib/Classes/CDVFile.m
@@ -1116,6 +1116,8 @@ NSString* const kCDVAssetsLibraryPrefix = @"assets-library://";
  * IN:
  * NSArray* arguments
  *	0 - NSString* fullPath
+ *	1 - NSString* start - OPTIONAL, only provided when not == 0.
+ *	2 - NSString* end - OPTIONAL, only provided when not == length.
  *
  * Determines the mime type from the file extension, returns ENCODING_ERR if mimetype can not be determined.
  */
@@ -1203,6 +1205,88 @@ NSString* const kCDVAssetsLibraryPrefix = @"assets-library://";
     [self.commandDelegate sendPluginResult:result callbackId:command.callbackId];
 }
 
+/* Read content of text file and return as an arraybuffer
+ * IN:
+ * NSArray* arguments
+ *	0 - NSString* fullPath
+ *	1 - NSString* start - OPTIONAL, only provided when not == 0.
+ *	2 - NSString* end - OPTIONAL, only provided when not == length.
+ */
+
+- (void)readAsArrayBuffer:(CDVInvokedUrlCommand*)command
+{
+    // arguments
+    NSString* argPath = [command.arguments objectAtIndex:0];
+    NSInteger start = 0;
+    NSInteger end = -1;
+
+    if ([command.arguments count] >= 2) {
+        start = [[command.arguments objectAtIndex:1] integerValue];
+    }
+    if ([command.arguments count] >= 3) {
+        end = [[command.arguments objectAtIndex:2] integerValue];
+    }
+
+    CDVFileError errCode = ABORT_ERR;
+    __block CDVPluginResult* result = nil;
+
+    if (!argPath) {
+        errCode = SYNTAX_ERR;
+    } else if ([argPath hasPrefix:kCDVAssetsLibraryPrefix]) {
+        // In this case, we need to use an asynchronous method to retrieve the file.
+        // Because of this, we can't just assign to `result` and send it at the end of the method.
+        // Instead, we return after calling the asynchronous method and send `result` in each of the blocks.
+        ALAssetsLibraryAssetForURLResultBlock resultBlock = ^(ALAsset * asset) {
+            if (asset) {
+                // We have the asset!  Get the data and send it off.
+                ALAssetRepresentation* assetRepresentation = [asset defaultRepresentation];
+                Byte* buffer = (Byte*)malloc ([assetRepresentation size]);
+                NSUInteger bufferSize = [assetRepresentation getBytes:buffer fromOffset:0.0 length:[assetRepresentation size] error:nil];
+                NSData* data = [NSData dataWithBytesNoCopy:buffer length:bufferSize freeWhenDone:YES];
+                result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsArrayBuffer:data];
+                [self.commandDelegate sendPluginResult:result callbackId:command.callbackId];
+            } else {
+                // We couldn't find the asset.  Send the appropriate error.
+                result = [CDVPluginResult resultWithStatus:CDVCommandStatus_IO_EXCEPTION messageAsInt:NOT_FOUND_ERR];
+                [self.commandDelegate sendPluginResult:result callbackId:command.callbackId];
+            }
+        };
+        ALAssetsLibraryAccessFailureBlock failureBlock = ^(NSError * error) {
+            // Retrieving the asset failed for some reason.  Send the appropriate error.
+            result = [CDVPluginResult resultWithStatus:CDVCommandStatus_IO_EXCEPTION messageAsString:[error localizedDescription]];
+            [self.commandDelegate sendPluginResult:result callbackId:command.callbackId];
+        };
+
+        ALAssetsLibrary* assetsLibrary = [[ALAssetsLibrary alloc] init];
+        [assetsLibrary assetForURL:[NSURL URLWithString:argPath] resultBlock:resultBlock failureBlock:failureBlock];
+        return;
+    } else {
+        NSFileHandle* file = [NSFileHandle fileHandleForReadingAtPath:argPath];
+        if (start > 0) {
+            [file seekToFileOffset:start];
+        }
+
+        NSData* readData;
+        if (end < 0) {
+            readData = [file readDataToEndOfFile];
+        } else {
+            readData = [file readDataOfLength:(end - start)];
+        }
+
+        [file closeFile];
+        if (readData) {
+            result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsArrayBuffer:readData];
+        } else {
+            errCode = NOT_FOUND_ERR;
+        }
+    }
+    if (!result) {
+        result = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsInt:errCode];
+    }
+
+    [self.commandDelegate sendPluginResult:result callbackId:command.callbackId];
+}
+
 /* helper function to get the mimeType from the file extension
  * IN:
  *	NSString* fullPath - filename (may include path)