From e5bda0265670bb10a4805986f37ac7b0c720ea84 Mon Sep 17 00:00:00 2001 From: Jia-Sheng Chen Date: Mon, 21 Nov 2022 12:04:20 +0800 Subject: [PATCH 1/4] add more methods and tests for converting to byte array --- .../org/astraea/common/backup/ByteUtils.java | 37 +++++++++++ .../astraea/common/backup/ByteUtilsTest.java | 63 +++++++++++++++++++ 2 files changed, 100 insertions(+) create mode 100644 common/src/test/java/org/astraea/common/backup/ByteUtilsTest.java diff --git a/common/src/main/java/org/astraea/common/backup/ByteUtils.java b/common/src/main/java/org/astraea/common/backup/ByteUtils.java index 5697671a65..6efc487434 100644 --- a/common/src/main/java/org/astraea/common/backup/ByteUtils.java +++ b/common/src/main/java/org/astraea/common/backup/ByteUtils.java @@ -34,6 +34,43 @@ public static byte[] toBytes(int value) { }; } + public static byte[] toBytes(long value) { + byte[] result = new byte[8]; + for (int i = 0; i < 8; i++) { + result[i] = (byte) ((value >> (7 - i) * 8) & 0xff); + } + return result; + } + + public static byte[] toBytes(String value) { + return value.getBytes(StandardCharsets.UTF_8); + } + + public static byte[] toBytes(char value) { + return String.valueOf(value).getBytes(StandardCharsets.UTF_8); + } + + public static byte[] toBytes(float value) { + int intBits = Float.floatToIntBits(value); + return new byte[] { + (byte) (intBits >> 24), (byte) (intBits >> 16), (byte) (intBits >> 8), (byte) intBits + }; + } + + public static byte[] toBytes(Double value) { + byte[] result = new byte[8]; + long longBits = Double.doubleToLongBits(value); + for (int i = 0; i < 8; i++) { + result[i] = (byte) ((longBits >> (7 - i) * 8) & 0xff); + } + return result; + } + + public static byte[] toBytes(Boolean value) { + if (value) return new byte[] {1}; + return new byte[] {0}; + } + public static int readInt(ReadableByteChannel channel) { var buf = ByteBuffer.allocate(Integer.BYTES); try { diff --git a/common/src/test/java/org/astraea/common/backup/ByteUtilsTest.java b/common/src/test/java/org/astraea/common/backup/ByteUtilsTest.java new file mode 100644 index 0000000000..092a3ce8fe --- /dev/null +++ b/common/src/test/java/org/astraea/common/backup/ByteUtilsTest.java @@ -0,0 +1,63 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.astraea.common.backup; + +import java.nio.charset.StandardCharsets; +import org.astraea.common.Utils; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +public class ByteUtilsTest { + @Test + void testInt2Bytes() { + Assertions.assertArrayEquals(new byte[] {0, 0, 4, -46}, ByteUtils.toBytes(1234)); + } + + @Test + void testString2Bytes() { + var string = Utils.randomString(); + Assertions.assertEquals(string, new String(ByteUtils.toBytes(string), StandardCharsets.UTF_8)); + } + + @Test + void testChar2Bytes() { + Assertions.assertEquals( + '©', new String(ByteUtils.toBytes('©'), StandardCharsets.UTF_8).charAt(0)); + } + + @Test + void testLong2Bytes() { + Assertions.assertArrayEquals(new byte[] {0, 0, 0, 0, 0, 0, 1, 123}, ByteUtils.toBytes(379L)); + } + + @Test + void testFloat2Bytes() { + Assertions.assertArrayEquals(new byte[] {63, -64, 0, 0}, ByteUtils.toBytes(1.5f)); + } + + @Test + void testDouble2Bytes() { + Assertions.assertArrayEquals( + new byte[] {64, 94, -58, 102, 102, 102, 102, 102}, ByteUtils.toBytes(123.1D)); + } + + @Test + void testBoolean2Bytes() { + Assertions.assertArrayEquals(new byte[] {1}, ByteUtils.toBytes(true)); + Assertions.assertArrayEquals(new byte[] {0}, ByteUtils.toBytes(false)); + } +} From b76eb415e6859cf585042d54cd8ffed4cff822ad Mon Sep 17 00:00:00 2001 From: Jia-Sheng Chen Date: Mon, 21 Nov 2022 12:30:59 +0800 Subject: [PATCH 2/4] change test data in testChar2Bytes to avoid build failed --- .../src/test/java/org/astraea/common/backup/ByteUtilsTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/common/src/test/java/org/astraea/common/backup/ByteUtilsTest.java b/common/src/test/java/org/astraea/common/backup/ByteUtilsTest.java index 092a3ce8fe..5011548f1a 100644 --- a/common/src/test/java/org/astraea/common/backup/ByteUtilsTest.java +++ b/common/src/test/java/org/astraea/common/backup/ByteUtilsTest.java @@ -36,7 +36,7 @@ void testString2Bytes() { @Test void testChar2Bytes() { Assertions.assertEquals( - '©', new String(ByteUtils.toBytes('©'), StandardCharsets.UTF_8).charAt(0)); + 'c', new String(ByteUtils.toBytes('c'), StandardCharsets.UTF_8).charAt(0)); } @Test From 45db32de7ec4ff70957b0e937c8083c140671f68 Mon Sep 17 00:00:00 2001 From: Jia-Sheng Chen Date: Mon, 28 Nov 2022 10:28:47 +0800 Subject: [PATCH 3/4] replace the for loop --- .../org/astraea/common/backup/ByteUtils.java | 30 ++++++++++++------- 1 file changed, 20 insertions(+), 10 deletions(-) diff --git a/common/src/main/java/org/astraea/common/backup/ByteUtils.java b/common/src/main/java/org/astraea/common/backup/ByteUtils.java index 6efc487434..82e8bec7b9 100644 --- a/common/src/main/java/org/astraea/common/backup/ByteUtils.java +++ b/common/src/main/java/org/astraea/common/backup/ByteUtils.java @@ -35,11 +35,16 @@ public static byte[] toBytes(int value) { } public static byte[] toBytes(long value) { - byte[] result = new byte[8]; - for (int i = 0; i < 8; i++) { - result[i] = (byte) ((value >> (7 - i) * 8) & 0xff); - } - return result; + return new byte[] { + (byte) (value >>> 56), + (byte) (value >>> 48), + (byte) (value >>> 40), + (byte) (value >>> 32), + (byte) (value >>> 24), + (byte) (value >>> 16), + (byte) (value >>> 8), + (byte) value + }; } public static byte[] toBytes(String value) { @@ -58,12 +63,17 @@ public static byte[] toBytes(float value) { } public static byte[] toBytes(Double value) { - byte[] result = new byte[8]; long longBits = Double.doubleToLongBits(value); - for (int i = 0; i < 8; i++) { - result[i] = (byte) ((longBits >> (7 - i) * 8) & 0xff); - } - return result; + return new byte[] { + (byte) (longBits >> 56), + (byte) (longBits >> 48), + (byte) (longBits >> 40), + (byte) (longBits >> 32), + (byte) (longBits >> 24), + (byte) (longBits >> 16), + (byte) (longBits >> 8), + (byte) longBits + }; } public static byte[] toBytes(Boolean value) { From c63ab4f95039fcb5354d2d7bce6c4ed430d52194 Mon Sep 17 00:00:00 2001 From: Jia-Sheng Chen Date: Mon, 28 Nov 2022 16:16:34 +0800 Subject: [PATCH 4/4] change parameter type --- common/src/main/java/org/astraea/common/backup/ByteUtils.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/common/src/main/java/org/astraea/common/backup/ByteUtils.java b/common/src/main/java/org/astraea/common/backup/ByteUtils.java index 82e8bec7b9..b61e6ce139 100644 --- a/common/src/main/java/org/astraea/common/backup/ByteUtils.java +++ b/common/src/main/java/org/astraea/common/backup/ByteUtils.java @@ -62,7 +62,7 @@ public static byte[] toBytes(float value) { }; } - public static byte[] toBytes(Double value) { + public static byte[] toBytes(double value) { long longBits = Double.doubleToLongBits(value); return new byte[] { (byte) (longBits >> 56), @@ -76,7 +76,7 @@ public static byte[] toBytes(Double value) { }; } - public static byte[] toBytes(Boolean value) { + public static byte[] toBytes(boolean value) { if (value) return new byte[] {1}; return new byte[] {0}; }