Skip to content

Commit

Permalink
Add file utility methods.
Browse files Browse the repository at this point in the history
  • Loading branch information
Justin Tyberg committed Jan 28, 2011
1 parent 02927e2 commit 8c3d5cc
Show file tree
Hide file tree
Showing 4 changed files with 330 additions and 81 deletions.
16 changes: 15 additions & 1 deletion framework/ext/src/com/phonegap/api/PluginManager.java
Expand Up @@ -7,12 +7,14 @@
*/
package com.phonegap.api;

import java.io.IOException;
import java.util.Hashtable;

import net.rim.device.api.script.Scriptable;
import net.rim.device.api.script.ScriptableFunction;

import com.phonegap.PhoneGapExtension;
import com.phonegap.file.FileUtils;
import com.phonegap.util.Logger;

/**
Expand Down Expand Up @@ -83,7 +85,19 @@ else if (name.equals(FIELD_DESTROY)) {
final PluginManagerFunction plugin_mgr = this.pluginManagerFunction;
return new ScriptableFunction() {
public Object invoke(Object obj, Object[] oargs) throws Exception {
// allow plugins to clean up
plugin_mgr.onDestroy();

// delete temporary application directory
// NOTE: doing this on a background thread doesn't work
// because the app is closing and the thread is killed before it completes
try {
FileUtils.deleteApplicationTempDirectory();
}
catch (IOException e) {
Logger.log(this.getClass().getName() + ": error deleting application temp directory: " +e);
}

return null;
}
};
Expand Down Expand Up @@ -124,4 +138,4 @@ public void addService(String serviceName, String className) {
public String getClassForService(String serviceName) {
return (String)this.services.get(serviceName);
}
}
}
83 changes: 4 additions & 79 deletions framework/ext/src/com/phonegap/file/FileManager.java
Expand Up @@ -8,19 +8,15 @@
package com.phonegap.file;

import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;

import javax.microedition.io.Connector;
import javax.microedition.io.file.FileConnection;

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.io.MIMETypeAssociations;
import net.rim.device.api.system.Application;

import org.json.me.JSONArray;
import org.json.me.JSONException;
Expand Down Expand Up @@ -167,7 +163,7 @@ else if (a == ACTION_TRUNCATE) {
*/
protected String readAsText(String filePath, String encoding) throws FileNotFoundException, UnsupportedEncodingException, IOException {
// read the file
byte[] blob = readFile(filePath);
byte[] blob = FileUtils.readFile(filePath, Connector.READ);

// return encoded file contents
Logger.log(this.getClass().getName() + ": encoding file contents using " + encoding);
Expand All @@ -184,7 +180,7 @@ protected String readAsDataURL(String filePath) throws FileNotFoundException, IO
String result = null;

// read file
byte[] blob = readFile(filePath);
byte[] blob = FileUtils.readFile(filePath, Connector.READ);

// encode file contents using BASE64 encoding
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
Expand All @@ -210,19 +206,7 @@ protected String readAsDataURL(String filePath) throws FileNotFoundException, IO
* @return file content as a byte array
*/
protected byte[] readFile(String filePath) throws FileNotFoundException, IOException {
byte[] blob = null;
DataInputStream dis = null;
try {
dis = openDataInputStream(filePath);
blob = IOUtilities.streamToBytes(dis);
} finally {
try {
if (dis != null) dis.close();
} catch (IOException e) {
Logger.log(this.getClass().getName() + ": " + e);
}
}
return blob;
return FileUtils.readFile(filePath, Connector.READ);
}

/**
Expand All @@ -232,25 +216,7 @@ protected byte[] readFile(String filePath) throws FileNotFoundException, IOExcep
* @param position Position at which to begin writing
*/
protected int writeFile(String filePath, String data, int position) throws IOException {
FileConnection fconn = null;
OutputStream os = null;
byte[] bytes = data.getBytes();
try {
fconn = (FileConnection)Connector.open(filePath, Connector.READ_WRITE);
if (!fconn.exists()) {
fconn.create();
}
os = fconn.openOutputStream(position);
os.write(bytes);
} finally {
try {
if (os != null) os.close();
if (fconn != null) fconn.close();
} catch (IOException e) {
Logger.log(this.getClass().getName() + ": " + e);
}
}
return bytes.length;
return FileUtils.writeFile(filePath, data.getBytes(), position);
}

/**
Expand Down Expand Up @@ -282,47 +248,6 @@ protected long truncateFile(String filePath, long size) throws FileNotFoundExcep
return fileSize;
}

/**
* 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
Expand Down

0 comments on commit 8c3d5cc

Please sign in to comment.