Skip to content

Commit

Permalink
[TIMOB-26954] Android: Fixed bug where File.read().text returns null …
Browse files Browse the repository at this point in the history
…on Android Q (#10931)

- Modified Ti.Blob "text" property to always return text even if its a binary type, like iOS.
- Modified Ti.Blob toString() method to only return blob's text if mimetype is a known text type, like how "text" property used to work.
  • Loading branch information
jquick-axway authored and lokeshchdhry committed Jun 5, 2019
1 parent b8e5cd2 commit 3f49d79
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 27 deletions.
40 changes: 23 additions & 17 deletions android/titanium/src/java/org/appcelerator/titanium/TiBlob.java
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ public static TiBlob blobFromImage(Bitmap image)
*/
public static TiBlob blobFromData(byte[] data)
{
return blobFromData(data, "application/octet-stream");
return blobFromData(data, TiMimeTypeHelper.MIME_TYPE_OCTET_STREAM);
}

/**
Expand All @@ -178,7 +178,7 @@ public static TiBlob blobFromData(byte[] data)
public static TiBlob blobFromData(byte[] data, String mimetype)
{
if (mimetype == null || mimetype.length() == 0) {
return new TiBlob(TYPE_DATA, data, "application/octet-stream");
return new TiBlob(TYPE_DATA, data, TiMimeTypeHelper.MIME_TYPE_OCTET_STREAM);
}
TiBlob blob = new TiBlob(TYPE_DATA, data, mimetype);
blob.loadBitmapInfo();
Expand Down Expand Up @@ -414,17 +414,9 @@ public String getText()
result = (String) data;
case TYPE_DATA:
case TYPE_FILE:
// Don't try to return a string if we can see the
// mimetype is binary, unless it's application/octet-stream, which means
// we don't really know what it is, so assume the user-developer knows
// what she's doing.
if (mimetype != null && TiMimeTypeHelper.isBinaryMimeType(mimetype)
&& !mimetype.equals("application/octet-stream")) {
return null;
}
try {
result = new String(getBytes(), "utf-8");
} catch (UnsupportedEncodingException e) {
} catch (Exception ex) {
Log.w(TAG, "Unable to convert to string.");
}
break;
Expand Down Expand Up @@ -504,13 +496,27 @@ public int getHeight()
@Kroll.method
public String toString()
{
// blob should return the text value on toString
// if it's not null
String text = getText();
if (text != null) {
return text;
// Determine if this blob references binary data instead of text.
// Note: If mime-type is "application/octet-stream", then type is unknown. Assume it is text.
boolean isBinary = false;
if ((this.mimetype != null) && TiMimeTypeHelper.isBinaryMimeType(this.mimetype)) {
if (!this.mimetype.equals(TiMimeTypeHelper.MIME_TYPE_OCTET_STREAM)) {
isBinary = true;
}
}
return "[object TiBlob]";

// Attempt to fetch text from the blob, but only if it's NOT a known binary type.
// Ex: Don't let Javascript toString() waste huge amounts of memory on a blob that wraps an image file.
String text = null;
if (!isBinary) {
text = getText();
}
if (text == null) {
text = "[object TiBlob]";
}

// Return the blob's text.
return text;
}

// clang-format off
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
public class TiMimeTypeHelper
{
private static final String DEFAULT_MIME_TYPE = "application/octet-stream";
public static final String MIME_TYPE_OCTET_STREAM = DEFAULT_MIME_TYPE;
public static final String MIME_TYPE_JAVASCRIPT = "text/javascript";
public static final String MIME_TYPE_HTML = "text/html";
public static final HashMap<String, String> EXTRA_MIMETYPES = new HashMap<String, String>();
Expand Down Expand Up @@ -248,19 +249,27 @@ public static String getFileExtensionFromMimeType(String mimeType, String defaul

public static boolean isBinaryMimeType(String mimeType)
{
if (mimeType != null) {
boolean isBinary = false;
if ((mimeType != null) && !mimeType.isEmpty()) {
String parts[] = mimeType.split(";");
mimeType = parts[0];

if (mimeType.startsWith("application/") && !mimeType.endsWith("xml")) {
return true;
} else if (mimeType.startsWith("image/") && !mimeType.endsWith("xml")) {
return true;
} else if (mimeType.startsWith("audio/") || mimeType.startsWith("video/")) {
return true;
} else
return false;
if (mimeType.startsWith("application/")) {
if (!mimeType.endsWith("xml") && !mimeType.endsWith("json")) {
isBinary = true;
}
} else if (mimeType.startsWith("image/")) {
if (!mimeType.endsWith("xml")) {
isBinary = true;
}
} else if (mimeType.startsWith("audio/")) {
isBinary = true;
} else if (mimeType.startsWith("font/")) {
isBinary = true;
} else if (mimeType.startsWith("video/")) {
isBinary = true;
}
}
return false;
return isBinary;
}
}

0 comments on commit 3f49d79

Please sign in to comment.