From 3ccd40efde59a2c75c4db968d03e69a4c10b9fcd Mon Sep 17 00:00:00 2001 From: salachi Date: Tue, 13 May 2014 21:52:19 +0530 Subject: [PATCH] TIMOB-15594-Used Base64OutputStream to encode --- .../titanium/network/TiHTTPClient.java | 5 ++-- .../modules/titanium/utils/UtilsModule.java | 5 ++-- .../org/appcelerator/titanium/TiBlob.java | 26 +++++++++---------- 3 files changed, 18 insertions(+), 18 deletions(-) diff --git a/android/modules/network/src/java/ti/modules/titanium/network/TiHTTPClient.java b/android/modules/network/src/java/ti/modules/titanium/network/TiHTTPClient.java index 793c26f2917..b0d5a9c71f0 100644 --- a/android/modules/network/src/java/ti/modules/titanium/network/TiHTTPClient.java +++ b/android/modules/network/src/java/ti/modules/titanium/network/TiHTTPClient.java @@ -107,6 +107,7 @@ import ti.modules.titanium.xml.XMLModule; import android.net.Uri; import android.os.Build; +import android.util.Base64OutputStream; public class TiHTTPClient { @@ -955,8 +956,8 @@ private int addTitaniumFileAsPostData(String name, Object value) String mimeType = blob.getMimeType(); File tmpFile = File.createTempFile("tixhr", "." + TiMimeTypeHelper.getFileExtensionFromMimeType(mimeType, "txt")); FileOutputStream fos = new FileOutputStream(tmpFile); - if (blob.getType() == TiBlob.TYPE_STREAM) { - TiBaseFile.copyStream(blob.getInputStream(), fos); + if (blob.getType() == TiBlob.TYPE_STREAM_BASE64) { + TiBaseFile.copyStream(blob.getInputStream(), new Base64OutputStream(fos, android.util.Base64.DEFAULT)); } else { fos.write(blob.getBytes()); } diff --git a/android/modules/utils/src/java/ti/modules/titanium/utils/UtilsModule.java b/android/modules/utils/src/java/ti/modules/titanium/utils/UtilsModule.java index 87ba06a09ff..9d3a5048e43 100644 --- a/android/modules/utils/src/java/ti/modules/titanium/utils/UtilsModule.java +++ b/android/modules/utils/src/java/ti/modules/titanium/utils/UtilsModule.java @@ -60,9 +60,8 @@ public TiBlob base64encode(Object obj) return TiBlob.blobFromString(((TiBlob) obj).toBase64()); } else if (obj instanceof TiFileProxy) { try { - return TiBlob.blobFromStream(new Base64InputStream(((TiFileProxy) obj).getInputStream(), - android.util.Base64.DEFAULT), TiMimeTypeHelper.getMimeType(((TiFileProxy) obj).getBaseFile() - .nativePath())); + return TiBlob.blobFromStreamBase64(((TiFileProxy) obj).getInputStream(), + TiMimeTypeHelper.getMimeType(((TiFileProxy) obj).getBaseFile().nativePath())); } catch (IOException e) { Log.e(TAG, "Problem reading file"); } diff --git a/android/titanium/src/java/org/appcelerator/titanium/TiBlob.java b/android/titanium/src/java/org/appcelerator/titanium/TiBlob.java index 9ddfb49bca0..8ff212e1de9 100644 --- a/android/titanium/src/java/org/appcelerator/titanium/TiBlob.java +++ b/android/titanium/src/java/org/appcelerator/titanium/TiBlob.java @@ -65,10 +65,10 @@ public class TiBlob extends KrollProxy public static final int TYPE_STRING = 3; /** - * Represents a Blob that contains stream data. + * Represents a Blob that contains stream data that needs to be converted to base64. * @module.api */ - public static final int TYPE_STREAM = 4; + public static final int TYPE_STREAM_BASE64 = 4; private int type; private Object data; @@ -110,13 +110,13 @@ public static TiBlob blobFromFile(TiBaseFile file) } /** - * Creates a blob from a stream. + * Creates a blob from a stream to convert to base64. * @param stream the stream used to create blob. * @return new instane of TiBlob. */ - public static TiBlob blobFromStream(InputStream stream, String mimeType) + public static TiBlob blobFromStreamBase64(InputStream stream, String mimeType) { - return new TiBlob(TYPE_STREAM, stream, mimeType); + return new TiBlob(TYPE_STREAM_BASE64, stream, mimeType); } /** @@ -285,7 +285,7 @@ public byte[] getBytes() } } break; - case TYPE_STREAM: + case TYPE_STREAM_BASE64: InputStream inStream = (InputStream)data; if (inStream != null) { try { @@ -320,8 +320,8 @@ public int getLength() case TYPE_DATA: case TYPE_IMAGE: return ((byte[])data).length; - case TYPE_STREAM: - throw new IllegalStateException("Not yet implemented. TYPE_STREAM"); + case TYPE_STREAM_BASE64: + throw new IllegalStateException("Not yet implemented. TYPE_STREAM_BASE64"); default: // this is probably overly expensive.. is there a better way? return getBytes().length; @@ -342,7 +342,7 @@ public InputStream getInputStream() Log.e(TAG, e.getMessage(), e); return null; } - case TYPE_STREAM: + case TYPE_STREAM_BASE64: return (InputStream)data; default: return new ByteArrayInputStream(getBytes()); @@ -373,8 +373,8 @@ public void append(TiBlob blob) break; case TYPE_FILE : throw new IllegalStateException("Not yet implemented. TYPE_FILE"); - case TYPE_STREAM : - throw new IllegalStateException("Not yet implemented. TYPE_STREAM"); + case TYPE_STREAM_BASE64 : + throw new IllegalStateException("Not yet implemented. TYPE_STREAM_BASE64"); // break; default : throw new IllegalArgumentException("Unknown Blob type id " + type); @@ -405,8 +405,8 @@ public String getText() Log.w(TAG, "Unable to convert to string."); } break; - case TYPE_STREAM : - throw new IllegalStateException("Not yet implemented. TYPE_STREAM"); + case TYPE_STREAM_BASE64 : + throw new IllegalStateException("Not yet implemented. TYPE_STREAM_BASE64"); } return result;