cordova-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ste...@apache.org
Subject [08/11] cordova-plugin-file git commit: CB-8699 CB-6428 Fix uncompressed assets being copied as zero length files
Date Tue, 31 Mar 2015 17:58:17 GMT
CB-8699 CB-6428 Fix uncompressed assets being copied as zero length files


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

Branch: refs/heads/old-ID
Commit: 67924cf9f679d3e6cbb05411f76f90405f36288c
Parents: ed4cb4b
Author: Andrew Grieve <agrieve@chromium.org>
Authored: Tue Mar 17 21:40:01 2015 -0400
Committer: Andrew Grieve <agrieve@chromium.org>
Committed: Tue Mar 17 21:40:01 2015 -0400

----------------------------------------------------------------------
 src/android/LocalFilesystem.java | 45 ++++++++++++++++++++++++++++++++++-
 1 file changed, 44 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cordova-plugin-file/blob/67924cf9/src/android/LocalFilesystem.java
----------------------------------------------------------------------
diff --git a/src/android/LocalFilesystem.java b/src/android/LocalFilesystem.java
index 258b957..7f5431c 100644
--- a/src/android/LocalFilesystem.java
+++ b/src/android/LocalFilesystem.java
@@ -20,9 +20,12 @@ 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.OutputStream;
 import java.io.RandomAccessFile;
 import java.nio.channels.FileChannel;
 import org.apache.cordova.CordovaResourceApi;
@@ -262,7 +265,7 @@ public class LocalFilesystem extends Filesystem {
         }
 
         CordovaResourceApi.OpenForReadResult offr = resourceApi.openForRead(srcFs.toNativeUri(srcURL));
-        resourceApi.copyResource(offr, new FileOutputStream(destFile));
+        copyResource(offr, new FileOutputStream(destFile));
 
         if (move) {
             srcFs.removeFileAtLocalURL(srcURL);
@@ -458,4 +461,44 @@ public class LocalFilesystem extends Filesystem {
 		File file = new File(path);
 		return file.exists();
 	}
+
+    // This is a copy & paste from CordovaResource API that is required since CordovaResourceApi
+    // has a bug pre-4.0.0.
+    // TODO: Once cordova-android@4.0.0 is released, delete this copy and make the plugin
depend on
+    // 4.0.0 with an engine tag.
+    private static void copyResource(CordovaResourceApi.OpenForReadResult input, OutputStream
outputStream) throws IOException {
+        try {
+            InputStream inputStream = input.inputStream;
+            if (inputStream instanceof FileInputStream && outputStream instanceof
FileOutputStream) {
+                FileChannel inChannel = ((FileInputStream)input.inputStream).getChannel();
+                FileChannel outChannel = ((FileOutputStream)outputStream).getChannel();
+                long offset = 0;
+                long length = input.length;
+                if (input.assetFd != null) {
+                    offset = input.assetFd.getStartOffset();
+                }
+                // transferFrom()'s 2nd arg is a relative position. Need to set the absolute
+                // position first.
+                inChannel.position(offset);
+                outChannel.transferFrom(inChannel, 0, length);
+            } else {
+                final int BUFFER_SIZE = 8192;
+                byte[] buffer = new byte[BUFFER_SIZE];
+
+                for (;;) {
+                    int bytesRead = inputStream.read(buffer, 0, BUFFER_SIZE);
+
+                    if (bytesRead <= 0) {
+                        break;
+                    }
+                    outputStream.write(buffer, 0, bytesRead);
+                }
+            }
+        } finally {
+            input.inputStream.close();
+            if (outputStream != null) {
+                outputStream.close();
+            }
+        }
+    }
 }


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


Mime
View raw message