public class Bytes extends Object
可对数字和字节进行转换。 假设数据存储是以大端模式存储的:
| 限定符和类型 | 字段和说明 |
|---|---|
static ByteOrder |
CPU_ENDIAN
CPU的字节序
|
static ByteOrder |
DEFAULT_ORDER |
| 构造器和说明 |
|---|
Bytes() |
| 限定符和类型 | 方法和说明 |
|---|---|
static byte[] |
bytes(CharSequence str) |
static byte[] |
bytes(String str) |
static double |
bytesToDouble(byte[] bytes)
byte数组转Double
默认以大端序转换 |
static double |
bytesToDouble(byte[] bytes,
ByteOrder byteOrder)
byte数组转double
自定义端序 |
static float |
bytesToFloat(byte[] bytes)
byte数组转float
默认以大端序转换 |
static float |
bytesToFloat(byte[] bytes,
ByteOrder byteOrder)
byte数组转float
自定义端序 |
static int |
bytesToInt(byte[] bytes)
byte[]转int值
默认以大端序转换 |
static int |
bytesToInt(byte[] bytes,
ByteOrder byteOrder)
byte[]转int值
自定义端序 |
static int |
bytesToInt(byte[] bytes,
int start,
ByteOrder byteOrder)
byte[]转int值
自定义端序 |
static long |
bytesToLong(byte[] bytes)
byte数组转long
默认以大端序转换 from: https://stackoverflow.com/questions/4485128/how-do-i-convert-long-to-byte-and-back-in-java |
static long |
bytesToLong(byte[] bytes,
ByteOrder byteOrder)
byte数组转long
自定义端序 from: https://stackoverflow.com/questions/4485128/how-do-i-convert-long-to-byte-and-back-in-java |
static long |
bytesToLong(byte[] bytes,
int start,
ByteOrder byteOrder)
byte数组转long
自定义端序 from: https://stackoverflow.com/questions/4485128/how-do-i-convert-long-to-byte-and-back-in-java |
static <T extends Number> |
bytesToNumber(byte[] bytes,
Class<T> targetClass,
ByteOrder byteOrder)
byte数组转换为指定类型数字
|
static short |
bytesToShort(byte[] bytes)
byte数组转short
默认以大端序转换 |
static short |
bytesToShort(byte[] bytes,
ByteOrder byteOrder)
byte数组转short
自定义端序 |
static short |
bytesToShort(byte[] bytes,
int start,
ByteOrder byteOrder)
byte数组转short
自定义端序 |
static int |
byteToUnsignedInt(byte byteValue)
byte转无符号int
|
static byte[] |
concat(byte[] byteArray,
byte[]... byteArrays) |
static boolean |
contains(byte[] array,
byte value)
数组中是否包含元素
|
static byte[] |
copy(byte[] array,
int length) |
static byte[] |
copy(byte[] array,
int from,
int to) |
static byte[] |
doubleToBytes(double doubleValue)
double转byte数组
默认以大端序转换 |
static byte[] |
doubleToBytes(double doubleValue,
ByteOrder byteOrder)
double转byte数组
自定义端序 from: https://stackoverflow.com/questions/4485128/how-do-i-convert-long-to-byte-and-back-in-java |
static byte[] |
floatToBytes(float floatValue)
float转byte数组,默认以大端序转换
|
static byte[] |
floatToBytes(float floatValue,
ByteOrder byteOrder)
float转byte数组,自定义端序
|
static int |
indexOf(byte[] array,
byte value)
返回数组中指定元素所在位置,未找到返回 -1
|
static int |
indexOf(byte[] array,
byte[] target) |
static byte |
intToByte(int intValue)
int转byte
|
static byte[] |
intToBytes(int intValue)
int转byte数组
默认以大端序转换 |
static byte[] |
intToBytes(int intValue,
ByteOrder byteOrder)
int转byte数组
自定义端序 |
static boolean |
isEmpty(byte[] array)
数组是否为空
|
static boolean |
isNotEmpty(byte[] array)
数组是否为非空
|
static boolean |
isSorted(byte[] array)
检查数组是否升序,即array[i] <= array[i+1],若传入空数组,则返回false
|
static boolean |
isSortedAsc(byte[] array)
检查数组是否升序,即array[i] <= array[i+1],若传入空数组,则返回false
|
static boolean |
isSortedDesc(byte[] array)
检查数组是否降序,即array[i] >= array[i+1],若传入空数组,则返回false
|
static byte[] |
join(byte[]... arrays)
将多个数组合并在一起
忽略null的数组 |
static int |
lastIndexOf(byte[] array,
byte value)
返回数组中指定元素所在最后的位置,未找到返回 -1
|
static byte[] |
longToBytes(long longValue)
long转byte数组
默认以大端序转换 from: https://stackoverflow.com/questions/4485128/how-do-i-convert-long-to-byte-and-back-in-java |
static byte[] |
longToBytes(long longValue,
ByteOrder byteOrder)
long转byte数组
自定义端序 from: https://stackoverflow.com/questions/4485128/how-do-i-convert-long-to-byte-and-back-in-java |
static byte |
max(byte... numberArray)
取最大值
|
static byte |
min(byte... numberArray)
取最小值
|
static byte[] |
numberToBytes(Number number)
将
Number转换为 |
static byte[] |
numberToBytes(Number number,
ByteOrder byteOrder)
将
Number转换为 |
static byte[] |
remove(byte[] array,
int index)
移除数组中对应位置的元素
copy from commons-lang |
static byte[] |
removeElement(byte[] array,
byte element)
移除数组中指定的元素
只会移除匹配到的第一个元素 copy from commons-lang |
static byte[] |
resize(byte[] bytes,
int newSize)
生成一个新的重新设置大小的数组
调整大小后拷贝原数组到新数组下。 |
static byte[] |
reverse(byte[] array)
反转数组,会变更原数组
|
static byte[] |
reverse(byte[] array,
int startIndexInclusive,
int endIndexExclusive)
反转数组,会变更原数组
|
static byte[] |
shortToBytes(short shortValue)
short转byte数组
默认以大端序转换 |
static byte[] |
shortToBytes(short shortValue,
ByteOrder byteOrder)
short转byte数组
自定义端序 |
static byte[] |
shuffle(byte[] array)
打乱数组顺序,会变更原数组
|
static byte[] |
shuffle(byte[] array,
Random random)
打乱数组顺序,会变更原数组
|
static byte[][] |
split(byte[] array,
int len)
拆分byte数组为几个等份(最后一份按照剩余长度分配空间)
|
static byte[] |
sub(byte[] array,
int start,
int end)
获取子数组
|
static byte[] |
swap(byte[] array,
int index1,
int index2)
交换数组中两个位置的值
|
static byte[] |
unwrap(Byte... values)
包装类数组转为原始类型数组
|
static byte[] |
utf8Bytes(CharSequence str) |
static byte[] |
utf8Bytes(String str) |
static Byte[] |
wrap(byte... values)
将原始类型数组包装为包装类型
|
public static final ByteOrder DEFAULT_ORDER
public static final ByteOrder CPU_ENDIAN
public static boolean isEmpty(byte[] array)
array - 数组public static boolean isNotEmpty(byte[] array)
array - 数组public static byte[] resize(byte[] bytes,
int newSize)
bytes - 原数组newSize - 新的数组大小public static byte[] join(byte[]... arrays)
arrays - 数组集合public static byte[][] split(byte[] array,
int len)
array - 数组len - 每个小节的长度public static int indexOf(byte[] array,
byte value)
array - 数组value - 被检查的元素public static int lastIndexOf(byte[] array,
byte value)
array - 数组value - 被检查的元素public static boolean contains(byte[] array,
byte value)
array - 数组value - 被检查的元素public static Byte[] wrap(byte... values)
values - 原始类型数组public static byte[] unwrap(Byte... values)
values - 包装类型数组public static byte[] sub(byte[] array,
int start,
int end)
array - 数组start - 开始位置(包括)end - 结束位置(不包括)Arrays.copyOfRange(Object[], int, int)public static byte[] remove(byte[] array,
int index)
throws IllegalArgumentException
array - 数组对象,可以是对象数组,也可以原始类型数组index - 位置,如果位置小于0或者大于长度,返回原数组IllegalArgumentException - 参数对象不为数组对象public static byte[] removeElement(byte[] array,
byte element)
throws IllegalArgumentException
array - 数组对象,可以是对象数组,也可以原始类型数组element - 要移除的元素IllegalArgumentException - 参数对象不为数组对象public static byte[] reverse(byte[] array,
int startIndexInclusive,
int endIndexExclusive)
array - 数组,会变更startIndexInclusive - 起始位置(包含)endIndexExclusive - 结束位置(不包含)public static byte[] reverse(byte[] array)
array - 数组,会变更public static byte min(byte... numberArray)
numberArray - 数字数组public static byte max(byte... numberArray)
numberArray - 数字数组public static byte[] shuffle(byte[] array)
array - 数组,会变更public static byte[] shuffle(byte[] array,
Random random)
array - 数组,会变更random - 随机数生成器public static byte[] swap(byte[] array,
int index1,
int index2)
array - 数组index1 - 位置1index2 - 位置2public static boolean isSorted(byte[] array)
array - 数组public static boolean isSortedAsc(byte[] array)
array - 数组public static boolean isSortedDesc(byte[] array)
array - 数组public static byte[] utf8Bytes(CharSequence str)
public static byte[] utf8Bytes(String str)
public static byte[] bytes(CharSequence str)
public static byte[] bytes(String str)
public static byte[] copy(byte[] array,
int length)
public static byte[] copy(byte[] array,
int from,
int to)
public static byte[] concat(@Nonnull byte[] byteArray, byte[]... byteArrays)
public static int indexOf(byte[] array,
byte[] target)
public static byte intToByte(int intValue)
intValue - int值public static int byteToUnsignedInt(byte byteValue)
byteValue - byte值public static short bytesToShort(byte[] bytes)
bytes - byte数组public static short bytesToShort(byte[] bytes,
ByteOrder byteOrder)
bytes - byte数组,长度必须为2byteOrder - 端序public static short bytesToShort(byte[] bytes,
int start,
ByteOrder byteOrder)
bytes - byte数组,长度必须大于2start - 开始位置byteOrder - 端序public static byte[] shortToBytes(short shortValue)
shortValue - short值public static byte[] shortToBytes(short shortValue,
ByteOrder byteOrder)
shortValue - short值byteOrder - 端序public static int bytesToInt(byte[] bytes)
bytes - byte数组public static int bytesToInt(byte[] bytes,
ByteOrder byteOrder)
bytes - byte数组byteOrder - 端序public static int bytesToInt(byte[] bytes,
int start,
ByteOrder byteOrder)
bytes - byte数组start - 开始位置(包含)byteOrder - 端序public static byte[] intToBytes(int intValue)
intValue - int值public static byte[] intToBytes(int intValue,
ByteOrder byteOrder)
intValue - int值byteOrder - 端序public static byte[] longToBytes(long longValue)
longValue - long值public static byte[] longToBytes(long longValue,
ByteOrder byteOrder)
longValue - long值byteOrder - 端序public static long bytesToLong(byte[] bytes)
bytes - byte数组public static long bytesToLong(byte[] bytes,
ByteOrder byteOrder)
bytes - byte数组byteOrder - 端序public static long bytesToLong(byte[] bytes,
int start,
ByteOrder byteOrder)
bytes - byte数组start - 计算数组开始位置byteOrder - 端序public static byte[] floatToBytes(float floatValue)
floatValue - float值public static byte[] floatToBytes(float floatValue,
ByteOrder byteOrder)
floatValue - float值byteOrder - 端序public static float bytesToFloat(byte[] bytes)
bytes - byte数组public static float bytesToFloat(byte[] bytes,
ByteOrder byteOrder)
bytes - byte数组byteOrder - 端序public static byte[] doubleToBytes(double doubleValue)
doubleValue - double值public static byte[] doubleToBytes(double doubleValue,
ByteOrder byteOrder)
doubleValue - double值byteOrder - 端序public static double bytesToDouble(byte[] bytes)
bytes - byte数组public static double bytesToDouble(byte[] bytes,
ByteOrder byteOrder)
bytes - byte数组byteOrder - 端序public static byte[] numberToBytes(Number number)
Number转换为number - 数字public static byte[] numberToBytes(Number number, ByteOrder byteOrder)
Number转换为number - 数字byteOrder - 端序public static <T extends Number> T bytesToNumber(byte[] bytes, Class<T> targetClass, ByteOrder byteOrder) throws IllegalArgumentException
T - 数字类型bytes - byte数组targetClass - 目标数字类型byteOrder - 端序IllegalArgumentException - 不支持的数字类型,如用户自定义数字类型Copyright © 2024 fossc. All rights reserved.