From 2e20bb063933a5cc27ae95b0ec70c59c5b9ae3b9 Mon Sep 17 00:00:00 2001
From: Andrew Grieve <agrieve@chromium.org>
Date: Tue, 29 Jan 2013 13:47:15 -0500
Subject: [PATCH] [CB-2293] Fix typo bufferSize->bytesRead.

Also adds a log statement to print out amount uploaded when an
IOException is thrown.
---
 framework/src/org/apache/cordova/FileTransfer.java | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)

diff --git a/framework/src/org/apache/cordova/FileTransfer.java b/framework/src/org/apache/cordova/FileTransfer.java
index c0bd1bd9..783a71b6 100644
--- a/framework/src/org/apache/cordova/FileTransfer.java
+++ b/framework/src/org/apache/cordova/FileTransfer.java
@@ -215,6 +215,8 @@ public class FileTransfer extends CordovaPlugin {
                 HttpURLConnection conn = null;
                 HostnameVerifier oldHostnameVerifier = null;
                 SSLSocketFactory oldSocketFactory = null;
+                int totalBytes = 0;
+                int fixedLength = -1;
                 try {
                     // Create return object
                     FileUploadResult result = new FileUploadResult();
@@ -320,7 +322,6 @@ public class FileTransfer extends CordovaPlugin {
                     
                     int stringLength = extraBytes.length + midParams.length() + tailParams.length() + fileNameBytes.length;
                     Log.d(LOG_TAG, "String Length: " + stringLength);
-                    int fixedLength = -1;
                     if (sourceInputStream instanceof FileInputStream) {
                         fixedLength = (int) ((FileInputStream)sourceInputStream).getChannel().size() + stringLength;
                         progress.setLengthComputable(true);
@@ -363,13 +364,12 @@ public class FileTransfer extends CordovaPlugin {
     
                         // read file and write it into form...
                         int bytesRead = sourceInputStream.read(buffer, 0, bufferSize);
-                        long totalBytes = 0;
     
                         long prevBytesRead = 0;
                         while (bytesRead > 0) {
                             totalBytes += bytesRead;
                             result.setBytesSent(totalBytes);
-                            dos.write(buffer, 0, bufferSize);
+                            dos.write(buffer, 0, bytesRead);
                             if (totalBytes > prevBytesRead + 102400) {
                                 prevBytesRead = totalBytes;
                                 Log.d(LOG_TAG, "Uploaded " + totalBytes + " of " + fixedLength + " bytes");
@@ -436,6 +436,7 @@ public class FileTransfer extends CordovaPlugin {
                 } catch (IOException e) {
                     JSONObject error = createFileTransferError(CONNECTION_ERR, source, target, conn);
                     Log.e(LOG_TAG, error.toString(), e);
+                    Log.e(LOG_TAG, "Failed after uploading " + totalBytes + " of " + fixedLength + " bytes.");
                     context.sendPluginResult(new PluginResult(PluginResult.Status.IO_EXCEPTION, error));
                 } catch (JSONException e) {
                     Log.e(LOG_TAG, e.getMessage(), e);