|
@@ -0,0 +1,569 @@
|
|
|
|
+package cn.nosum.common.util;
|
|
|
|
+
|
|
|
|
+import java.math.BigDecimal;
|
|
|
|
+import java.math.BigInteger;
|
|
|
|
+import java.nio.ByteBuffer;
|
|
|
|
+import java.nio.charset.Charset;
|
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+ * 类型转换器
|
|
|
|
+ */
|
|
|
|
+public class Convert {
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 转换为字符串<br>
|
|
|
|
+ * 如果给定的值为null,或者转换失败,返回默认值<br>
|
|
|
|
+ * 转换失败不会报错
|
|
|
|
+ *
|
|
|
|
+ * @param value 被转换的值
|
|
|
|
+ * @param defaultValue 转换错误时的默认值
|
|
|
|
+ * @return 结果
|
|
|
|
+ */
|
|
|
|
+ public static String toStr(Object value, String defaultValue) {
|
|
|
|
+ if (null == value) {
|
|
|
|
+ return defaultValue;
|
|
|
|
+ }
|
|
|
|
+ if (value instanceof String) {
|
|
|
|
+ return (String) value;
|
|
|
|
+ }
|
|
|
|
+ return value.toString();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 转换为字符串<br>
|
|
|
|
+ * 如果给定的值为<code>null</code>,或者转换失败,返回默认值<code>null</code><br>
|
|
|
|
+ * 转换失败不会报错
|
|
|
|
+ *
|
|
|
|
+ * @param value 被转换的值
|
|
|
|
+ * @return 结果
|
|
|
|
+ */
|
|
|
|
+ public static String toStr(Object value) {
|
|
|
|
+ return toStr(value, null);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 转换为byte<br>
|
|
|
|
+ * 如果给定的值为<code>null</code>,或者转换失败,返回默认值<br>
|
|
|
|
+ * 转换失败不会报错
|
|
|
|
+ *
|
|
|
|
+ * @param value 被转换的值
|
|
|
|
+ * @param defaultValue 转换错误时的默认值
|
|
|
|
+ * @return 结果
|
|
|
|
+ */
|
|
|
|
+ public static Byte toByte(Object value, Byte defaultValue) {
|
|
|
|
+ if (value == null) {
|
|
|
|
+ return defaultValue;
|
|
|
|
+ }
|
|
|
|
+ if (value instanceof Byte) {
|
|
|
|
+ return (Byte) value;
|
|
|
|
+ }
|
|
|
|
+ if (value instanceof Number) {
|
|
|
|
+ return ((Number) value).byteValue();
|
|
|
|
+ }
|
|
|
|
+ final String valueStr = toStr(value, null);
|
|
|
|
+ if (StringUtils.isEmpty(valueStr)) {
|
|
|
|
+ return defaultValue;
|
|
|
|
+ }
|
|
|
|
+ try {
|
|
|
|
+ return Byte.parseByte(valueStr);
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ return defaultValue;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 转换为byte<br>
|
|
|
|
+ * 如果给定的值为<code>null</code>,或者转换失败,返回默认值<code>null</code><br>
|
|
|
|
+ * 转换失败不会报错
|
|
|
|
+ *
|
|
|
|
+ * @param value 被转换的值
|
|
|
|
+ * @return 结果
|
|
|
|
+ */
|
|
|
|
+ public static Byte toByte(Object value) {
|
|
|
|
+ return toByte(value, null);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 转换为Short<br>
|
|
|
|
+ * 如果给定的值为<code>null</code>,或者转换失败,返回默认值<br>
|
|
|
|
+ * 转换失败不会报错
|
|
|
|
+ *
|
|
|
|
+ * @param value 被转换的值
|
|
|
|
+ * @param defaultValue 转换错误时的默认值
|
|
|
|
+ * @return 结果
|
|
|
|
+ */
|
|
|
|
+ public static Short toShort(Object value, Short defaultValue) {
|
|
|
|
+ if (value == null) {
|
|
|
|
+ return defaultValue;
|
|
|
|
+ }
|
|
|
|
+ if (value instanceof Short) {
|
|
|
|
+ return (Short) value;
|
|
|
|
+ }
|
|
|
|
+ if (value instanceof Number) {
|
|
|
|
+ return ((Number) value).shortValue();
|
|
|
|
+ }
|
|
|
|
+ final String valueStr = toStr(value, null);
|
|
|
|
+ if (StringUtils.isEmpty(valueStr)) {
|
|
|
|
+ return defaultValue;
|
|
|
|
+ }
|
|
|
|
+ try {
|
|
|
|
+ return Short.parseShort(valueStr.trim());
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ return defaultValue;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 转换为Short<br>
|
|
|
|
+ * 如果给定的值为<code>null</code>,或者转换失败,返回默认值<code>null</code><br>
|
|
|
|
+ * 转换失败不会报错
|
|
|
|
+ *
|
|
|
|
+ * @param value 被转换的值
|
|
|
|
+ * @return 结果
|
|
|
|
+ */
|
|
|
|
+ public static Short toShort(Object value) {
|
|
|
|
+ return toShort(value, null);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 转换为int<br>
|
|
|
|
+ * 如果给定的值为空,或者转换失败,返回默认值<br>
|
|
|
|
+ * 转换失败不会报错
|
|
|
|
+ *
|
|
|
|
+ * @param value 被转换的值
|
|
|
|
+ * @param defaultValue 转换错误时的默认值
|
|
|
|
+ * @return 结果
|
|
|
|
+ */
|
|
|
|
+ public static Integer toInt(Object value, Integer defaultValue) {
|
|
|
|
+ if (value == null) {
|
|
|
|
+ return defaultValue;
|
|
|
|
+ }
|
|
|
|
+ if (value instanceof Integer) {
|
|
|
|
+ return (Integer) value;
|
|
|
|
+ }
|
|
|
|
+ if (value instanceof Number) {
|
|
|
|
+ return ((Number) value).intValue();
|
|
|
|
+ }
|
|
|
|
+ final String valueStr = toStr(value, null);
|
|
|
|
+ if (StringUtils.isEmpty(valueStr)) {
|
|
|
|
+ return defaultValue;
|
|
|
|
+ }
|
|
|
|
+ try {
|
|
|
|
+ return Integer.parseInt(valueStr.trim());
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ return defaultValue;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 转换为int<br>
|
|
|
|
+ * 如果给定的值为<code>null</code>,或者转换失败,返回默认值<code>null</code><br>
|
|
|
|
+ * 转换失败不会报错
|
|
|
|
+ *
|
|
|
|
+ * @param value 被转换的值
|
|
|
|
+ * @return 结果
|
|
|
|
+ */
|
|
|
|
+ public static Integer toInt(Object value) {
|
|
|
|
+ return toInt(value, null);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 转换为Integer数组<br>
|
|
|
|
+ *
|
|
|
|
+ * @param str 被转换的值
|
|
|
|
+ * @return 结果
|
|
|
|
+ */
|
|
|
|
+ public static Integer[] toIntArray(String str) {
|
|
|
|
+ return toIntArray(",", str);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 转换为Long数组<br>
|
|
|
|
+ *
|
|
|
|
+ * @param str 被转换的值
|
|
|
|
+ * @return 结果
|
|
|
|
+ */
|
|
|
|
+ public static Long[] toLongArray(String str) {
|
|
|
|
+ return toLongArray(",", str);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 转换为Integer数组<br>
|
|
|
|
+ *
|
|
|
|
+ * @param split 分隔符
|
|
|
|
+ * @param split 被转换的值
|
|
|
|
+ * @return 结果
|
|
|
|
+ */
|
|
|
|
+ public static Integer[] toIntArray(String split, String str) {
|
|
|
|
+ if (StringUtils.isEmpty(str)) {
|
|
|
|
+ return new Integer[]{};
|
|
|
|
+ }
|
|
|
|
+ String[] arr = str.split(split);
|
|
|
|
+ final Integer[] ints = new Integer[arr.length];
|
|
|
|
+ for (int i = 0; i < arr.length; i++) {
|
|
|
|
+ final Integer v = toInt(arr[i], 0);
|
|
|
|
+ ints[i] = v;
|
|
|
|
+ }
|
|
|
|
+ return ints;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 转换为Long数组<br>
|
|
|
|
+ *
|
|
|
|
+ * @param split 分隔符
|
|
|
|
+ * @param str 被转换的值
|
|
|
|
+ * @return 结果
|
|
|
|
+ */
|
|
|
|
+ public static Long[] toLongArray(String split, String str) {
|
|
|
|
+ if (StringUtils.isEmpty(str)) {
|
|
|
|
+ return new Long[]{};
|
|
|
|
+ }
|
|
|
|
+ String[] arr = str.split(split);
|
|
|
|
+ final Long[] longs = new Long[arr.length];
|
|
|
|
+ for (int i = 0; i < arr.length; i++) {
|
|
|
|
+ final Long v = toLong(arr[i], null);
|
|
|
|
+ longs[i] = v;
|
|
|
|
+ }
|
|
|
|
+ return longs;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 转换为String数组<br>
|
|
|
|
+ *
|
|
|
|
+ * @param str 被转换的值
|
|
|
|
+ * @return 结果
|
|
|
|
+ */
|
|
|
|
+ public static String[] toStrArray(String str) {
|
|
|
|
+ return toStrArray(",", str);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 转换为String数组<br>
|
|
|
|
+ *
|
|
|
|
+ * @param split 分隔符
|
|
|
|
+ * @param split 被转换的值
|
|
|
|
+ * @return 结果
|
|
|
|
+ */
|
|
|
|
+ public static String[] toStrArray(String split, String str) {
|
|
|
|
+ return str.split(split);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 转换为long<br>
|
|
|
|
+ * 如果给定的值为空,或者转换失败,返回默认值<br>
|
|
|
|
+ * 转换失败不会报错
|
|
|
|
+ *
|
|
|
|
+ * @param value 被转换的值
|
|
|
|
+ * @param defaultValue 转换错误时的默认值
|
|
|
|
+ * @return 结果
|
|
|
|
+ */
|
|
|
|
+ public static Long toLong(Object value, Long defaultValue) {
|
|
|
|
+ if (value == null) {
|
|
|
|
+ return defaultValue;
|
|
|
|
+ }
|
|
|
|
+ if (value instanceof Long) {
|
|
|
|
+ return (Long) value;
|
|
|
|
+ }
|
|
|
|
+ if (value instanceof Number) {
|
|
|
|
+ return ((Number) value).longValue();
|
|
|
|
+ }
|
|
|
|
+ final String valueStr = toStr(value, null);
|
|
|
|
+ if (StringUtils.isEmpty(valueStr)) {
|
|
|
|
+ return defaultValue;
|
|
|
|
+ }
|
|
|
|
+ try {
|
|
|
|
+ // 支持科学计数法
|
|
|
|
+ return new BigDecimal(valueStr.trim()).longValue();
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ return defaultValue;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 转换为long<br>
|
|
|
|
+ * 如果给定的值为<code>null</code>,或者转换失败,返回默认值<code>null</code><br>
|
|
|
|
+ * 转换失败不会报错
|
|
|
|
+ *
|
|
|
|
+ * @param value 被转换的值
|
|
|
|
+ * @return 结果
|
|
|
|
+ */
|
|
|
|
+ public static Long toLong(Object value) {
|
|
|
|
+ return toLong(value, null);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 转换为double<br>
|
|
|
|
+ * 如果给定的值为空,或者转换失败,返回默认值<br>
|
|
|
|
+ * 转换失败不会报错
|
|
|
|
+ *
|
|
|
|
+ * @param value 被转换的值
|
|
|
|
+ * @param defaultValue 转换错误时的默认值
|
|
|
|
+ * @return 结果
|
|
|
|
+ */
|
|
|
|
+ public static Double toDouble(Object value, Double defaultValue) {
|
|
|
|
+ if (value == null) {
|
|
|
|
+ return defaultValue;
|
|
|
|
+ }
|
|
|
|
+ if (value instanceof Double) {
|
|
|
|
+ return (Double) value;
|
|
|
|
+ }
|
|
|
|
+ if (value instanceof Number) {
|
|
|
|
+ return ((Number) value).doubleValue();
|
|
|
|
+ }
|
|
|
|
+ final String valueStr = toStr(value, null);
|
|
|
|
+ if (StringUtils.isEmpty(valueStr)) {
|
|
|
|
+ return defaultValue;
|
|
|
|
+ }
|
|
|
|
+ try {
|
|
|
|
+ // 支持科学计数法
|
|
|
|
+ return new BigDecimal(valueStr.trim()).doubleValue();
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ return defaultValue;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 转换为double<br>
|
|
|
|
+ * 如果给定的值为空,或者转换失败,返回默认值<code>null</code><br>
|
|
|
|
+ * 转换失败不会报错
|
|
|
|
+ *
|
|
|
|
+ * @param value 被转换的值
|
|
|
|
+ * @return 结果
|
|
|
|
+ */
|
|
|
|
+ public static Double toDouble(Object value) {
|
|
|
|
+ return toDouble(value, null);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 转换为Float<br>
|
|
|
|
+ * 如果给定的值为空,或者转换失败,返回默认值<br>
|
|
|
|
+ * 转换失败不会报错
|
|
|
|
+ *
|
|
|
|
+ * @param value 被转换的值
|
|
|
|
+ * @param defaultValue 转换错误时的默认值
|
|
|
|
+ * @return 结果
|
|
|
|
+ */
|
|
|
|
+ public static Float toFloat(Object value, Float defaultValue) {
|
|
|
|
+ if (value == null) {
|
|
|
|
+ return defaultValue;
|
|
|
|
+ }
|
|
|
|
+ if (value instanceof Float) {
|
|
|
|
+ return (Float) value;
|
|
|
|
+ }
|
|
|
|
+ if (value instanceof Number) {
|
|
|
|
+ return ((Number) value).floatValue();
|
|
|
|
+ }
|
|
|
|
+ final String valueStr = toStr(value, null);
|
|
|
|
+ if (StringUtils.isEmpty(valueStr)) {
|
|
|
|
+ return defaultValue;
|
|
|
|
+ }
|
|
|
|
+ try {
|
|
|
|
+ return Float.parseFloat(valueStr.trim());
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ return defaultValue;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 转换为Float<br>
|
|
|
|
+ * 如果给定的值为空,或者转换失败,返回默认值<code>null</code><br>
|
|
|
|
+ * 转换失败不会报错
|
|
|
|
+ *
|
|
|
|
+ * @param value 被转换的值
|
|
|
|
+ * @return 结果
|
|
|
|
+ */
|
|
|
|
+ public static Float toFloat(Object value) {
|
|
|
|
+ return toFloat(value, null);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 转换为boolean<br>
|
|
|
|
+ * String支持的值为:true、false、yes、ok、no,1,0 如果给定的值为空,或者转换失败,返回默认值<br>
|
|
|
|
+ * 转换失败不会报错
|
|
|
|
+ *
|
|
|
|
+ * @param value 被转换的值
|
|
|
|
+ * @param defaultValue 转换错误时的默认值
|
|
|
|
+ * @return 结果
|
|
|
|
+ */
|
|
|
|
+ public static Boolean toBoolean(Object value, Boolean defaultValue) {
|
|
|
|
+ if (value == null) {
|
|
|
|
+ return defaultValue;
|
|
|
|
+ }
|
|
|
|
+ if (value instanceof Boolean) {
|
|
|
|
+ return (Boolean) value;
|
|
|
|
+ }
|
|
|
|
+ String valueStr = toStr(value, null);
|
|
|
|
+ if (StringUtils.isEmpty(valueStr)) {
|
|
|
|
+ return defaultValue;
|
|
|
|
+ }
|
|
|
|
+ valueStr = valueStr.trim().toLowerCase();
|
|
|
|
+ switch (valueStr) {
|
|
|
|
+ case "true":
|
|
|
|
+ case "yes":
|
|
|
|
+ case "ok":
|
|
|
|
+ case "1":
|
|
|
|
+ return true;
|
|
|
|
+ case "false":
|
|
|
|
+ case "no":
|
|
|
|
+ case "0":
|
|
|
|
+ return false;
|
|
|
|
+ default:
|
|
|
|
+ return defaultValue;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 转换为boolean<br>
|
|
|
|
+ * 如果给定的值为空,或者转换失败,返回默认值<code>null</code><br>
|
|
|
|
+ * 转换失败不会报错
|
|
|
|
+ *
|
|
|
|
+ * @param value 被转换的值
|
|
|
|
+ * @return 结果
|
|
|
|
+ */
|
|
|
|
+ public static Boolean toBoolean(Object value) {
|
|
|
|
+ return toBoolean(value, false);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 转换为BigInteger<br>
|
|
|
|
+ * 如果给定的值为空,或者转换失败,返回默认值<br>
|
|
|
|
+ * 转换失败不会报错
|
|
|
|
+ *
|
|
|
|
+ * @param value 被转换的值
|
|
|
|
+ * @param defaultValue 转换错误时的默认值
|
|
|
|
+ * @return 结果
|
|
|
|
+ */
|
|
|
|
+ public static BigInteger toBigInteger(Object value, BigInteger defaultValue) {
|
|
|
|
+ if (value == null) {
|
|
|
|
+ return defaultValue;
|
|
|
|
+ }
|
|
|
|
+ if (value instanceof BigInteger) {
|
|
|
|
+ return (BigInteger) value;
|
|
|
|
+ }
|
|
|
|
+ if (value instanceof Long) {
|
|
|
|
+ return BigInteger.valueOf((Long) value);
|
|
|
|
+ }
|
|
|
|
+ final String valueStr = toStr(value, null);
|
|
|
|
+ if (StringUtils.isEmpty(valueStr)) {
|
|
|
|
+ return defaultValue;
|
|
|
|
+ }
|
|
|
|
+ try {
|
|
|
|
+ return new BigInteger(valueStr);
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ return defaultValue;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 转换为BigInteger<br>
|
|
|
|
+ * 如果给定的值为空,或者转换失败,返回默认值<code>null</code><br>
|
|
|
|
+ * 转换失败不会报错
|
|
|
|
+ *
|
|
|
|
+ * @param value 被转换的值
|
|
|
|
+ * @return 结果
|
|
|
|
+ */
|
|
|
|
+ public static BigInteger toBigInteger(Object value) {
|
|
|
|
+ return toBigInteger(value, null);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 转换为BigDecimal<br>
|
|
|
|
+ * 如果给定的值为空,或者转换失败,返回默认值<br>
|
|
|
|
+ * 转换失败不会报错
|
|
|
|
+ *
|
|
|
|
+ * @param value 被转换的值
|
|
|
|
+ * @param defaultValue 转换错误时的默认值
|
|
|
|
+ * @return 结果
|
|
|
|
+ */
|
|
|
|
+ public static BigDecimal toBigDecimal(Object value, BigDecimal defaultValue) {
|
|
|
|
+ if (value == null) {
|
|
|
|
+ return defaultValue;
|
|
|
|
+ }
|
|
|
|
+ if (value instanceof BigDecimal) {
|
|
|
|
+ return (BigDecimal) value;
|
|
|
|
+ }
|
|
|
|
+ if (value instanceof Long) {
|
|
|
|
+ return new BigDecimal((Long) value);
|
|
|
|
+ }
|
|
|
|
+ if (value instanceof Double) {
|
|
|
|
+ return new BigDecimal((Double) value);
|
|
|
|
+ }
|
|
|
|
+ if (value instanceof Integer) {
|
|
|
|
+ return new BigDecimal((Integer) value);
|
|
|
|
+ }
|
|
|
|
+ final String valueStr = toStr(value, null);
|
|
|
|
+ if (StringUtils.isEmpty(valueStr)) {
|
|
|
|
+ return defaultValue;
|
|
|
|
+ }
|
|
|
|
+ try {
|
|
|
|
+ return new BigDecimal(valueStr);
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ return defaultValue;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 转换为BigDecimal<br>
|
|
|
|
+ * 如果给定的值为空,或者转换失败,返回默认值<br>
|
|
|
|
+ * 转换失败不会报错
|
|
|
|
+ *
|
|
|
|
+ * @param value 被转换的值
|
|
|
|
+ * @return 结果
|
|
|
|
+ */
|
|
|
|
+ public static BigDecimal toBigDecimal(Object value) {
|
|
|
|
+ return toBigDecimal(value, null);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 将对象转为字符串<br>
|
|
|
|
+ * 1、Byte数组和ByteBuffer会被转换为对应字符串的数组
|
|
|
|
+ * 2、对象数组会调用Arrays.toString方法
|
|
|
|
+ *
|
|
|
|
+ * @param obj 对象
|
|
|
|
+ * @param charsetName 字符集
|
|
|
|
+ * @return 字符串
|
|
|
|
+ */
|
|
|
|
+ public static String objectToStr(Object obj, String charsetName) {
|
|
|
|
+ return objectToStr(obj, Charset.forName(charsetName));
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 将对象转为字符串<br>
|
|
|
|
+ * 1、Byte数组和ByteBuffer会被转换为对应字符串的数组
|
|
|
|
+ * 2、对象数组会调用Arrays.toString方法
|
|
|
|
+ *
|
|
|
|
+ * @param obj 对象
|
|
|
|
+ * @param charset 字符集
|
|
|
|
+ * @return 字符串
|
|
|
|
+ */
|
|
|
|
+ public static String objectToStr(Object obj, Charset charset) {
|
|
|
|
+ if (null == obj) {
|
|
|
|
+ return null;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if (obj instanceof String) {
|
|
|
|
+ return (String) obj;
|
|
|
|
+ } else if (obj instanceof byte[] || obj instanceof Byte[]) {
|
|
|
|
+ return objectToStr(obj, charset);
|
|
|
|
+ } else if (obj instanceof ByteBuffer) {
|
|
|
|
+ return objectToStr(obj, charset);
|
|
|
|
+ }
|
|
|
|
+ return obj.toString();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 将byte数组转为字符串
|
|
|
|
+ *
|
|
|
|
+ * @param bytes byte数组
|
|
|
|
+ * @param charset 字符集
|
|
|
|
+ * @return 字符串
|
|
|
|
+ */
|
|
|
|
+ public static String byteToStr(byte[] bytes, String charset) {
|
|
|
|
+ return objectToStr(bytes, StringUtils.isEmpty(charset) ? Charset.defaultCharset() : Charset.forName(charset));
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+}
|
|
|
|
+
|