package com.hdl.sdk.common.utils;
|
|
import java.util.ArrayList;
|
import java.util.Arrays;
|
import java.util.List;
|
|
/**
|
* Created by Tong on 2021/9/23.
|
*/
|
public class ByteUtils {
|
|
public static byte[] toByteArray(List<Byte> list) {
|
Byte[] temps = list.toArray(new Byte[0]);
|
byte[] result = new byte[temps.length];
|
for (int i = 0; i < result.length; i++) {
|
result[i] = temps[i];
|
}
|
return result;
|
|
}
|
|
public static List<Byte> toByteList(byte[] bytes) {
|
final List<Byte> list = new ArrayList<>();
|
for (byte aByte : bytes) {
|
list.add(aByte);
|
}
|
return list;
|
|
}
|
|
public static byte[] getRangeBytes(List<Byte> list, int start, int end) {
|
Byte[] temps = Arrays.copyOfRange(list.toArray(new Byte[0]), start, end);
|
byte[] result = new byte[temps.length];
|
for (int i = 0; i < temps.length; i++) {
|
result[i] = temps[i];
|
}
|
return result;
|
|
}
|
|
/**
|
* 拼接byte
|
*/
|
public static byte[] concatBytes(byte[] bt1, byte[] bt2) {
|
if (bt1 == null) {
|
return bt2;
|
}
|
if (bt2 == null) {
|
return bt1;
|
}
|
byte[] bt3 = new byte[bt1.length + bt2.length];
|
System.arraycopy(bt1, 0, bt3, 0, bt1.length);
|
System.arraycopy(bt2, 0, bt3, bt1.length, bt2.length);
|
return bt3;
|
}
|
|
|
public boolean endWith(Byte[] src, byte[] target) {
|
if (src.length < target.length) {
|
return false;
|
}
|
for (int i = 0; i < target.length; i++) {
|
if (target[target.length - i - 1] != src[src.length - i - 1]) {
|
return false;
|
}
|
}
|
return true;
|
}
|
|
|
public static int byteIndexOf(byte[] searched, byte[] find, int start) {
|
boolean matched;
|
int end = find.length - 1;
|
int skip = 0;
|
for (int index = start; index <= searched.length - find.length; ++index) {
|
matched = true;
|
if (find[0] != searched[index] || find[end] != searched[index + end]) continue;
|
else skip++;
|
if (end > 10)
|
if (find[skip] != searched[index + skip] || find[end - skip] != searched[index + end - skip])
|
continue;
|
else skip++;
|
for (int subIndex = skip; subIndex < find.length - skip; ++subIndex) {
|
if (find[subIndex] != searched[index + subIndex]) {
|
matched = false;
|
break;
|
}
|
}
|
if (matched) {
|
return index;
|
}
|
}
|
return -1;
|
|
}
|
|
public static int getByteIndexOf(byte[] sources, byte[] src) {
|
return getByteIndexOf(sources, src, 0, sources.length);
|
}
|
|
//判断一个byte数值在另外一个byte数组中对应的游标值
|
public static int getByteIndexOf(byte[] sources, byte[] src, int startIndex) {
|
return getByteIndexOf(sources, src, startIndex, sources.length);
|
}
|
|
|
//判断一个byte数值在另外一个byte数组中对应的游标值,指定开始的游标和结束的游标位置
|
public static int getByteIndexOf(byte[] sources, byte[] src, int startIndex, int endIndex) {
|
|
if (sources == null || src == null || sources.length == 0 || src.length == 0) {
|
return -1;
|
}
|
|
if (endIndex > sources.length) {
|
endIndex = sources.length;
|
}
|
|
int i, j;
|
for (i = startIndex; i < endIndex; i++) {
|
if (sources[i] == src[0] && i + src.length < endIndex) {
|
for (j = 1; j < src.length; j++) {
|
if (sources[i + j] != src[j]) {
|
break;
|
}
|
}
|
|
if (j == src.length) {
|
return i;
|
}
|
}
|
}
|
return -1;
|
}
|
}
|