mirror of
https://github.com/apache/cordova-android.git
synced 2026-05-11 00:00:05 +08:00
Following File API spec.
This commit is contained in:
@@ -8,6 +8,11 @@
|
||||
package com.phonegap;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.Date;
|
||||
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import android.os.Environment;
|
||||
import android.os.StatFs;
|
||||
@@ -21,6 +26,8 @@ import android.util.Log;
|
||||
*/
|
||||
public class DirectoryManager {
|
||||
|
||||
private static final String LOG_TAG = "DirectoryManager";
|
||||
|
||||
/**
|
||||
* Determine if a file or directory exists.
|
||||
*
|
||||
@@ -36,7 +43,6 @@ public class DirectoryManager {
|
||||
File newPath = constructFilePaths(path.toString(), name);
|
||||
status = newPath.exists();
|
||||
}
|
||||
|
||||
// If no SD card
|
||||
else{
|
||||
status = false;
|
||||
@@ -215,8 +221,52 @@ public class DirectoryManager {
|
||||
*/
|
||||
private static File constructFilePaths (String file1, String file2) {
|
||||
File newPath;
|
||||
newPath = new File(file1+"/"+file2);
|
||||
if (file2.startsWith(file1)) {
|
||||
newPath = new File(file2);
|
||||
}
|
||||
else {
|
||||
newPath = new File(file1+"/"+file2);
|
||||
}
|
||||
return newPath;
|
||||
}
|
||||
|
||||
/**
|
||||
* This method will determine the file properties of the file specified
|
||||
* by the filePath. Creates a JSONObject with name, lastModifiedDate and
|
||||
* size properties.
|
||||
*
|
||||
* @param filePath the file to get the properties of
|
||||
* @return a JSONObject with the files properties
|
||||
*/
|
||||
protected static JSONObject getFile(String filePath) {
|
||||
File fp = new File(filePath);
|
||||
|
||||
JSONObject obj = new JSONObject();
|
||||
try {
|
||||
obj.put("name", fp.getAbsolutePath());
|
||||
obj.put("lastModifiedDate", new Date(fp.lastModified()).toString());
|
||||
obj.put("size", fp.length());
|
||||
}
|
||||
catch (JSONException e) {
|
||||
Log.e(LOG_TAG, e.getMessage(), e);
|
||||
}
|
||||
|
||||
return obj;
|
||||
}
|
||||
|
||||
/**
|
||||
* This method returns a JSONArray of file paths. Android's default
|
||||
* location where files can be written is Environment.getExternalStorageDirectory().
|
||||
* We are returning a array with one element so the interface can remain
|
||||
* consistent with BlackBerry as they have two areas where files can be
|
||||
* written.
|
||||
*
|
||||
* @return an array of file paths
|
||||
*/
|
||||
protected static JSONArray getRootPaths() {
|
||||
JSONArray retVal = new JSONArray();
|
||||
retVal.put(Environment.getExternalStorageDirectory().getAbsolutePath());
|
||||
return retVal;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -13,6 +13,7 @@ import java.nio.channels.FileChannel;
|
||||
import org.apache.commons.codec.binary.Base64;
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import com.phonegap.api.Plugin;
|
||||
import com.phonegap.api.PluginResult;
|
||||
@@ -82,6 +83,9 @@ public class FileUtils extends Plugin {
|
||||
else if (action.equals("createDirectory")) {
|
||||
boolean b = DirectoryManager.createDirectory(args.getString(0));
|
||||
return new PluginResult(status, b);
|
||||
}
|
||||
else if (action.equals("getRootPaths")) {
|
||||
return new PluginResult(status, DirectoryManager.getRootPaths());
|
||||
}
|
||||
else if (action.equals("readAsText")) {
|
||||
try {
|
||||
@@ -118,9 +122,10 @@ public class FileUtils extends Plugin {
|
||||
return new PluginResult(PluginResult.Status.ERROR, FileUtils.NOT_READABLE_ERR);
|
||||
}
|
||||
}
|
||||
else if (action.equals("truncate")) {
|
||||
else if (action.equals("write")) {
|
||||
try {
|
||||
this.truncateFile(args.getString(0), args.getLong(1));
|
||||
long fileSize = this.write(args.getString(0), args.getString(1), args.getLong(2));
|
||||
return new PluginResult(status, fileSize);
|
||||
} catch (FileNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
return new PluginResult(PluginResult.Status.ERROR, FileUtils.NOT_FOUND_ERR);
|
||||
@@ -129,6 +134,22 @@ public class FileUtils extends Plugin {
|
||||
return new PluginResult(PluginResult.Status.ERROR, FileUtils.NOT_READABLE_ERR);
|
||||
}
|
||||
}
|
||||
else if (action.equals("truncate")) {
|
||||
try {
|
||||
long fileSize = this.truncateFile(args.getString(0), args.getLong(1));
|
||||
return new PluginResult(status, fileSize);
|
||||
} catch (FileNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
return new PluginResult(PluginResult.Status.ERROR, FileUtils.NOT_FOUND_ERR);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
return new PluginResult(PluginResult.Status.ERROR, FileUtils.NOT_READABLE_ERR);
|
||||
}
|
||||
}
|
||||
else if (action.equals("getFile")) {
|
||||
JSONObject obj = DirectoryManager.getFile(args.getString(0));
|
||||
return new PluginResult(status, obj);
|
||||
}
|
||||
return new PluginResult(status, result);
|
||||
} catch (JSONException e) {
|
||||
e.printStackTrace();
|
||||
@@ -169,15 +190,14 @@ public class FileUtils extends Plugin {
|
||||
* @throws FileNotFoundException, IOException
|
||||
*/
|
||||
public String readAsText(String filename, String encoding) throws FileNotFoundException, IOException {
|
||||
StringBuilder data = new StringBuilder();
|
||||
FileInputStream fis = new FileInputStream(filename);
|
||||
BufferedReader reader = new BufferedReader(new InputStreamReader(fis, encoding), 1024);
|
||||
String line;
|
||||
while ((line = reader.readLine()) != null) {
|
||||
data.append(line);
|
||||
data.append('\n');
|
||||
}
|
||||
return data.toString();
|
||||
byte[] bytes = new byte[1000];
|
||||
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(filename), 1024);
|
||||
ByteArrayOutputStream bos = new ByteArrayOutputStream();
|
||||
int numRead = 0;
|
||||
while ((numRead = bis.read(bytes, 0, 1000)) >= 0) {
|
||||
bos.write(bytes, 0, numRead);
|
||||
}
|
||||
return new String(bos.toByteArray(), encoding);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -225,6 +245,23 @@ public class FileUtils extends Plugin {
|
||||
out.close();
|
||||
}
|
||||
|
||||
/**
|
||||
* Write contents of file.
|
||||
*
|
||||
* @param filename The name of the file.
|
||||
* @param data The contents of the file.
|
||||
* @param offset The position to begin writing the file.
|
||||
* @throws FileNotFoundException, IOException
|
||||
*/
|
||||
public long write(String filename, String data, long offset) throws FileNotFoundException, IOException {
|
||||
RandomAccessFile file = new RandomAccessFile(filename, "rw");
|
||||
file.seek(offset);
|
||||
file.writeBytes(data);
|
||||
file.close();
|
||||
|
||||
return data.length();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Truncate the file to size
|
||||
@@ -233,13 +270,16 @@ public class FileUtils extends Plugin {
|
||||
* @param size
|
||||
* @throws FileNotFoundException, IOException
|
||||
*/
|
||||
private void truncateFile(String filename, long size) throws FileNotFoundException, IOException {
|
||||
private long truncateFile(String filename, long size) throws FileNotFoundException, IOException {
|
||||
RandomAccessFile raf = new RandomAccessFile(filename, "rw");
|
||||
|
||||
|
||||
if (raf.length() >= size) {
|
||||
FileChannel channel = raf.getChannel();
|
||||
channel.truncate(size);
|
||||
return size;
|
||||
}
|
||||
|
||||
return raf.length();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user