Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Timob 15594] (3_3_x): Backport of base64 encoding failing on large files #5733

Merged
merged 1 commit into from
May 23, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
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
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