Skip to content

Commit baea217

Browse files
committed
Merge pull request #5733 from hieupham007/timob-15594-3_3_X
[Timob 15594] (3_3_x): Backport of base64 encoding failing on large files
2 parents a94b42c + 3ccd40e commit baea217

File tree

3 files changed

+18
-18
lines changed

3 files changed

+18
-18
lines changed

android/modules/network/src/java/ti/modules/titanium/network/TiHTTPClient.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@
107107
import ti.modules.titanium.xml.XMLModule;
108108
import android.net.Uri;
109109
import android.os.Build;
110+
import android.util.Base64OutputStream;
110111

111112
public class TiHTTPClient
112113
{
@@ -955,8 +956,8 @@ private int addTitaniumFileAsPostData(String name, Object value)
955956
String mimeType = blob.getMimeType();
956957
File tmpFile = File.createTempFile("tixhr", "." + TiMimeTypeHelper.getFileExtensionFromMimeType(mimeType, "txt"));
957958
FileOutputStream fos = new FileOutputStream(tmpFile);
958-
if (blob.getType() == TiBlob.TYPE_STREAM) {
959-
TiBaseFile.copyStream(blob.getInputStream(), fos);
959+
if (blob.getType() == TiBlob.TYPE_STREAM_BASE64) {
960+
TiBaseFile.copyStream(blob.getInputStream(), new Base64OutputStream(fos, android.util.Base64.DEFAULT));
960961
} else {
961962
fos.write(blob.getBytes());
962963
}

android/modules/utils/src/java/ti/modules/titanium/utils/UtilsModule.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,8 @@ public TiBlob base64encode(Object obj)
6060
return TiBlob.blobFromString(((TiBlob) obj).toBase64());
6161
} else if (obj instanceof TiFileProxy) {
6262
try {
63-
return TiBlob.blobFromStream(new Base64InputStream(((TiFileProxy) obj).getInputStream(),
64-
android.util.Base64.DEFAULT), TiMimeTypeHelper.getMimeType(((TiFileProxy) obj).getBaseFile()
65-
.nativePath()));
63+
return TiBlob.blobFromStreamBase64(((TiFileProxy) obj).getInputStream(),
64+
TiMimeTypeHelper.getMimeType(((TiFileProxy) obj).getBaseFile().nativePath()));
6665
} catch (IOException e) {
6766
Log.e(TAG, "Problem reading file");
6867
}

android/titanium/src/java/org/appcelerator/titanium/TiBlob.java

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,10 @@ public class TiBlob extends KrollProxy
6565
public static final int TYPE_STRING = 3;
6666

6767
/**
68-
* Represents a Blob that contains stream data.
68+
* Represents a Blob that contains stream data that needs to be converted to base64.
6969
* @module.api
7070
*/
71-
public static final int TYPE_STREAM = 4;
71+
public static final int TYPE_STREAM_BASE64 = 4;
7272

7373
private int type;
7474
private Object data;
@@ -110,13 +110,13 @@ public static TiBlob blobFromFile(TiBaseFile file)
110110
}
111111

112112
/**
113-
* Creates a blob from a stream.
113+
* Creates a blob from a stream to convert to base64.
114114
* @param stream the stream used to create blob.
115115
* @return new instane of TiBlob.
116116
*/
117-
public static TiBlob blobFromStream(InputStream stream, String mimeType)
117+
public static TiBlob blobFromStreamBase64(InputStream stream, String mimeType)
118118
{
119-
return new TiBlob(TYPE_STREAM, stream, mimeType);
119+
return new TiBlob(TYPE_STREAM_BASE64, stream, mimeType);
120120
}
121121

122122
/**
@@ -285,7 +285,7 @@ public byte[] getBytes()
285285
}
286286
}
287287
break;
288-
case TYPE_STREAM:
288+
case TYPE_STREAM_BASE64:
289289
InputStream inStream = (InputStream)data;
290290
if (inStream != null) {
291291
try {
@@ -320,8 +320,8 @@ public int getLength()
320320
case TYPE_DATA:
321321
case TYPE_IMAGE:
322322
return ((byte[])data).length;
323-
case TYPE_STREAM:
324-
throw new IllegalStateException("Not yet implemented. TYPE_STREAM");
323+
case TYPE_STREAM_BASE64:
324+
throw new IllegalStateException("Not yet implemented. TYPE_STREAM_BASE64");
325325
default:
326326
// this is probably overly expensive.. is there a better way?
327327
return getBytes().length;
@@ -342,7 +342,7 @@ public InputStream getInputStream()
342342
Log.e(TAG, e.getMessage(), e);
343343
return null;
344344
}
345-
case TYPE_STREAM:
345+
case TYPE_STREAM_BASE64:
346346
return (InputStream)data;
347347
default:
348348
return new ByteArrayInputStream(getBytes());
@@ -373,8 +373,8 @@ public void append(TiBlob blob)
373373
break;
374374
case TYPE_FILE :
375375
throw new IllegalStateException("Not yet implemented. TYPE_FILE");
376-
case TYPE_STREAM :
377-
throw new IllegalStateException("Not yet implemented. TYPE_STREAM");
376+
case TYPE_STREAM_BASE64 :
377+
throw new IllegalStateException("Not yet implemented. TYPE_STREAM_BASE64");
378378
// break;
379379
default :
380380
throw new IllegalArgumentException("Unknown Blob type id " + type);
@@ -405,8 +405,8 @@ public String getText()
405405
Log.w(TAG, "Unable to convert to string.");
406406
}
407407
break;
408-
case TYPE_STREAM :
409-
throw new IllegalStateException("Not yet implemented. TYPE_STREAM");
408+
case TYPE_STREAM_BASE64 :
409+
throw new IllegalStateException("Not yet implemented. TYPE_STREAM_BASE64");
410410
}
411411

412412
return result;

0 commit comments

Comments
 (0)