com.arextest.schedule.comparer.EncodingUtils Maven / Gradle / Ivy
package com.arextest.schedule.comparer;
import java.util.Base64;
/**
* Created by qzmo on 2023-11-27
*
* Extracted from DefaultReplayResultComparer
*/
public class EncodingUtils {
public static String tryBase64Decode(String encoded) {
try {
if (encoded == null) {
return null;
}
if (isJson(encoded)) {
return encoded;
}
String decoded = new String(Base64.getDecoder().decode(trimBase64Str(encoded)));
if (isJson(decoded)) {
return decoded;
}
return encoded;
} catch (Exception e) {
return encoded;
}
}
private static boolean isJson(String value) {
if (value.startsWith("{") && value.endsWith("}")) {
return true;
} else {
return value.startsWith("[") && value.endsWith("]");
}
}
private static String trimBase64Str(String in) {
return in.replace("\"", "");
}
}