cordova-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ste...@apache.org
Subject [07/11] cordova-plugin-file git commit: CB-6428 android: Fix assets FileEntry having size of -1
Date Tue, 31 Mar 2015 17:58:16 GMT
CB-6428 android: Fix assets FileEntry having size of -1


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/ed4cb4b9
Tree: http://git-wip-us.apache.org/repos/asf/cordova-plugin-file/tree/ed4cb4b9
Diff: http://git-wip-us.apache.org/repos/asf/cordova-plugin-file/diff/ed4cb4b9

Branch: refs/heads/old-ID
Commit: ed4cb4b999373eaa80b2fe1f38e3fc24db1b00ae
Parents: 2ce5c49
Author: Andrew Grieve <agrieve@chromium.org>
Authored: Tue Mar 17 20:58:36 2015 -0400
Committer: Andrew Grieve <agrieve@chromium.org>
Committed: Tue Mar 17 20:58:36 2015 -0400

----------------------------------------------------------------------
 src/android/AssetFilesystem.java | 75 ++++++++++++++++++++++++-----------
 src/android/build-extras.gradle  |  7 ++--
 2 files changed, 56 insertions(+), 26 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cordova-plugin-file/blob/ed4cb4b9/src/android/AssetFilesystem.java
----------------------------------------------------------------------
diff --git a/src/android/AssetFilesystem.java b/src/android/AssetFilesystem.java
index 934a237..f501b27 100644
--- a/src/android/AssetFilesystem.java
+++ b/src/android/AssetFilesystem.java
@@ -43,22 +43,27 @@ public class AssetFilesystem extends Filesystem {
     private static Object listCacheLock = new Object();
     private static boolean listCacheFromFile;
     private static Map<String, String[]> listCache;
+    private static Map<String, Long> lengthCache;
 
-    private String[] listAssets(String assetPath) throws IOException {
+    private void lazyInitCaches() {
         synchronized (listCacheLock) {
             if (listCache == null) {
                 ObjectInputStream ois = null;
                 try {
                     ois = new ObjectInputStream(assetManager.open("cdvasset.manifest"));
                     listCache = (Map<String, String[]>) ois.readObject();
+                    lengthCache = (Map<String, Long>) ois.readObject();
                     listCacheFromFile = true;
-                } catch (FileNotFoundException e) {
-                    // Asset manifest won't exist if the gradle hook isn't set up correctly.
                 } catch (ClassNotFoundException e) {
                     e.printStackTrace();
+                } catch (IOException e) {
+                    // Asset manifest won't exist if the gradle hook isn't set up correctly.
                 } finally {
                     if (ois != null) {
-                        ois.close();
+                        try {
+                            ois.close();
+                        } catch (IOException e) {
+                        }
                     }
                 }
                 if (listCache == null) {
@@ -67,6 +72,13 @@ public class AssetFilesystem extends Filesystem {
                 }
             }
         }
+    }
+
+    private String[] listAssets(String assetPath) throws IOException {
+        if (assetPath.startsWith("/")) {
+            assetPath = assetPath.substring(1);
+        }
+        lazyInitCaches();
         String[] ret = listCache.get(assetPath);
         if (ret == null) {
             if (listCacheFromFile) {
@@ -79,6 +91,39 @@ public class AssetFilesystem extends Filesystem {
         return ret;
     }
 
+    private long getAssetSize(String assetPath) throws FileNotFoundException {
+        if (assetPath.startsWith("/")) {
+            assetPath = assetPath.substring(1);
+        }
+        lazyInitCaches();
+        if (lengthCache != null) {
+            Long ret = lengthCache.get(assetPath);
+            if (ret == null) {
+                throw new FileNotFoundException("Asset not found: " + assetPath);
+            }
+            return ret;
+        }
+        CordovaResourceApi.OpenForReadResult offr = null;
+        try {
+            offr = resourceApi.openForRead(nativeUriForFullPath(assetPath));
+            long length = offr.length;
+            if (length < 0) {
+                // available() doesn't always yield the file size, but for assets it does.
+                length = offr.inputStream.available();
+            }
+            return length;
+        } catch (IOException e) {
+            throw new FileNotFoundException("File not found: " + assetPath);
+        } finally {
+            if (offr != null) {
+                try {
+                    offr.inputStream.close();
+                } catch (IOException e) {
+                }
+            }
+        }
+    }
+
     public AssetFilesystem(AssetManager assetManager, CordovaResourceApi resourceApi) {
         super(Uri.parse("file:///android_asset/"), "assets", resourceApi);
         this.assetManager = assetManager;
@@ -122,9 +167,6 @@ public class AssetFilesystem extends Filesystem {
     }
 
     private boolean isDirectory(String assetPath) {
-        if (assetPath.startsWith("/")) {
-            assetPath = assetPath.substring(1);
-        }
         try {
             return listAssets(assetPath).length != 0;
         } catch (IOException e) {
@@ -187,31 +229,18 @@ public class AssetFilesystem extends Filesystem {
         return makeEntryForURL(requestedURL);
     }
 
-
     @Override
 	public JSONObject getFileMetadataForLocalURL(LocalFilesystemURL inputURL) throws FileNotFoundException
{
-        CordovaResourceApi.OpenForReadResult offr;
-        try {
-            offr = inputURL.isDirectory ? null : resourceApi.openForRead(toNativeUri(inputURL));
-        } catch (IOException e) {
-            throw new FileNotFoundException("File not found: " + inputURL);
-        }
         JSONObject metadata = new JSONObject();
+        long size = inputURL.isDirectory ? 0 : getAssetSize(inputURL.path);
         try {
-        	metadata.put("size", inputURL.isDirectory ? 0 : offr.length);
-        	metadata.put("type", inputURL.isDirectory ? "text/directory" : offr.mimeType);
+        	metadata.put("size", size);
+        	metadata.put("type", inputURL.isDirectory ? "text/directory" : resourceApi.getMimeType(toNativeUri(inputURL)));
         	metadata.put("name", new File(inputURL.path).getName());
         	metadata.put("fullPath", inputURL.path);
         	metadata.put("lastModifiedDate", 0);
         } catch (JSONException e) {
             return null;
-        } finally {
-            if (offr != null) {
-                try {
-                    offr.inputStream.close();
-                } catch (IOException e) {
-                }
-            }
         }
         return metadata;
 	}

http://git-wip-us.apache.org/repos/asf/cordova-plugin-file/blob/ed4cb4b9/src/android/build-extras.gradle
----------------------------------------------------------------------
diff --git a/src/android/build-extras.gradle b/src/android/build-extras.gradle
index 86d91a6..a0a7844 100644
--- a/src/android/build-extras.gradle
+++ b/src/android/build-extras.gradle
@@ -22,21 +22,22 @@ ext.postBuildExtras = {
     def outFile = new File(outAssetsDir, "cdvasset.manifest")
 
     def newTask = task("cdvCreateAssetManifest") << {
-        println("Reading from ${inAssetsDir}")
-        println("Writing to ${outFile}")
-
         def contents = new HashMap()
+        def sizes = new HashMap()
         contents[""] = inAssetsDir.list()
         def tree = fileTree(dir: inAssetsDir)
         tree.visit { fileDetails ->
             if (fileDetails.isDirectory()) {
                 contents[fileDetails.relativePath.toString()] = fileDetails.file.list()
+            } else {
+                sizes[fileDetails.relativePath.toString()] = fileDetails.file.length()
             }
         }
 
         outAssetsDir.mkdirs()
         outFile.withObjectOutputStream { oos ->
             oos.writeObject(contents)
+            oos.writeObject(sizes)
         }
     }
     newTask.inputs.dir inAssetsDir


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@cordova.apache.org
For additional commands, e-mail: commits-help@cordova.apache.org


Mime
View raw message