Skip to content

Commit

Permalink
Merge pull request #5733 from hieupham007/timob-15594-3_3_X
Browse files Browse the repository at this point in the history
[Timob 15594] (3_3_x): Backport of base64 encoding failing on large files
  • Loading branch information
hieupham007 committed May 23, 2014
2 parents a94b42c + 3ccd40e commit baea217
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
Expand Down
26 changes: 13 additions & 13 deletions android/titanium/src/java/org/appcelerator/titanium/TiBlob.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}

/**
Expand Down Expand Up @@ -285,7 +285,7 @@ public byte[] getBytes()
}
}
break;
case TYPE_STREAM:
case TYPE_STREAM_BASE64:
InputStream inStream = (InputStream)data;
if (inStream != null) {
try {
Expand Down Expand Up @@ -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;
Expand All @@ -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());
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit baea217

Please sign in to comment.