diff --git a/CHANGELOG.md b/CHANGELOG.md index 30aa85d8b..a7f31e628 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,9 +3,10 @@ ------------------------------------------------------------------------------------------------------------- -# 5.6.3 (2021-03-28) +# 5.6.3 (2021-03-30) ### 新特性 +* 【core 】 修改数字转换的实现,增加按照指定端序转换(pr#1492@Github) ### Bug修复 ------------------------------------------------------------------------------------------------------------- diff --git a/hutool-core/src/main/java/cn/hutool/core/convert/Convert.java b/hutool-core/src/main/java/cn/hutool/core/convert/Convert.java index 031d62091..50a2602c7 100644 --- a/hutool-core/src/main/java/cn/hutool/core/convert/Convert.java +++ b/hutool-core/src/main/java/cn/hutool/core/convert/Convert.java @@ -6,6 +6,7 @@ import cn.hutool.core.convert.impl.MapConverter; import cn.hutool.core.lang.Assert; import cn.hutool.core.lang.TypeReference; import cn.hutool.core.text.UnicodeUtil; +import cn.hutool.core.util.ByteUtil; import cn.hutool.core.util.CharsetUtil; import cn.hutool.core.util.ClassUtil; import cn.hutool.core.util.HexUtil; @@ -14,11 +15,16 @@ import cn.hutool.core.util.StrUtil; import java.lang.reflect.Type; import java.math.BigDecimal; import java.math.BigInteger; -import java.nio.ByteOrder; import java.nio.charset.Charset; import java.time.Instant; import java.time.LocalDateTime; -import java.util.*; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Date; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Set; import java.util.concurrent.TimeUnit; /** @@ -1047,24 +1053,7 @@ public class Convert { * @since 5.6.3 */ public static short bytesToShort(byte[] bytes) { - return bytesToShort(bytes, ByteOrder.LITTLE_ENDIAN); - } - - /** - * byte数组转short
- * 自定义端序 - * - * @param bytes byte数组 - * @param byteOrder 端序 - * @return short值 - * @since 5.6.3 - */ - public static short bytesToShort(byte[] bytes, ByteOrder byteOrder) { - if (ByteOrder.LITTLE_ENDIAN == byteOrder) { - return (short) (bytes[1] & 0xff | (bytes[0] & 0xff) << Byte.SIZE); - } else { - return (short) (bytes[0] & 0xff | (bytes[1] & 0xff) << Byte.SIZE); - } + return ByteUtil.bytesToShort(bytes); } /** @@ -1076,28 +1065,7 @@ public class Convert { * @since 5.6.3 */ public static byte[] shortToBytes(short shortValue) { - return shortToBytes(shortValue, ByteOrder.LITTLE_ENDIAN); - } - - /** - * short转byte数组
- * 自定义端序 - * - * @param shortValue short值 - * @param byteOrder 端序 - * @return byte数组 - * @since 5.6.3 - */ - public static byte[] shortToBytes(short shortValue, ByteOrder byteOrder) { - byte[] b = new byte[Short.BYTES]; - if (ByteOrder.LITTLE_ENDIAN == byteOrder) { - b[1] = (byte) (shortValue & 0xff); - b[0] = (byte) ((shortValue >> Byte.SIZE) & 0xff); - } else { - b[0] = (byte) (shortValue & 0xff); - b[1] = (byte) ((shortValue >> Byte.SIZE) & 0xff); - } - return b; + return ByteUtil.shortToBytes(shortValue); } /** @@ -1109,31 +1077,7 @@ public class Convert { * @since 5.6.3 */ public static int bytesToInt(byte[] bytes) { - return bytesToInt(bytes, ByteOrder.LITTLE_ENDIAN); - } - - /** - * byte[]转int值
- * 自定义端序 - * - * @param bytes byte数组 - * @param byteOrder 端序 - * @return int值 - * @since 5.6.3 - */ - public static int bytesToInt(byte[] bytes, ByteOrder byteOrder) { - if (ByteOrder.LITTLE_ENDIAN == byteOrder) { - return bytes[3] & 0xFF | // - (bytes[2] & 0xFF) << 8 | // - (bytes[1] & 0xFF) << 16 | // - (bytes[0] & 0xFF) << 24; // - } else { - return bytes[0] & 0xFF | // - (bytes[1] & 0xFF) << 8 | // - (bytes[2] & 0xFF) << 16 | // - (bytes[3] & 0xFF) << 24; // - } - + return ByteUtil.bytesToInt(bytes); } /** @@ -1145,38 +1089,7 @@ public class Convert { * @since 5.6.3 */ public static byte[] intToBytes(int intValue) { - return intToBytes(intValue, ByteOrder.LITTLE_ENDIAN); - } - - /** - * int转byte数组
- * 自定义端序 - * - * @param intValue int值 - * @param byteOrder 端序 - * @return byte数组 - * @since 5.6.3 - */ - public static byte[] intToBytes(int intValue, ByteOrder byteOrder) { - - if (ByteOrder.LITTLE_ENDIAN == byteOrder) { - return new byte[]{ // - (byte) ((intValue >> 24) & 0xFF), // - (byte) ((intValue >> 16) & 0xFF), // - (byte) ((intValue >> 8) & 0xFF), // - (byte) (intValue & 0xFF) // - }; - - } else { - return new byte[]{ // - (byte) (intValue & 0xFF), // - (byte) ((intValue >> 8) & 0xFF), // - (byte) ((intValue >> 16) & 0xFF), // - (byte) ((intValue >> 24) & 0xFF) // - }; - } - - + return ByteUtil.intToBytes(intValue); } /** @@ -1189,34 +1102,7 @@ public class Convert { * @since 5.6.3 */ public static byte[] longToBytes(long longValue) { - return longToBytes(longValue, ByteOrder.LITTLE_ENDIAN); - } - - /** - * long转byte数组
- * 自定义端序
- * from: https://stackoverflow.com/questions/4485128/how-do-i-convert-long-to-byte-and-back-in-java - * - * @param longValue long值 - * @param byteOrder 端序 - * @return byte数组 - * @since 5.6.3 - */ - public static byte[] longToBytes(long longValue, ByteOrder byteOrder) { - byte[] result = new byte[Long.BYTES]; - if (ByteOrder.LITTLE_ENDIAN == byteOrder) { - for (int i = (result.length - 1); i >= 0; i--) { - result[i] = (byte) (longValue & 0xFF); - longValue >>= Byte.SIZE; - } - } else { - for (int i = 0; i < result.length; i++) { - result[i] = (byte) (longValue & 0xFF); - longValue >>= Byte.SIZE; - } - } - - return result; + return ByteUtil.longToBytes(longValue); } /** @@ -1229,33 +1115,6 @@ public class Convert { * @since 5.6.3 */ public static long bytesToLong(byte[] bytes) { - return bytesToLong(bytes, ByteOrder.LITTLE_ENDIAN); - } - - /** - * byte数组转long
- * 自定义端序
- * from: https://stackoverflow.com/questions/4485128/how-do-i-convert-long-to-byte-and-back-in-java - * - * @param bytes byte数组 - * @param byteOrder 端序 - * @return long值 - * @since 5.6.3 - */ - public static long bytesToLong(byte[] bytes, ByteOrder byteOrder) { - long values = 0; - if (ByteOrder.LITTLE_ENDIAN == byteOrder) { - for (int i = 0; i < Long.BYTES; i++) { - values <<= Byte.SIZE; - values |= (bytes[i] & 0xff); - } - } else { - for (int i = (Long.BYTES - 1); i >= 0; i--) { - values <<= Byte.SIZE; - values |= (bytes[i] & 0xff); - } - } - - return values; + return ByteUtil.bytesToLong(bytes); } } diff --git a/hutool-core/src/main/java/cn/hutool/core/convert/ConverterRegistry.java b/hutool-core/src/main/java/cn/hutool/core/convert/ConverterRegistry.java index 40fb260f8..6800a9213 100644 --- a/hutool-core/src/main/java/cn/hutool/core/convert/ConverterRegistry.java +++ b/hutool-core/src/main/java/cn/hutool/core/convert/ConverterRegistry.java @@ -298,7 +298,7 @@ public class ConverterRegistry implements Serializable { * @param 转换的目标类型(转换器转换到的类型) * @param type 类型 * @param value 值 - * @return 转换后的值,默认为null + * @return 转换后的值,默认为{@code null} * @throws ConvertException 转换器不存在 */ public T convert(Type type, Object value) throws ConvertException { diff --git a/hutool-core/src/main/java/cn/hutool/core/convert/impl/ArrayConverter.java b/hutool-core/src/main/java/cn/hutool/core/convert/impl/ArrayConverter.java index e3fddf479..b0793d50c 100644 --- a/hutool-core/src/main/java/cn/hutool/core/convert/impl/ArrayConverter.java +++ b/hutool-core/src/main/java/cn/hutool/core/convert/impl/ArrayConverter.java @@ -4,6 +4,7 @@ import cn.hutool.core.collection.IterUtil; import cn.hutool.core.convert.AbstractConverter; import cn.hutool.core.convert.Convert; import cn.hutool.core.util.ArrayUtil; +import cn.hutool.core.util.ByteUtil; import cn.hutool.core.util.ObjectUtil; import cn.hutool.core.util.StrUtil; @@ -159,6 +160,9 @@ public class ArrayConverter extends AbstractConverter { for (int i = 0; i < list.size(); i++) { Array.set(result, i, convertComponentType(list.get(i))); } + }else if (value instanceof Number && byte.class == targetComponentType) { + // 用户可能想序列化指定对象 + result = ByteUtil.numberToBytes((Number)value); } else if (value instanceof Serializable && byte.class == targetComponentType) { // 用户可能想序列化指定对象 result = ObjectUtil.serialize(value); diff --git a/hutool-core/src/main/java/cn/hutool/core/convert/impl/NumberConverter.java b/hutool-core/src/main/java/cn/hutool/core/convert/impl/NumberConverter.java index a5e547b8c..25d05fe8c 100644 --- a/hutool-core/src/main/java/cn/hutool/core/convert/impl/NumberConverter.java +++ b/hutool-core/src/main/java/cn/hutool/core/convert/impl/NumberConverter.java @@ -3,6 +3,7 @@ package cn.hutool.core.convert.impl; import cn.hutool.core.convert.AbstractConverter; import cn.hutool.core.date.DateUtil; import cn.hutool.core.util.BooleanUtil; +import cn.hutool.core.util.ByteUtil; import cn.hutool.core.util.NumberUtil; import cn.hutool.core.util.StrUtil; @@ -90,6 +91,8 @@ public class NumberConverter extends AbstractConverter { return ((Number) value).shortValue(); } else if (value instanceof Boolean) { return BooleanUtil.toShortObj((Boolean) value); + } else if (value instanceof byte[]){ + return ByteUtil.bytesToShort((byte[]) value); } final String valueStr = toStrFunc.apply((value)); try{ @@ -108,6 +111,8 @@ public class NumberConverter extends AbstractConverter { return (int) ((Calendar) value).getTimeInMillis(); } else if (value instanceof TemporalAccessor) { return (int) DateUtil.toInstant((TemporalAccessor) value).toEpochMilli(); + } else if (value instanceof byte[]){ + return ByteUtil.bytesToInt((byte[]) value); } final String valueStr = toStrFunc.apply((value)); return StrUtil.isBlank(valueStr) ? null : NumberUtil.parseInt(valueStr); @@ -129,6 +134,8 @@ public class NumberConverter extends AbstractConverter { return ((Calendar) value).getTimeInMillis(); } else if (value instanceof TemporalAccessor) { return DateUtil.toInstant((TemporalAccessor) value).toEpochMilli(); + }else if (value instanceof byte[]){ + return ByteUtil.bytesToLong((byte[]) value); } final String valueStr = toStrFunc.apply((value)); return StrUtil.isBlank(valueStr) ? null : NumberUtil.parseLong(valueStr); @@ -152,6 +159,8 @@ public class NumberConverter extends AbstractConverter { return ((Number) value).floatValue(); } else if (value instanceof Boolean) { return BooleanUtil.toFloatObj((Boolean) value); + } else if (value instanceof byte[]){ + return (float)ByteUtil.bytesToDouble((byte[]) value); } final String valueStr = toStrFunc.apply((value)); return StrUtil.isBlank(valueStr) ? null : NumberUtil.parseFloat(valueStr); @@ -160,6 +169,8 @@ public class NumberConverter extends AbstractConverter { return ((Number) value).doubleValue(); } else if (value instanceof Boolean) { return BooleanUtil.toDoubleObj((Boolean) value); + } else if (value instanceof byte[]){ + return ByteUtil.bytesToDouble((byte[]) value); } final String valueStr = toStrFunc.apply((value)); return StrUtil.isBlank(valueStr) ? null : NumberUtil.parseDouble(valueStr); @@ -202,6 +213,8 @@ public class NumberConverter extends AbstractConverter { return NumberUtil.toBigDecimal((Number) value); } else if (value instanceof Boolean) { return new BigDecimal((boolean) value ? 1 : 0); + } else if (value instanceof byte[]){ + return NumberUtil.toBigDecimal(ByteUtil.bytesToDouble((byte[]) value)); } //对于Double类型,先要转换为String,避免精度问题 @@ -222,6 +235,8 @@ public class NumberConverter extends AbstractConverter { return BigInteger.valueOf((Long) value); } else if (value instanceof Boolean) { return BigInteger.valueOf((boolean) value ? 1 : 0); + } else if (value instanceof byte[]){ + return BigInteger.valueOf(ByteUtil.bytesToLong((byte[]) value)); } return NumberUtil.toBigInteger(toStrFunc.apply(value)); diff --git a/hutool-core/src/main/java/cn/hutool/core/convert/impl/PrimitiveConverter.java b/hutool-core/src/main/java/cn/hutool/core/convert/impl/PrimitiveConverter.java index 53712fd17..36b092b09 100644 --- a/hutool-core/src/main/java/cn/hutool/core/convert/impl/PrimitiveConverter.java +++ b/hutool-core/src/main/java/cn/hutool/core/convert/impl/PrimitiveConverter.java @@ -12,14 +12,14 @@ import java.util.function.Function; * 原始类型转换器
* 支持类型为:
*
    - *
  • byte
  • - *
  • short
  • - *
  • int
  • - *
  • long
  • - *
  • float
  • - *
  • double
  • - *
  • char
  • - *
  • boolean
  • + *
  • {@code byte}
  • + *
  • {@code short}
  • + *
  • {@code int}
  • + *
  • {@code long}
  • + *
  • {@code float}
  • + *
  • {@code double}
  • + *
  • {@code char}
  • + *
  • {@code boolean}
  • *
* * @author Looly diff --git a/hutool-core/src/main/java/cn/hutool/core/util/ByteUtil.java b/hutool-core/src/main/java/cn/hutool/core/util/ByteUtil.java new file mode 100644 index 000000000..78ee8b4f6 --- /dev/null +++ b/hutool-core/src/main/java/cn/hutool/core/util/ByteUtil.java @@ -0,0 +1,330 @@ +package cn.hutool.core.util; + +import java.nio.ByteOrder; + +/** + * 对数字和字节进行转换。
+ * 假设数据存储是以大端模式存储的:
+ *
    + *
  • byte: 字节类型 占8位二进制 00000000
  • + *
  • char: 字符类型 占2个字节 16位二进制 byte[0] byte[1]
  • + *
  • int : 整数类型 占4个字节 32位二进制 byte[0] byte[1] byte[2] byte[3]
  • + *
  • long: 长整数类型 占8个字节 64位二进制 byte[0] byte[1] byte[2] byte[3] byte[4] byte[5]
  • + *
  • long: 长整数类型 占8个字节 64位二进制 byte[0] byte[1] byte[2] byte[3] byte[4] byte[5] byte[6] byte[7]
  • + *
  • float: 浮点数(小数) 占4个字节 32位二进制 byte[0] byte[1] byte[2] byte[3]
  • + *
  • double: 双精度浮点数(小数) 占8个字节 64位二进制 byte[0] byte[1] byte[2] byte[3] byte[4]byte[5] byte[6] byte[7]
  • + *
+ * 注:注释来自Hanlp,代码提供来自pr#1492@Github + * + * @author looly, hanlp, FULaBUla + * @since 5.6.3 + */ +public class ByteUtil { + + /** + * int转byte + * + * @param intValue int值 + * @return byte值 + */ + public static byte intToByte(int intValue) { + return (byte) intValue; + } + + /** + * byte转无符号int + * + * @param byteValue byte值 + * @return 无符号int值 + * @since 3.2.0 + */ + public static int byteToUnsignedInt(byte byteValue) { + // Java 总是把 byte 当做有符处理;我们可以通过将其和 0xFF 进行二进制与得到它的无符值 + return byteValue & 0xFF; + } + + /** + * byte数组转short
+ * 默认以小端序转换 + * + * @param bytes byte数组 + * @return short值 + */ + public static short bytesToShort(byte[] bytes) { + return bytesToShort(bytes, ByteOrder.LITTLE_ENDIAN); + } + + /** + * byte数组转short
+ * 自定义端序 + * + * @param bytes byte数组 + * @param byteOrder 端序 + * @return short值 + */ + public static short bytesToShort(byte[] bytes, ByteOrder byteOrder) { + if (ByteOrder.LITTLE_ENDIAN == byteOrder) { + return (short) (bytes[1] & 0xff | (bytes[0] & 0xff) << Byte.SIZE); + } else { + return (short) (bytes[0] & 0xff | (bytes[1] & 0xff) << Byte.SIZE); + } + } + + /** + * short转byte数组
+ * 默认以小端序转换 + * + * @param shortValue short值 + * @return byte数组 + */ + public static byte[] shortToBytes(short shortValue) { + return shortToBytes(shortValue, ByteOrder.LITTLE_ENDIAN); + } + + /** + * short转byte数组
+ * 自定义端序 + * + * @param shortValue short值 + * @param byteOrder 端序 + * @return byte数组 + */ + public static byte[] shortToBytes(short shortValue, ByteOrder byteOrder) { + byte[] b = new byte[Short.BYTES]; + if (ByteOrder.LITTLE_ENDIAN == byteOrder) { + b[1] = (byte) (shortValue & 0xff); + b[0] = (byte) ((shortValue >> Byte.SIZE) & 0xff); + } else { + b[0] = (byte) (shortValue & 0xff); + b[1] = (byte) ((shortValue >> Byte.SIZE) & 0xff); + } + return b; + } + + /** + * byte[]转int值
+ * 默认以小端序转换 + * + * @param bytes byte数组 + * @return int值 + */ + public static int bytesToInt(byte[] bytes) { + return bytesToInt(bytes, ByteOrder.LITTLE_ENDIAN); + } + + /** + * byte[]转int值
+ * 自定义端序 + * + * @param bytes byte数组 + * @param byteOrder 端序 + * @return int值 + */ + public static int bytesToInt(byte[] bytes, ByteOrder byteOrder) { + if (ByteOrder.LITTLE_ENDIAN == byteOrder) { + return bytes[3] & 0xFF | // + (bytes[2] & 0xFF) << 8 | // + (bytes[1] & 0xFF) << 16 | // + (bytes[0] & 0xFF) << 24; // + } else { + return bytes[0] & 0xFF | // + (bytes[1] & 0xFF) << 8 | // + (bytes[2] & 0xFF) << 16 | // + (bytes[3] & 0xFF) << 24; // + } + + } + + /** + * int转byte数组
+ * 默认以小端序转换 + * + * @param intValue int值 + * @return byte数组 + */ + public static byte[] intToBytes(int intValue) { + return intToBytes(intValue, ByteOrder.LITTLE_ENDIAN); + } + + /** + * int转byte数组
+ * 自定义端序 + * + * @param intValue int值 + * @param byteOrder 端序 + * @return byte数组 + */ + public static byte[] intToBytes(int intValue, ByteOrder byteOrder) { + + if (ByteOrder.LITTLE_ENDIAN == byteOrder) { + return new byte[]{ // + (byte) ((intValue >> 24) & 0xFF), // + (byte) ((intValue >> 16) & 0xFF), // + (byte) ((intValue >> 8) & 0xFF), // + (byte) (intValue & 0xFF) // + }; + + } else { + return new byte[]{ // + (byte) (intValue & 0xFF), // + (byte) ((intValue >> 8) & 0xFF), // + (byte) ((intValue >> 16) & 0xFF), // + (byte) ((intValue >> 24) & 0xFF) // + }; + } + + + } + + /** + * long转byte数组
+ * 默认以小端序转换
+ * from: https://stackoverflow.com/questions/4485128/how-do-i-convert-long-to-byte-and-back-in-java + * + * @param longValue long值 + * @return byte数组 + */ + public static byte[] longToBytes(long longValue) { + return longToBytes(longValue, ByteOrder.LITTLE_ENDIAN); + } + + /** + * long转byte数组
+ * 自定义端序
+ * from: https://stackoverflow.com/questions/4485128/how-do-i-convert-long-to-byte-and-back-in-java + * + * @param longValue long值 + * @param byteOrder 端序 + * @return byte数组 + */ + public static byte[] longToBytes(long longValue, ByteOrder byteOrder) { + byte[] result = new byte[Long.BYTES]; + if (ByteOrder.LITTLE_ENDIAN == byteOrder) { + for (int i = (result.length - 1); i >= 0; i--) { + result[i] = (byte) (longValue & 0xFF); + longValue >>= Byte.SIZE; + } + } else { + for (int i = 0; i < result.length; i++) { + result[i] = (byte) (longValue & 0xFF); + longValue >>= Byte.SIZE; + } + } + + return result; + } + + /** + * byte数组转long
+ * 默认以小端序转换
+ * from: https://stackoverflow.com/questions/4485128/how-do-i-convert-long-to-byte-and-back-in-java + * + * @param bytes byte数组 + * @return long值 + */ + public static long bytesToLong(byte[] bytes) { + return bytesToLong(bytes, ByteOrder.LITTLE_ENDIAN); + } + + /** + * byte数组转long
+ * 自定义端序
+ * from: https://stackoverflow.com/questions/4485128/how-do-i-convert-long-to-byte-and-back-in-java + * + * @param bytes byte数组 + * @param byteOrder 端序 + * @return long值 + */ + public static long bytesToLong(byte[] bytes, ByteOrder byteOrder) { + long values = 0; + if (ByteOrder.LITTLE_ENDIAN == byteOrder) { + for (int i = 0; i < Long.BYTES; i++) { + values <<= Byte.SIZE; + values |= (bytes[i] & 0xff); + } + } else { + for (int i = (Long.BYTES - 1); i >= 0; i--) { + values <<= Byte.SIZE; + values |= (bytes[i] & 0xff); + } + } + + return values; + } + + /** + * double转byte数组
+ * 默认以小端序转换
+ * + * @param doubleValue double值 + * @return byte数组 + */ + public static byte[] doubleToBytes(double doubleValue) { + return doubleToBytes(doubleValue, ByteOrder.LITTLE_ENDIAN); + } + + /** + * double转byte数组
+ * 自定义端序
+ * from: https://stackoverflow.com/questions/4485128/how-do-i-convert-long-to-byte-and-back-in-java + * + * @param doubleValue double值 + * @param byteOrder 端序 + * @return byte数组 + */ + public static byte[] doubleToBytes(double doubleValue, ByteOrder byteOrder) { + return longToBytes(Double.doubleToLongBits(doubleValue), byteOrder); + } + + /** + * byte数组转Double
+ * 默认以小端序转换
+ * + * @param bytes byte数组 + * @return long值 + */ + public static double bytesToDouble(byte[] bytes) { + return bytesToDouble(bytes, ByteOrder.LITTLE_ENDIAN); + } + + /** + * byte数组转double
+ * 自定义端序
+ * + * @param bytes byte数组 + * @param byteOrder 端序 + * @return long值 + */ + public static double bytesToDouble(byte[] bytes, ByteOrder byteOrder) { + return Double.longBitsToDouble(bytesToLong(bytes, byteOrder)); + } + + /** + * 将{@link Number}转换为 + * @param number 数字 + * @return bytes + */ + public static byte[] numberToBytes(Number number){ + return numberToBytes(number, ByteOrder.LITTLE_ENDIAN); + } + + /** + * 将{@link Number}转换为 + * @param number 数字 + * @param byteOrder 端序 + * @return bytes + */ + public static byte[] numberToBytes(Number number, ByteOrder byteOrder){ + if(number instanceof Double){ + return doubleToBytes((Double) number, byteOrder); + } else if(number instanceof Long){ + return longToBytes((Long) number, byteOrder); + } else if(number instanceof Integer){ + return intToBytes((Integer) number, byteOrder); + } else if(number instanceof Short){ + return shortToBytes((Short) number, byteOrder); + } else{ + return doubleToBytes(number.doubleValue(), byteOrder); + } + } +} diff --git a/hutool-core/src/test/java/cn/hutool/core/convert/ConvertTest.java b/hutool-core/src/test/java/cn/hutool/core/convert/ConvertTest.java index e2e0b23fb..0157610e2 100644 --- a/hutool-core/src/test/java/cn/hutool/core/convert/ConvertTest.java +++ b/hutool-core/src/test/java/cn/hutool/core/convert/ConvertTest.java @@ -3,6 +3,7 @@ package cn.hutool.core.convert; import cn.hutool.core.collection.CollUtil; import cn.hutool.core.date.DateException; import cn.hutool.core.lang.TypeReference; +import cn.hutool.core.util.ByteUtil; import lombok.AllArgsConstructor; import lombok.Data; import lombok.Getter; @@ -10,7 +11,6 @@ import org.junit.Assert; import org.junit.Test; import java.io.Serializable; -import java.nio.ByteOrder; import java.util.ArrayList; import java.util.Arrays; import java.util.Date; @@ -174,35 +174,6 @@ public class ConvertTest { Assert.assertEquals(int2, int3); } - @Test - public void intAndBytesLittleEndianTest() { - // 测试 int 转小端序 byte 数组 - int int1 = 1417; - - byte[] bytesInt = Convert.intToBytes(int1, ByteOrder.LITTLE_ENDIAN); - int int2 = Convert.bytesToInt(bytesInt, ByteOrder.LITTLE_ENDIAN); - Assert.assertEquals(int1, int2); - - byte[] bytesInt2 = Convert.intToBytes(int1, ByteOrder.LITTLE_ENDIAN); - int int3 = Convert.bytesToInt(bytesInt2, ByteOrder.LITTLE_ENDIAN); - Assert.assertEquals(int1, int3); - - byte[] bytesInt3 = Convert.intToBytes(int1, ByteOrder.LITTLE_ENDIAN); - int int4 = Convert.bytesToInt(bytesInt3, ByteOrder.LITTLE_ENDIAN); - Assert.assertEquals(int1, int4); - } - - @Test - public void intAndBytesBigEndianTest() { - // 测试 int 转大端序 byte 数组 - int int2 = 1417; - byte[] bytesInt = Convert.intToBytes(int2, ByteOrder.BIG_ENDIAN); - - // 测试大端序 byte 数组转 int - int int3 = Convert.bytesToInt(bytesInt, ByteOrder.BIG_ENDIAN); - Assert.assertEquals(int2, int3); - } - @Test public void longAndBytesTest() { // 测试 long 转 byte 数组 @@ -214,35 +185,6 @@ public class ConvertTest { Assert.assertEquals(long1, long2); } - @Test - public void longAndBytesLittleEndianTest() { - // 测试 long 转 byte 数组 - long long1 = 2223; - - byte[] bytesLong = Convert.longToBytes(long1, ByteOrder.LITTLE_ENDIAN); - long long2 = Convert.bytesToLong(bytesLong, ByteOrder.LITTLE_ENDIAN); - Assert.assertEquals(long1, long2); - - byte[] bytesLong2 = Convert.longToBytes(long1); - long long3 = Convert.bytesToLong(bytesLong2, ByteOrder.LITTLE_ENDIAN); - Assert.assertEquals(long1, long3); - - byte[] bytesLong3 = Convert.longToBytes(long1, ByteOrder.LITTLE_ENDIAN); - long long4 = Convert.bytesToLong(bytesLong3); - Assert.assertEquals(long1, long4); - } - - @Test - public void longAndBytesBigEndianTest() { - // 测试大端序 long 转 byte 数组 - long long1 = 2223; - - byte[] bytesLong = Convert.longToBytes(long1, ByteOrder.BIG_ENDIAN); - long long2 = Convert.bytesToLong(bytesLong, ByteOrder.BIG_ENDIAN); - - Assert.assertEquals(long1, long2); - } - @Test public void shortAndBytesTest() { short short1 = 122; @@ -252,32 +194,6 @@ public class ConvertTest { Assert.assertEquals(short2, short1); } - @Test - public void shortAndBytesLittleEndianTest() { - short short1 = 122; - - byte[] bytes = Convert.shortToBytes(short1, ByteOrder.LITTLE_ENDIAN); - short short2 = Convert.bytesToShort(bytes, ByteOrder.LITTLE_ENDIAN); - Assert.assertEquals(short2, short1); - - byte[] bytes2 = Convert.shortToBytes(short1); - short short3 = Convert.bytesToShort(bytes2, ByteOrder.LITTLE_ENDIAN); - Assert.assertEquals(short3, short1); - - byte[] bytes3 = Convert.shortToBytes(short1, ByteOrder.LITTLE_ENDIAN); - short short4 = Convert.bytesToShort(bytes3); - Assert.assertEquals(short4, short1); - } - - @Test - public void shortAndBytesBigEndianTest() { - short short1 = 122; - byte[] bytes = Convert.shortToBytes(short1, ByteOrder.BIG_ENDIAN); - short short2 = Convert.bytesToShort(bytes, ByteOrder.BIG_ENDIAN); - - Assert.assertEquals(short2, short1); - } - @Test public void toListTest() { List list = Arrays.asList("1", "2"); @@ -315,6 +231,13 @@ public class ConvertTest { Assert.assertEquals("5.1.1", product.getVersion()); } + @Test + public void numberToByteArrayTest(){ + // 测试Serializable转换为bytes,调用序列化转换 + final byte[] bytes = Convert.toPrimitiveByteArray(12L); + Assert.assertArrayEquals(ByteUtil.longToBytes(12L), bytes); + } + @Test public void toAtomicIntegerArrayTest(){ String str = "1,2"; diff --git a/hutool-core/src/test/java/cn/hutool/core/util/ByteUtilTest.java b/hutool-core/src/test/java/cn/hutool/core/util/ByteUtilTest.java new file mode 100644 index 000000000..a8bd49721 --- /dev/null +++ b/hutool-core/src/test/java/cn/hutool/core/util/ByteUtilTest.java @@ -0,0 +1,92 @@ +package cn.hutool.core.util; + +import org.junit.Assert; +import org.junit.Test; + +import java.nio.ByteOrder; + +public class ByteUtilTest { + @Test + public void intAndBytesLittleEndianTest() { + // 测试 int 转小端序 byte 数组 + int int1 = 1417; + + byte[] bytesInt = ByteUtil.intToBytes(int1, ByteOrder.LITTLE_ENDIAN); + int int2 = ByteUtil.bytesToInt(bytesInt, ByteOrder.LITTLE_ENDIAN); + Assert.assertEquals(int1, int2); + + byte[] bytesInt2 = ByteUtil.intToBytes(int1, ByteOrder.LITTLE_ENDIAN); + int int3 = ByteUtil.bytesToInt(bytesInt2, ByteOrder.LITTLE_ENDIAN); + Assert.assertEquals(int1, int3); + + byte[] bytesInt3 = ByteUtil.intToBytes(int1, ByteOrder.LITTLE_ENDIAN); + int int4 = ByteUtil.bytesToInt(bytesInt3, ByteOrder.LITTLE_ENDIAN); + Assert.assertEquals(int1, int4); + } + + @Test + public void intAndBytesBigEndianTest() { + // 测试 int 转大端序 byte 数组 + int int2 = 1417; + byte[] bytesInt = ByteUtil.intToBytes(int2, ByteOrder.BIG_ENDIAN); + + // 测试大端序 byte 数组转 int + int int3 = ByteUtil.bytesToInt(bytesInt, ByteOrder.BIG_ENDIAN); + Assert.assertEquals(int2, int3); + } + + @Test + public void longAndBytesLittleEndianTest() { + // 测试 long 转 byte 数组 + long long1 = 2223; + + byte[] bytesLong = ByteUtil.longToBytes(long1, ByteOrder.LITTLE_ENDIAN); + long long2 = ByteUtil.bytesToLong(bytesLong, ByteOrder.LITTLE_ENDIAN); + Assert.assertEquals(long1, long2); + + byte[] bytesLong2 = ByteUtil.longToBytes(long1); + long long3 = ByteUtil.bytesToLong(bytesLong2, ByteOrder.LITTLE_ENDIAN); + Assert.assertEquals(long1, long3); + + byte[] bytesLong3 = ByteUtil.longToBytes(long1, ByteOrder.LITTLE_ENDIAN); + long long4 = ByteUtil.bytesToLong(bytesLong3); + Assert.assertEquals(long1, long4); + } + + @Test + public void longAndBytesBigEndianTest() { + // 测试大端序 long 转 byte 数组 + long long1 = 2223; + + byte[] bytesLong = ByteUtil.longToBytes(long1, ByteOrder.BIG_ENDIAN); + long long2 = ByteUtil.bytesToLong(bytesLong, ByteOrder.BIG_ENDIAN); + + Assert.assertEquals(long1, long2); + } + + @Test + public void shortAndBytesLittleEndianTest() { + short short1 = 122; + + byte[] bytes = ByteUtil.shortToBytes(short1, ByteOrder.LITTLE_ENDIAN); + short short2 = ByteUtil.bytesToShort(bytes, ByteOrder.LITTLE_ENDIAN); + Assert.assertEquals(short2, short1); + + byte[] bytes2 = ByteUtil.shortToBytes(short1); + short short3 = ByteUtil.bytesToShort(bytes2, ByteOrder.LITTLE_ENDIAN); + Assert.assertEquals(short3, short1); + + byte[] bytes3 = ByteUtil.shortToBytes(short1, ByteOrder.LITTLE_ENDIAN); + short short4 = ByteUtil.bytesToShort(bytes3); + Assert.assertEquals(short4, short1); + } + + @Test + public void shortAndBytesBigEndianTest() { + short short1 = 122; + byte[] bytes = ByteUtil.shortToBytes(short1, ByteOrder.BIG_ENDIAN); + short short2 = ByteUtil.bytesToShort(bytes, ByteOrder.BIG_ENDIAN); + + Assert.assertEquals(short2, short1); + } +}