You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cordova.apache.org by ia...@apache.org on 2013/12/13 17:18:51 UTC
[09/19] git commit: CB-5407: Move read,
write and truncate methods into modules
CB-5407: Move read, write and truncate methods into modules
Project: http://git-wip-us.apache.org/repos/asf/cordova-plugin-file/repo
Commit: http://git-wip-us.apache.org/repos/asf/cordova-plugin-file/commit/ed4a8d7d
Tree: http://git-wip-us.apache.org/repos/asf/cordova-plugin-file/tree/ed4a8d7d
Diff: http://git-wip-us.apache.org/repos/asf/cordova-plugin-file/diff/ed4a8d7d
Branch: refs/heads/dev
Commit: ed4a8d7dda175a40b6476d9ad43132a6a01874f0
Parents: 4859149
Author: Ian Clelland <ic...@chromium.org>
Authored: Thu Nov 28 21:37:00 2013 -0500
Committer: Ian Clelland <ic...@chromium.org>
Committed: Fri Dec 13 11:00:09 2013 -0500
----------------------------------------------------------------------
src/android/ContentFilesystem.java | 16 +++
src/android/FileUtils.java | 182 ++++++++++++--------------------
src/android/Filesystem.java | 9 ++
src/android/LocalFilesystem.java | 89 ++++++++++++++++
src/android/ReadFileCallback.java | 5 +
5 files changed, 188 insertions(+), 113 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/cordova-plugin-file/blob/ed4a8d7d/src/android/ContentFilesystem.java
----------------------------------------------------------------------
diff --git a/src/android/ContentFilesystem.java b/src/android/ContentFilesystem.java
index 6071870..17bead8 100644
--- a/src/android/ContentFilesystem.java
+++ b/src/android/ContentFilesystem.java
@@ -91,5 +91,21 @@ public class ContentFilesystem implements Filesystem {
// TODO Auto-generated method stub
return null;
}
+ @Override
+ public void readFileAtURL(LocalFilesystemURL inputURL, int start, int end,
+ ReadFileCallback readFileCallback) throws IOException {
+ // TODO Auto-generated method stub
+
+ }
+ @Override
+ public long writeToFileAtURL(LocalFilesystemURL inputURL, String data,
+ int offset, boolean isBinary) throws NoModificationAllowedException {
+ throw new NoModificationAllowedException("Couldn't write to file given its content URI");
+ }
+ @Override
+ public long truncateFileAtURL(LocalFilesystemURL inputURL, long size)
+ throws NoModificationAllowedException {
+ throw new NoModificationAllowedException("Couldn't truncate file given its content URI");
+ }
}
http://git-wip-us.apache.org/repos/asf/cordova-plugin-file/blob/ed4a8d7d/src/android/FileUtils.java
----------------------------------------------------------------------
diff --git a/src/android/FileUtils.java b/src/android/FileUtils.java
index 364e3d4..868e34e 100644
--- a/src/android/FileUtils.java
+++ b/src/android/FileUtils.java
@@ -159,7 +159,7 @@ public class FileUtils extends CordovaPlugin {
final int end = args.getInt(3);
final String fname=args.getString(0);
threadhelper( new FileOp( ){
- public void run() {
+ public void run() throws MalformedURLException {
readFileAs(fname, start, end, callbackContext, encoding, PluginResult.MESSAGE_TYPE_STRING);
}
}, callbackContext);
@@ -169,7 +169,7 @@ public class FileUtils extends CordovaPlugin {
final int end = args.getInt(2);
final String fname=args.getString(0);
threadhelper( new FileOp( ){
- public void run() {
+ public void run() throws MalformedURLException {
readFileAs(fname, start, end, callbackContext, null, -1);
}
}, callbackContext);
@@ -179,7 +179,7 @@ public class FileUtils extends CordovaPlugin {
final int end = args.getInt(2);
final String fname=args.getString(0);
threadhelper( new FileOp( ){
- public void run() {
+ public void run() throws MalformedURLException {
readFileAs(fname, start, end, callbackContext, null, PluginResult.MESSAGE_TYPE_ARRAYBUFFER);
}
},callbackContext);
@@ -189,7 +189,7 @@ public class FileUtils extends CordovaPlugin {
final int end = args.getInt(2);
final String fname=args.getString(0);
threadhelper( new FileOp( ){
- public void run() {
+ public void run() throws MalformedURLException {
readFileAs(fname, start, end, callbackContext, null, PluginResult.MESSAGE_TYPE_BINARYSTRING);
}
}, callbackContext);
@@ -715,69 +715,56 @@ public class FileUtils extends CordovaPlugin {
* @param encoding The encoding to return contents as. Typical value is UTF-8. (see http://www.iana.org/assignments/character-sets)
* @param resultType The desired type of data to send to the callback.
* @return Contents of file.
+ * @throws MalformedURLException
*/
- public void readFileAs(final String filename, final int start, final int end, final CallbackContext callbackContext, final String encoding, final int resultType) {
- this.cordova.getThreadPool().execute(new Runnable() {
- public void run() {
- try {
- byte[] bytes = readAsBinaryHelper(filename, start, end);
-
- PluginResult result;
- switch (resultType) {
- case PluginResult.MESSAGE_TYPE_STRING:
- result = new PluginResult(PluginResult.Status.OK, new String(bytes, encoding));
- break;
- case PluginResult.MESSAGE_TYPE_ARRAYBUFFER:
- result = new PluginResult(PluginResult.Status.OK, bytes);
- break;
- case PluginResult.MESSAGE_TYPE_BINARYSTRING:
- result = new PluginResult(PluginResult.Status.OK, bytes, true);
- break;
- default: // Base64.
- String contentType = FileHelper.getMimeType(filename, cordova);
- byte[] base64 = Base64.encode(bytes, Base64.NO_WRAP);
- String s = "data:" + contentType + ";base64," + new String(base64, "US-ASCII");
- result = new PluginResult(PluginResult.Status.OK, s);
+ public void readFileAs(final String srcURLstr, final int start, final int end, final CallbackContext callbackContext, final String encoding, final int resultType) throws MalformedURLException {
+ try {
+ LocalFilesystemURL inputURL = new LocalFilesystemURL(srcURLstr);
+ Filesystem fs = this.filesystemForURL(inputURL);
+ if (fs == null) {
+ throw new MalformedURLException("No installed handlers for this URL");
+ }
+
+ fs.readFileAtURL(inputURL, start, end, new ReadFileCallback() {
+ public void handleData(byte[] bytes, String contentType) {
+ try {
+ PluginResult result;
+ switch (resultType) {
+ case PluginResult.MESSAGE_TYPE_STRING:
+ result = new PluginResult(PluginResult.Status.OK, new String(bytes, encoding));
+ break;
+ case PluginResult.MESSAGE_TYPE_ARRAYBUFFER:
+ result = new PluginResult(PluginResult.Status.OK, bytes);
+ break;
+ case PluginResult.MESSAGE_TYPE_BINARYSTRING:
+ result = new PluginResult(PluginResult.Status.OK, bytes, true);
+ break;
+ default: // Base64.
+ byte[] base64 = Base64.encode(bytes, Base64.NO_WRAP);
+ String s = "data:" + contentType + ";base64," + new String(base64, "US-ASCII");
+ result = new PluginResult(PluginResult.Status.OK, s);
+ }
+
+ callbackContext.sendPluginResult(result);
+ } catch (IOException e) {
+ Log.d(LOG_TAG, e.getLocalizedMessage());
+ callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.IO_EXCEPTION, NOT_READABLE_ERR));
}
+ }
+ });
- callbackContext.sendPluginResult(result);
- } catch (FileNotFoundException e) {
- callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.IO_EXCEPTION, NOT_FOUND_ERR));
- } catch (IOException e) {
- Log.d(LOG_TAG, e.getLocalizedMessage());
- callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.IO_EXCEPTION, NOT_READABLE_ERR));
- }
- }
- });
- }
- /**
- * Read the contents of a file as binary.
- * This is done synchronously; the result is returned.
- *
- * @param filename The name of the file.
- * @param start Start position in the file.
- * @param end End position to stop at (exclusive).
- * @return Contents of the file as a byte[].
- * @throws IOException
- */
- private byte[] readAsBinaryHelper(String filename, int start, int end) throws IOException {
- int numBytesToRead = end - start;
- byte[] bytes = new byte[numBytesToRead];
- InputStream inputStream = FileHelper.getInputStreamFromUriString(filename, cordova);
- int numBytesRead = 0;
-
- if (start > 0) {
- inputStream.skip(start);
- }
-
- while (numBytesToRead > 0 && (numBytesRead = inputStream.read(bytes, numBytesRead, numBytesToRead)) >= 0) {
- numBytesToRead -= numBytesRead;
+ } catch (IllegalArgumentException e) {
+ throw new MalformedURLException("Unrecognized filesystem URL");
+ } catch (FileNotFoundException e) {
+ callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.IO_EXCEPTION, NOT_FOUND_ERR));
+ } catch (IOException e) {
+ Log.d(LOG_TAG, e.getLocalizedMessage());
+ callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.IO_EXCEPTION, NOT_READABLE_ERR));
}
-
- return bytes;
}
+
/**
* Write contents of file.
*
@@ -789,43 +776,19 @@ public class FileUtils extends CordovaPlugin {
* @throws NoModificationAllowedException
*/
/**/
- public long write(String filename, String data, int offset, boolean isBinary) throws FileNotFoundException, IOException, NoModificationAllowedException {
- if (filename.startsWith("content://")) {
- throw new NoModificationAllowedException("Couldn't write to file given its content URI");
- }
-
- filename = FileHelper.getRealPath(filename, cordova);
-
- boolean append = false;
- if (offset > 0) {
- this.truncateFile(filename, offset);
- append = true;
- }
-
- byte[] rawData;
- if (isBinary) {
- rawData = Base64.decode(data, Base64.DEFAULT);
- } else {
- rawData = data.getBytes();
- }
- ByteArrayInputStream in = new ByteArrayInputStream(rawData);
- try
- {
- FileOutputStream out = new FileOutputStream(filename, append);
- byte buff[] = new byte[rawData.length];
- in.read(buff, 0, buff.length);
- out.write(buff, 0, rawData.length);
- out.flush();
- out.close();
- }
- catch (NullPointerException e)
- {
- // This is a bug in the Android implementation of the Java Stack
- NoModificationAllowedException realException = new NoModificationAllowedException(filename);
- throw realException;
+ public long write(String srcURLstr, String data, int offset, boolean isBinary) throws FileNotFoundException, IOException, NoModificationAllowedException {
+ try {
+ LocalFilesystemURL inputURL = new LocalFilesystemURL(srcURLstr);
+ Filesystem fs = this.filesystemForURL(inputURL);
+ if (fs == null) {
+ throw new MalformedURLException("No installed handlers for this URL");
+ }
+
+ long x = fs.writeToFileAtURL(inputURL, data, offset, isBinary); Log.d("TEST",srcURLstr + ": "+x); return x;
+ } catch (IllegalArgumentException e) {
+ throw new MalformedURLException("Unrecognized filesystem URL");
}
-
- return rawData.length;
+
}
/**
@@ -836,24 +799,17 @@ public class FileUtils extends CordovaPlugin {
* @throws FileNotFoundException, IOException
* @throws NoModificationAllowedException
*/
- private long truncateFile(String filename, long size) throws FileNotFoundException, IOException, NoModificationAllowedException {
- if (filename.startsWith("content://")) {
- throw new NoModificationAllowedException("Couldn't truncate file given its content URI");
- }
-
- filename = FileHelper.getRealPath(filename, cordova);
-
- RandomAccessFile raf = new RandomAccessFile(filename, "rw");
+ private long truncateFile(String srcURLstr, long size) throws FileNotFoundException, IOException, NoModificationAllowedException {
try {
- if (raf.length() >= size) {
- FileChannel channel = raf.getChannel();
- channel.truncate(size);
- return size;
- }
-
- return raf.length();
- } finally {
- raf.close();
+ LocalFilesystemURL inputURL = new LocalFilesystemURL(srcURLstr);
+ Filesystem fs = this.filesystemForURL(inputURL);
+ if (fs == null) {
+ throw new MalformedURLException("No installed handlers for this URL");
+ }
+
+ return fs.truncateFileAtURL(inputURL, size);
+ } catch (IllegalArgumentException e) {
+ throw new MalformedURLException("Unrecognized filesystem URL");
}
}
}
http://git-wip-us.apache.org/repos/asf/cordova-plugin-file/blob/ed4a8d7d/src/android/Filesystem.java
----------------------------------------------------------------------
diff --git a/src/android/Filesystem.java b/src/android/Filesystem.java
index cbec37a..6955520 100644
--- a/src/android/Filesystem.java
+++ b/src/android/Filesystem.java
@@ -28,4 +28,13 @@ public interface Filesystem {
JSONObject copyFileToURL(LocalFilesystemURL destURL, String newName,
Filesystem srcFs, LocalFilesystemURL srcURL, boolean move) throws IOException, InvalidModificationException, JSONException, NoModificationAllowedException, FileExistsException;
+ void readFileAtURL(LocalFilesystemURL inputURL, int start, int end,
+ ReadFileCallback readFileCallback) throws IOException;
+
+ long writeToFileAtURL(LocalFilesystemURL inputURL, String data, int offset,
+ boolean isBinary) throws NoModificationAllowedException, IOException;
+
+ long truncateFileAtURL(LocalFilesystemURL inputURL, long size)
+ throws IOException, NoModificationAllowedException;
+
}
http://git-wip-us.apache.org/repos/asf/cordova-plugin-file/blob/ed4a8d7d/src/android/LocalFilesystem.java
----------------------------------------------------------------------
diff --git a/src/android/LocalFilesystem.java b/src/android/LocalFilesystem.java
index e2f5de1..2613329 100644
--- a/src/android/LocalFilesystem.java
+++ b/src/android/LocalFilesystem.java
@@ -1,10 +1,13 @@
package org.apache.cordova.file;
+import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
+import java.io.InputStream;
+import java.io.RandomAccessFile;
import java.nio.channels.FileChannel;
import org.apache.cordova.CordovaInterface;
@@ -544,5 +547,91 @@ public class LocalFilesystem implements Filesystem {
return null;
}
+ @Override
+ public void readFileAtURL(LocalFilesystemURL inputURL, int start, int end,
+ ReadFileCallback readFileCallback) throws IOException {
+
+ int numBytesToRead = end - start;
+ byte[] bytes = new byte[numBytesToRead];
+ String contentType;
+
+ File file = new File(this.filesystemPathForURL(inputURL));
+ InputStream inputStream = new FileInputStream(file);
+
+ contentType = FileHelper.getMimeTypeForExtension(file.getAbsolutePath());
+ int numBytesRead = 0;
+
+ if (start > 0) {
+ inputStream.skip(start);
+ }
+
+ while (numBytesToRead > 0 && (numBytesRead = inputStream.read(bytes, numBytesRead, numBytesToRead)) >= 0) {
+ numBytesToRead -= numBytesRead;
+ }
+ inputStream.close();
+ readFileCallback.handleData(bytes, contentType);
+ }
+
+ @Override
+ public long writeToFileAtURL(LocalFilesystemURL inputURL, String data,
+ int offset, boolean isBinary) throws IOException, NoModificationAllowedException {
+ File file = new File(filesystemPathForURL(inputURL));
+
+ boolean append = false;
+ if (offset > 0) {
+ this.truncateFileAtURL(inputURL, offset);
+ append = true;
+ }
+
+ byte[] rawData;
+ if (isBinary) {
+ rawData = Base64.decode(data, Base64.DEFAULT);
+ } else {
+ rawData = data.getBytes();
+ }
+ ByteArrayInputStream in = new ByteArrayInputStream(rawData);
+ try
+ {
+ FileOutputStream out = new FileOutputStream(this.filesystemPathForURL(inputURL), append);
+ byte buff[] = new byte[rawData.length];
+ in.read(buff, 0, buff.length);
+ out.write(buff, 0, rawData.length);
+ out.flush();
+ out.close();
+ }
+ catch (NullPointerException e)
+ {
+ // This is a bug in the Android implementation of the Java Stack
+ NoModificationAllowedException realException = new NoModificationAllowedException(inputURL.toString());
+ throw realException;
+ }
+
+ return rawData.length;
+ }
+
+ @Override
+ public long truncateFileAtURL(LocalFilesystemURL inputURL, long size) throws IOException {
+ File file = new File(filesystemPathForURL(inputURL));
+
+ if (!file.exists()) {
+ throw new FileNotFoundException("File at " + inputURL.URL + " does not exist.");
+ }
+
+ RandomAccessFile raf = new RandomAccessFile(filesystemPathForURL(inputURL), "rw");
+ try {
+ if (raf.length() >= size) {
+ FileChannel channel = raf.getChannel();
+ channel.truncate(size);
+ return size;
+ }
+
+ return raf.length();
+ } finally {
+ raf.close();
+ }
+
+
+ }
+
}
http://git-wip-us.apache.org/repos/asf/cordova-plugin-file/blob/ed4a8d7d/src/android/ReadFileCallback.java
----------------------------------------------------------------------
diff --git a/src/android/ReadFileCallback.java b/src/android/ReadFileCallback.java
new file mode 100644
index 0000000..77bb7da
--- /dev/null
+++ b/src/android/ReadFileCallback.java
@@ -0,0 +1,5 @@
+package org.apache.cordova.file;
+
+public interface ReadFileCallback {
+ public void handleData(byte[] data, String contentType);
+}