forked from plusone/plusone-commons
新增 ByteArrayTools
parent
f2e12a4aab
commit
6ba06d7ea1
|
@ -0,0 +1,57 @@
|
|||
/*
|
||||
* Copyright 2024 the original author or authors.
|
||||
*
|
||||
* Licensed 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
|
||||
*
|
||||
* https://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 xyz.zhouxy.plusone.commons.util;
|
||||
|
||||
import com.google.common.annotations.Beta;
|
||||
|
||||
/**
|
||||
* ByteArrayTools
|
||||
*
|
||||
* <p>byte 数组工具类
|
||||
*
|
||||
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
|
||||
*/
|
||||
@Beta
|
||||
public class ByteArrayTools {
|
||||
|
||||
public static byte[] longToByteArray(long value) {
|
||||
final byte[] bytes = new byte[8];
|
||||
fillToByteArrayInternal(value, bytes, 0);
|
||||
return bytes;
|
||||
}
|
||||
|
||||
public static void fillToByteArray(long value, final byte[] bytes) {
|
||||
AssertTools.checkArgument(bytes != null && bytes.length >= 8);
|
||||
fillToByteArrayInternal(value, bytes, 0);
|
||||
}
|
||||
|
||||
public static void fillToByteArray(long value, final byte[] bytes, int startIndex) {
|
||||
AssertTools.checkArgument(bytes != null && bytes.length >= startIndex + 8);
|
||||
fillToByteArrayInternal(value, bytes, startIndex);
|
||||
}
|
||||
|
||||
private static void fillToByteArrayInternal(long value, final byte[] bytes, int startIndex) {
|
||||
for (int i = 0; i < 8; i++) {
|
||||
int offset = 8 * (7 - i);
|
||||
bytes[startIndex + i] = (byte) ((value & (0xFFL << offset)) >> offset);
|
||||
}
|
||||
}
|
||||
|
||||
private ByteArrayTools() {
|
||||
throw new IllegalStateException("Utility class");
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue