You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cordova.apache.org by ag...@apache.org on 2014/05/06 16:25:48 UTC

git commit: CB-6378 Use connection.disconnect() instead of stream.close() for thread-safety

Repository: cordova-plugin-file-transfer
Updated Branches:
  refs/heads/master 2ecc6859e -> 67ae8d885


CB-6378 Use connection.disconnect() instead of stream.close() for thread-safety


Project: http://git-wip-us.apache.org/repos/asf/cordova-plugin-file-transfer/repo
Commit: http://git-wip-us.apache.org/repos/asf/cordova-plugin-file-transfer/commit/67ae8d88
Tree: http://git-wip-us.apache.org/repos/asf/cordova-plugin-file-transfer/tree/67ae8d88
Diff: http://git-wip-us.apache.org/repos/asf/cordova-plugin-file-transfer/diff/67ae8d88

Branch: refs/heads/master
Commit: 67ae8d8852bcd53d5c8df514016b6b099eb399ef
Parents: 2ecc685
Author: Andrew Grieve <ag...@chromium.org>
Authored: Tue May 6 10:25:01 2014 -0400
Committer: Andrew Grieve <ag...@chromium.org>
Committed: Tue May 6 10:25:01 2014 -0400

----------------------------------------------------------------------
 src/android/FileTransfer.java | 44 +++++++++++++++++++++-----------------
 1 file changed, 24 insertions(+), 20 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cordova-plugin-file-transfer/blob/67ae8d88/src/android/FileTransfer.java
----------------------------------------------------------------------
diff --git a/src/android/FileTransfer.java b/src/android/FileTransfer.java
index eeed2f6..8c22e0d 100644
--- a/src/android/FileTransfer.java
+++ b/src/android/FileTransfer.java
@@ -81,8 +81,7 @@ public class FileTransfer extends CordovaPlugin {
         String target;
         File targetFile;
         CallbackContext callbackContext;
-        InputStream currentInputStream;
-        OutputStream currentOutputStream;
+        HttpURLConnection connection;
         boolean aborted;
         RequestContext(String source, String target, CallbackContext callbackContext) {
             this.source = source;
@@ -384,7 +383,7 @@ public class FileTransfer extends CordovaPlugin {
                             if (context.aborted) {
                                 return;
                             }
-                            context.currentOutputStream = sendStream;
+                            context.connection = conn;
                         }
                         //We don't want to change encoding, we just want this to write for all Unicode.
                         sendStream.write(beforeDataBytes);
@@ -426,7 +425,9 @@ public class FileTransfer extends CordovaPlugin {
                         safeClose(readResult.inputStream);
                         safeClose(sendStream);
                     }
-                    context.currentOutputStream = null;
+                    synchronized (context) {
+                        context.connection = null;
+                    }
                     Log.d(LOG_TAG, "Sent " + totalBytes + " of " + fixedLength);
 
                     //------------------ read the SERVER RESPONSE
@@ -441,7 +442,7 @@ public class FileTransfer extends CordovaPlugin {
                             if (context.aborted) {
                                 return;
                             }
-                            context.currentInputStream = inStream;
+                            context.connection = conn;
                         }
                         
                         ByteArrayOutputStream out = new ByteArrayOutputStream(Math.max(1024, conn.getContentLength()));
@@ -453,7 +454,9 @@ public class FileTransfer extends CordovaPlugin {
                         }
                         responseString = out.toString("UTF-8");
                     } finally {
-                        context.currentInputStream = null;
+                        synchronized (context) {
+                            context.connection = null;
+                        }
                         safeClose(inStream);
                     }
                     
@@ -767,7 +770,7 @@ public class FileTransfer extends CordovaPlugin {
                             if (context.aborted) {
                                 return;
                             }
-                            context.currentInputStream = inputStream;
+                            context.connection = connection;
                         }
                         
                         // write bytes to file
@@ -782,7 +785,9 @@ public class FileTransfer extends CordovaPlugin {
                             context.sendPluginResult(progressResult);
                         }
                     } finally {
-                        context.currentInputStream = null;
+                        synchronized (context) {
+                            context.connection = null;
+                        }
                         safeClose(inputStream);
                         safeClose(outputStream);
                     }
@@ -857,22 +862,21 @@ public class FileTransfer extends CordovaPlugin {
             context = activeRequests.remove(objectId);
         }
         if (context != null) {
-            File file = context.targetFile;
-            if (file != null) {
-                file.delete();
-            }
-            // Trigger the abort callback immediately to minimize latency between it and abort() being called.
-            JSONObject error = createFileTransferError(ABORTED_ERR, context.source, context.target, null, -1);
-            synchronized (context) {
-                context.sendPluginResult(new PluginResult(PluginResult.Status.ERROR, error));
-                context.aborted = true;
-            }
             // Closing the streams can block, so execute on a background thread.
             cordova.getThreadPool().execute(new Runnable() {
                 public void run() {
                     synchronized (context) {
-                        safeClose(context.currentInputStream);
-                        safeClose(context.currentOutputStream);
+                        File file = context.targetFile;
+                        if (file != null) {
+                            file.delete();
+                        }
+                        // Trigger the abort callback immediately to minimize latency between it and abort() being called.
+                        JSONObject error = createFileTransferError(ABORTED_ERR, context.source, context.target, null, -1);
+                        context.sendPluginResult(new PluginResult(PluginResult.Status.ERROR, error));
+                        context.aborted = true;
+                        if (context.connection != null) {
+                            context.connection.disconnect();
+                        }
                     }
                 }
             });