From 14e53d6e3c69d403fc80d261e80fd9ba7f89990e Mon Sep 17 00:00:00 2001 From: Looly Date: Tue, 12 Dec 2023 19:24:08 +0800 Subject: [PATCH] add method --- .../dromara/hutool/core/util/ByteUtil.java | 41 ++++++++++--------- .../hutool/core/util/ByteUtilTest.java | 11 +++-- 2 files changed, 26 insertions(+), 26 deletions(-) diff --git a/hutool-core/src/main/java/org/dromara/hutool/core/util/ByteUtil.java b/hutool-core/src/main/java/org/dromara/hutool/core/util/ByteUtil.java index a7d29c29b..35ca74c1d 100644 --- a/hutool-core/src/main/java/org/dromara/hutool/core/util/ByteUtil.java +++ b/hutool-core/src/main/java/org/dromara/hutool/core/util/ByteUtil.java @@ -213,14 +213,14 @@ public class ByteUtil { public static int toInt(final byte[] bytes, final int start, final ByteOrder byteOrder) { if (ByteOrder.LITTLE_ENDIAN == byteOrder) { return bytes[start] & 0xFF | // - (bytes[1 + start] & 0xFF) << 8 | // - (bytes[2 + start] & 0xFF) << 16 | // - (bytes[3 + start] & 0xFF) << 24; // + (bytes[1 + start] & 0xFF) << 8 | // + (bytes[2 + start] & 0xFF) << 16 | // + (bytes[3 + start] & 0xFF) << 24; // } else { return bytes[3 + start] & 0xFF | // - (bytes[2 + start] & 0xFF) << 8 | // - (bytes[1 + start] & 0xFF) << 16 | // - (bytes[start] & 0xFF) << 24; // + (bytes[2 + start] & 0xFF) << 8 | // + (bytes[1 + start] & 0xFF) << 16 | // + (bytes[start] & 0xFF) << 24; // } } @@ -248,18 +248,18 @@ public class ByteUtil { if (ByteOrder.LITTLE_ENDIAN == byteOrder) { return new byte[]{ // - (byte) (intValue & 0xFF), // - (byte) ((intValue >> 8) & 0xFF), // - (byte) ((intValue >> 16) & 0xFF), // - (byte) ((intValue >> 24) & 0xFF) // + (byte) (intValue & 0xFF), // + (byte) ((intValue >> 8) & 0xFF), // + (byte) ((intValue >> 16) & 0xFF), // + (byte) ((intValue >> 24) & 0xFF) // }; } else { return new byte[]{ // - (byte) ((intValue >> 24) & 0xFF), // - (byte) ((intValue >> 16) & 0xFF), // - (byte) ((intValue >> 8) & 0xFF), // - (byte) (intValue & 0xFF) // + (byte) ((intValue >> 24) & 0xFF), // + (byte) ((intValue >> 16) & 0xFF), // + (byte) ((intValue >> 8) & 0xFF), // + (byte) (intValue & 0xFF) // }; } @@ -480,7 +480,7 @@ public class ByteUtil { return ByteUtil.toBytes(number.shortValue(), byteOrder); } else if (number instanceof Float) { return toBytes(number.floatValue(), byteOrder); - } else if(number instanceof BigInteger){ + } else if (number instanceof BigInteger) { return ((BigInteger) number).toByteArray(); } else { return toBytes(number.doubleValue(), byteOrder); @@ -620,7 +620,7 @@ public class ByteUtil { * @return 连接后的byte[] * @since 6.0.0 */ - public static byte[] concat(final byte[]... byteArrays){ + public static byte[] concat(final byte[]... byteArrays) { int totalLength = 0; for (final byte[] byteArray : byteArrays) { totalLength += byteArray.length; @@ -638,10 +638,11 @@ public class ByteUtil { * * @param buf 无符号bytes * @return 为 1 的个数 + * @see Integer#bitCount(int) */ public static int bitCount(final byte[] buf) { int sum = 0; - for (byte b : buf) { + for (final byte b : buf) { sum += Integer.bitCount((b & 0xFF)); } return sum; @@ -654,9 +655,9 @@ public class ByteUtil { * @return 位数为1的索引集合 */ public static List toUnsignedBitIndex(final byte[] bytes) { - List idxList = new LinkedList<>(); - StringBuilder sb = new StringBuilder(); - for (byte b : bytes) { + final List idxList = new LinkedList<>(); + final StringBuilder sb = new StringBuilder(); + for (final byte b : bytes) { sb.append(StrUtil.padPre(Integer.toBinaryString((b & 0xFF)), 8, "0")); } final String bitStr = sb.toString(); diff --git a/hutool-core/src/test/java/org/dromara/hutool/core/util/ByteUtilTest.java b/hutool-core/src/test/java/org/dromara/hutool/core/util/ByteUtilTest.java index 5b49fbe3b..fe337f1d8 100644 --- a/hutool-core/src/test/java/org/dromara/hutool/core/util/ByteUtilTest.java +++ b/hutool-core/src/test/java/org/dromara/hutool/core/util/ByteUtilTest.java @@ -196,16 +196,15 @@ public class ByteUtilTest { @Test public void toUnsignedBitIndex() { - byte[] bytes = {0, 13, -64, -31, 101, 88, 47, -64}; - List list = ByteUtil.toUnsignedBitIndex(bytes); - Console.log(list); + final byte[] bytes = {0, 13, -64, -31, 101, 88, 47, -64}; + final List list = ByteUtil.toUnsignedBitIndex(bytes); + Assertions.assertEquals("[12, 13, 15, 16, 17, 24, 25, 26, 31, 33, 34, 37, 39, 41, 43, 44, 50, 52, 53, 54, 55, 56, 57]", list.toString()); } @Test public void bitCount() { - byte[] bytes = {0, 13, -64, -31, 101, 88, 47, -64}; - int count = ByteUtil.bitCount(bytes); - Console.log(count); + final byte[] bytes = {0, 13, -64, -31, 101, 88, 47, -64}; + final int count = ByteUtil.bitCount(bytes); Assertions.assertEquals(count, ByteUtil.toUnsignedBitIndex(bytes).size()); } }