Skip to content

Commit

Permalink
Fixes #39. Fix errors in FileReader/FileWriter found by MobileSpec.
Browse files Browse the repository at this point in the history
Address several failures reported by MobileSpec test suite for the
FileReader and FileWriter API.

 - Change the encoding of writes with the FileWriter API to use
   UTF-8 instead of the BlackBerry default of ISO-8859-1

 - Truncate the file to the seek position before writing new
   data in order to match behavior on other platforms.  Return the
   length of the file as the seek position plus data written.

 - Provide stubs for FileReader.readAsBinaryString and
   FileReader.readAsArrayBuffer.
  • Loading branch information
Drew Walters committed Jul 21, 2011
1 parent 40ef74c commit 07d785b
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 3 deletions.
5 changes: 4 additions & 1 deletion framework/ext/src/com/phonegap/file/FileManager.java
Expand Up @@ -383,7 +383,10 @@ protected static PluginResult writeFile(String filePath, String data, int positi
int bytesWritten = 0;
try {
// write file data
bytesWritten = FileUtils.writeFile(filePath, data.getBytes(), position);
// The default String encoding on BB is ISO-8859-1 which causes
// issues with extended characters. Force to UTF-8 to provide
// greater character support and match other platforms.
bytesWritten = FileUtils.writeFile(filePath, data.getBytes("UTF-8"), position);
result = new PluginResult(PluginResult.Status.OK, bytesWritten);
}
catch (SecurityException e) {
Expand Down
5 changes: 5 additions & 0 deletions framework/ext/src/com/phonegap/file/FileUtils.java
Expand Up @@ -125,6 +125,11 @@ public static int writeFile(String filePath, byte[] data, int position)
Connector.READ_WRITE);
if (!fconn.exists()) {
fconn.create();
} else {
// Originally, this did an overwrite in place and did not
// truncate. The truncate was added to match behavior on
// other platforms.
fconn.truncate(position);
}
os = fconn.openOutputStream(position);
os.write(data);
Expand Down
33 changes: 31 additions & 2 deletions javascript/file.js
Expand Up @@ -341,6 +341,34 @@ var FileReader = FileReader || (function() {
);
};

/**
* Read file and return data as a binary data.
*
* @param file {File} File object containing file properties
*/
FileReader.prototype.readAsBinaryString = function(file) {
// TODO - Can't return binary data to browser.
if (typeof file.fullPath === "undefined") {
this.fileName = file;
} else {
this.fileName = file.fullPath;
}
};

/**
* Read file and return data as a binary data.
*
* @param file {File} File object containing file properties
*/
FileReader.prototype.readAsArrayBuffer = function(file) {
// TODO - Can't return binary data to browser.
if (typeof file.fullPath === "undefined") {
this.fileName = file;
} else {
this.fileName = file.fullPath;
}
};

return FileReader;
}());

Expand Down Expand Up @@ -551,11 +579,12 @@ var FileWriter = FileWriter || (function() {
return;
}

// new length is maximum of old length, or position plus bytes written
me.length = Math.max(me.length, me.position + result);
// position always increases by bytes written because file would be extended
me.position += result;

// new length is now where writing finished
me.length = me.position;

// success procedure
if (typeof me.onwrite == "function") {
event = {"type":"write", "target":me};
Expand Down

0 comments on commit 07d785b

Please sign in to comment.