From af0feabb6a32ac0ccb9bfd2ffc980725c4bf966c Mon Sep 17 00:00:00 2001 From: Andrew Grieve Date: Fri, 13 Jul 2012 16:29:59 -0400 Subject: [PATCH] Prefer setFixedLengthStreamingMode over setChunkedStreamingMode in FileTransfer. setFixedLengthStreamingMode causes the Content-Length header to be set, which some servers require. We now use setChunkedStreamingMode only on Eclair devices since there is a bug with setFixedLengthStreamingMode in that version of the OS. --- .../src/org/apache/cordova/FileTransfer.java | 21 +++++++++---------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/framework/src/org/apache/cordova/FileTransfer.java b/framework/src/org/apache/cordova/FileTransfer.java index 4adace5b..6cde4b53 100644 --- a/framework/src/org/apache/cordova/FileTransfer.java +++ b/framework/src/org/apache/cordova/FileTransfer.java @@ -49,6 +49,7 @@ import org.json.JSONException; import org.json.JSONObject; import android.net.Uri; +import android.os.Build; import android.util.Log; import android.webkit.CookieManager; @@ -239,19 +240,17 @@ public class FileTransfer extends Plugin { String tailParams = LINE_END + LINE_START + BOUNDARY + LINE_START + LINE_END; byte[] fileNameBytes = fileName.getBytes("UTF-8"); - // Should set this up as an option - if (chunkedMode) { + int stringLength = extraBytes.length + midParams.length() + tailParams.length() + fileNameBytes.length; + Log.d(LOG_TAG, "String Length: " + stringLength); + int fixedLength = (int) fileInputStream.getChannel().size() + stringLength; + Log.d(LOG_TAG, "Content Length: " + fixedLength); + // setFixedLengthStreamingMode causes and OutOfMemoryException on pre-Froyo devices. + // http://code.google.com/p/android/issues/detail?id=3164 + if (Build.VERSION.SDK_INT < Build.VERSION_CODES.FROYO && chunkedMode) { conn.setChunkedStreamingMode(maxBufferSize); + } else { + conn.setFixedLengthStreamingMode(fixedLength); } - else - { - int stringLength = extraBytes.length + midParams.length() + tailParams.length() + fileNameBytes.length; - Log.d(LOG_TAG, "String Length: " + stringLength); - int fixedLength = (int) fileInputStream.getChannel().size() + stringLength; - Log.d(LOG_TAG, "Content Length: " + fixedLength); - conn.setFixedLengthStreamingMode(fixedLength); - } - dos = new DataOutputStream( conn.getOutputStream() ); //We don't want to change encoding, we just want this to write for all Unicode.