处理字节的常用工具类方法,供大家参考,具体内容如下
package com.demo.utils;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.nio.charset.Charset;
import java.util.Arrays;
public class ByteUtils {
private static final byte[] BUILD_BYTE_TABLE = new byte[] { (byte) 128, (byte) 64, (byte) 32, (byte) 16, (byte) 8, (byte) 4, (byte) 2, (byte) 1 };
private ByteUtils() {}
public static byte[] shortToByte(short number) {
byte[] b = new byte[2];
for (int i = 1; i >= 0; i--) {
b[i] = (byte) (number % 256);
number >>= 8;
}
return b;
}
public static short byteToShort(byte[] b) {
return (short) ((((b[0] & 0xff) << 8) | b[1] & 0xff));
}
public static byte[] intToByte(int number) {
byte[] b = new byte[4];
for (int i = 3; i >= 0; i--) {
b[i] = (byte) (number % 256);
number >>= 8;
}
return b;
}
public static int byteToInt(byte[] b) {
return ((((b[0] & 0xff) << 24) | ((b[1] & 0xff) << 16) | ((b[2] & 0xff) << 8) | (b[3] & 0xff)));
}
public static byte[] longToByte(long number) {
byte[] b = new byte[8];
for (int i = 7; i >= 0; i--) {
b[i] = (byte) (number % 256);
number >>= 8;
}
return b;
}
public static long byteToLong(byte[] b) {
return ((((long) b[0] & 0xff) << 56) | (((long) b[1] & 0xff) << 48) | (((long) b[2] & 0xff) << 40) | (((long) b[3] & 0xff) << 32) | (((long) b[4] & 0xff) << 24)
| (((long) b[5] & 0xff) << 16) | (((long) b[6] & 0xff) << 8) | ((long) b[7] & 0xff));
}
public static byte[] doubleToByte(double d) {
byte[] bytes = new byte[8];
long l = Double.doubleToLongBits(d);
for (int i = 0; i < bytes.length; i++) {
bytes[i] = Long.valueOf(l).bytevalue();
l = l >> 8;
}
return bytes;
}
public static double byteToDouble(byte[] b) {
long l;
l = b[0];
l &= 0xff;
l |= ((long) b[1] << 8);
l &= 0xffff;
l |= ((long) b[2] << 16);
l &= 0xffffff;
l |= ((long) b[3] << 24);
l &= 0xffffffffl;
l |= ((long) b[4] << 32);
l &= 0xffffffffffl;
l |= ((long) b[5] << 40);
l &= 0xffffffffffffl;
l |= ((long) b[6] << 48);
l &= 0xffffffffffffffl;
l |= ((long) b[7] << 56);
return Double.longBitsToDouble(l);
}
public static byte[] floatToByte(float d) {
byte[] bytes = new byte[4];
int l = Float.floatToIntBits(d);
for (int i = 0; i < bytes.length; i++) {
bytes[i] = Integer.valueOf(l).bytevalue();
l = l >> 8;
}
return bytes;
}
public static float byteToFloat(byte[] b) {
int l;
l = b[0];
l &= 0xff;
l |= ((long) b[1] << 8);
l &= 0xffff;
l |= ((long) b[2] << 16);
l &= 0xffffff;
l |= ((long) b[3] << 24);
l &= 0xffffffffl;
return Float.intBitsToFloat(l);
}
public static byte[] stringToByte(String s, Charset charset) {
return s.getBytes(charset);
}
public static String byteToString(byte[] b, Charset charset) {
return new String(b, charset);
}
public static byte[] objectToByte(Object obj) throws IOException {
ByteArrayOutputStream buff = new ByteArrayOutputStream();
ObjectOutputStream out = new ObjectOutputStream(buff);
out.writeObject(obj);
try {
return buff.toByteArray();
} finally {
out.close();
}
}
public static Object byteToObject(byte[] b) throws IOException, ClassNotFoundException {
ByteArrayInputStream buff = new ByteArrayInputStream(b);
ObjectInputStream in = new ObjectInputStream(buff);
Object obj = in.readObject();
try {
return obj;
} finally {
in.close();
}
}
public static boolean equalsBit(byte a, byte b) {
return Arrays.equals(byteToBitArray(a), byteToBitArray(b));
}
public static boolean equalsBit(byte[] a, byte[] b) {
if (a == b) {
return true;
}
if (a == null || b == null) {
return false;
}
int length = a.length;
if (b.length != length) {
return false;
}
for (int count = 0; count < a.length; count++) {
if (!equalsBit(a[count], b[count])) {
return false;
}
}
return true;
}
public static String bitString(byte b) {
StringBuilder buff = new StringBuilder();
boolean[] array = byteToBitArray(b);
for (int i = 0; i < array.length; i++) {
buff.append(array[i] ? 1 : 0);
}
return buff.toString();
}
public static boolean[] byteToBitArray(byte b) {
boolean[] buff = new boolean[8];
int index = 0;
for (int i = 7; i >= 0; i--) {
buff[index++] = ((b >>> i) & 1) == 1;
}
return buff;
}
public static boolean byteBitValue(byte b, int index) {
return byteToBitArray(b)[index];
}
public static byte buildNewByte(boolean[] values) {
byte b = 0;
for (int i = 0; i < 8; i++) {
if (values[i]) {
b |= BUILD_BYTE_TABLE[i];
}
}
return b;
}
public static byte changeByteBitValue(byte b, int index, boolean newValue) {
boolean[] bitValues = byteToBitArray(b);
bitValues[index] = newValue;
return buildNewByte(bitValues);
}
public static byte[] ipAddressBytes(String address) {
if (address == null || address.length() < 0 || address.length() > 15) {
throw new IllegalArgumentException("Invalid IP address.");
}
final int ipSize = 4;// 最大IP位数
final char ipSpace = '.';// IP数字的分隔符
int[] ipNums = new int[ipSize];
StringBuilder number = new StringBuilder();// 当前操作的数字
StringBuilder buff = new StringBuilder(address);
int point = 0;// 当前操作的数字下标,最大到3.
char currentChar;
for (int i = 0; i < buff.length(); i++) {
currentChar = buff.charAt(i);
if (ipSpace == currentChar) {
// 当前位置等于最大于序号后,还有字符没有处理表示这是一个错误的IP.
if (point == ipSize - 1 && buff.length() - (i + 1) > 0) {
throw new IllegalArgumentException("Invalid IP address.");
}
ipNums[point++] = Integer.parseInt(number.toString());
number.delete(0, number.length());
} else {
number.append(currentChar);
}
}
ipNums[point] = Integer.parseInt(number.toString());
byte[] ipBuff = new byte[ipSize];
int pointNum = 0;
for (int i = 0; i < 4; i++) {
pointNum = Math.abs(ipNums[i]);
if (pointNum > 255) {
throw new IllegalArgumentException("Invalid IP address.");
}
ipBuff[i] = (byte) (pointNum & 0xff);
}
return ipBuff;
}
}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持考高分网。



