From 5e0b1ea92beaff50c3138ba26f2c7a25fb982a4a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E5=8A=B2=E9=B9=8F?= Date: Sat, 6 Jun 2020 17:19:03 +0800 Subject: [PATCH] replace regex with sub string --- .../main/java/pub/doric/shader/ImageNode.java | 38 ++++++++++--------- .../main/java/pub/doric/utils/DoricUtils.java | 19 +++++++++- 2 files changed, 38 insertions(+), 19 deletions(-) diff --git a/doric-android/doric/src/main/java/pub/doric/shader/ImageNode.java b/doric-android/doric/src/main/java/pub/doric/shader/ImageNode.java index 75c317e8..3f611c82 100644 --- a/doric-android/doric/src/main/java/pub/doric/shader/ImageNode.java +++ b/doric-android/doric/src/main/java/pub/doric/shader/ImageNode.java @@ -29,6 +29,7 @@ import android.graphics.drawable.NinePatchDrawable; import android.os.Build; import android.text.TextUtils; import android.util.Base64; +import android.util.Pair; import android.widget.ImageView; import androidx.annotation.NonNull; @@ -49,9 +50,6 @@ import com.github.pengfeizhou.jscore.JSONBuilder; import com.github.pengfeizhou.jscore.JSObject; import com.github.pengfeizhou.jscore.JSValue; -import java.util.regex.Matcher; -import java.util.regex.Pattern; - import jp.wasabeef.glide.transformations.BlurTransformation; import pub.doric.DoricContext; import pub.doric.extension.bridge.DoricPlugin; @@ -163,11 +161,11 @@ public class ImageNode extends ViewNode { return new ColorDrawable(Color.GRAY); } } else if (!TextUtils.isEmpty(placeHolderImageBase64)) { - Pattern r = Pattern.compile("data:image/(\\S+?);base64,(\\S+)"); - Matcher m = r.matcher(placeHolderImageBase64); - if (m.find()) { - String imageType = m.group(1); - String base64 = m.group(2); + Pair result = DoricUtils.translateBase64(placeHolderImageBase64); + if (result != null) { + String imageType = result.first; + String base64 = result.second; + if (!TextUtils.isEmpty(imageType) && !TextUtils.isEmpty(base64)) { try { byte[] data = Base64.decode(base64, Base64.DEFAULT); @@ -177,6 +175,7 @@ public class ImageNode extends ViewNode { } } } + DoricLog.e("Cannot find PlaceHolderBase64 Drawable for " + placeHolderImageBase64); return getDoricContext().getDriver().getRegistry().getDefaultPlaceHolderDrawable(); } else if (placeHolderColor != Color.TRANSPARENT) { @@ -199,11 +198,11 @@ public class ImageNode extends ViewNode { return new ColorDrawable(Color.GRAY); } } else if (!TextUtils.isEmpty(errorImageBase64)) { - Pattern r = Pattern.compile("data:image/(\\S+?);base64,(\\S+)"); - Matcher m = r.matcher(errorImageBase64); - if (m.find()) { - String imageType = m.group(1); - String base64 = m.group(2); + Pair result = DoricUtils.translateBase64(errorImageBase64); + if (result != null) { + String imageType = result.first; + String base64 = result.second; + if (!TextUtils.isEmpty(imageType) && !TextUtils.isEmpty(base64)) { try { byte[] data = Base64.decode(base64, Base64.DEFAULT); @@ -213,6 +212,7 @@ public class ImageNode extends ViewNode { } } } + DoricLog.e("Cannot find ErrorBase64 Drawable for " + errorImageBase64); return getDoricContext().getDriver().getRegistry().getDefaultErrorDrawable(); } else if (errorColor != Color.TRANSPARENT) { @@ -372,11 +372,12 @@ public class ImageNode extends ViewNode { if (!prop.isString()) { return; } - Pattern r = Pattern.compile("data:image/(\\S+?);base64,(\\S+)"); - Matcher m = r.matcher(prop.asString().value()); - if (m.find()) { - String imageType = m.group(1); - String base64 = m.group(2); + String input = prop.asString().value(); + Pair result = DoricUtils.translateBase64(input); + if (result != null) { + String imageType = result.first; + String base64 = result.second; + if (!TextUtils.isEmpty(imageType) && !TextUtils.isEmpty(base64)) { try { byte[] data = Base64.decode(base64, Base64.DEFAULT); @@ -386,6 +387,7 @@ public class ImageNode extends ViewNode { } } } + break; case "imagePath": if (!prop.isString()) { diff --git a/doric-android/doric/src/main/java/pub/doric/utils/DoricUtils.java b/doric-android/doric/src/main/java/pub/doric/utils/DoricUtils.java index 8742e4fb..7b0ebaac 100644 --- a/doric-android/doric/src/main/java/pub/doric/utils/DoricUtils.java +++ b/doric-android/doric/src/main/java/pub/doric/utils/DoricUtils.java @@ -20,6 +20,7 @@ import android.content.res.AssetManager; import android.content.res.Resources; import android.graphics.Rect; import android.util.DisplayMetrics; +import android.util.Pair; import android.view.Display; import android.view.WindowManager; @@ -37,7 +38,6 @@ import org.json.JSONObject; import java.io.IOException; import java.io.InputStream; import java.lang.reflect.Array; -import java.lang.reflect.Field; import java.nio.ByteBuffer; import java.nio.ByteOrder; @@ -288,4 +288,21 @@ public class DoricUtils { return byteBuffer.array(); } + + public static Pair translateBase64(String input) { + String typePrefix = "data:image/"; + String dataPrefix = ";base64,"; + + int typeIndex = input.indexOf(typePrefix); + int dataIndex = input.indexOf(dataPrefix); + + if (typeIndex != -1 && dataIndex != -1) { + String imageType = input.substring(typePrefix.length() + 1, dataIndex); + String base64 = input.substring(dataIndex + dataPrefix.length()); + + return new Pair<>(imageType, base64); + } + + return null; + } }