Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge branch 'master' of github.com:phonegap/phonegap-blackberry-widget

  • Loading branch information...
commit 5dc2f1aef89ef42f5f3a3ce5e58c92d0fb51a824 2 parents 00a6cdf + 82bc5cc
@jtyberg jtyberg authored
Showing with 44 additions and 7 deletions.
  1. +44 −7 framework/ext/src/com/phonegap/file/FileManager.java
View
51 framework/ext/src/com/phonegap/file/FileManager.java
@@ -19,6 +19,7 @@
import net.rim.device.api.io.Base64OutputStream;
import net.rim.device.api.io.FileNotFoundException;
import net.rim.device.api.io.IOUtilities;
+import net.rim.device.api.system.Application;
import org.json.me.JSONArray;
import org.json.me.JSONException;
@@ -203,22 +204,17 @@ protected String readAsDataURL(String filePath) throws FileNotFoundException, IO
/**
* Reads file as byte array.
* @param filePath Full path of the file to be read
+ * @return file content as a byte array
*/
protected byte[] readFile(String filePath) throws FileNotFoundException, IOException {
byte[] blob = null;
- FileConnection fconn = null;
DataInputStream dis = null;
try {
- fconn = (FileConnection)Connector.open(filePath, Connector.READ);
- if (!fconn.exists()) {
- throw new FileNotFoundException(filePath + " not found");
- }
- dis = fconn.openDataInputStream();
+ dis = openDataInputStream(filePath);
blob = IOUtilities.streamToBytes(dis);
} finally {
try {
if (dis != null) dis.close();
- if (fconn != null) fconn.close();
} catch (IOException e) {
Logger.log(this.getClass().getName() + ": " + e);
}
@@ -284,6 +280,47 @@ protected long truncateFile(String filePath, long size) throws FileNotFoundExcep
}
/**
+ * Utility function to open a DataInputStream from a file path.
+ *
+ * A file can be referenced with the following protocols:
+ * - System.getProperty("fileconn.dir.*")
+ * - local:/// references files bundled with the application
+ *
+ * @param filePath The full path to the file to open
+ * @return Handle to the DataInputStream
+ */
+ protected DataInputStream openDataInputStream(final String filePath) throws FileNotFoundException, IOException {
+ FileConnection fconn = null;
+ DataInputStream dis = null;
+
+ try {
+ if (filePath.startsWith("local:///")) {
+ // Remove local:// from filePath but leave a leading /
+ dis = new DataInputStream(Application.class.getResourceAsStream(filePath.substring(8)));
+ }
+ else {
+ fconn = (FileConnection)Connector.open(filePath, Connector.READ);
+ if (!fconn.exists()) {
+ throw new FileNotFoundException(filePath + " not found");
+ }
+ dis = fconn.openDataInputStream();
+ }
+
+ if (dis == null) {
+ throw new FileNotFoundException(filePath + " not found");
+ }
+ } finally {
+ try {
+ if (fconn != null) fconn.close();
+ } catch (IOException e) {
+ Logger.log(this.getClass().getName() + ": " + e);
+ }
+ }
+
+ return dis;
+ }
+
+ /**
* Returns action to perform.
* @param action
* @return action to perform
Please sign in to comment.
Something went wrong with that request. Please try again.