java字节数组处理工具类

直接上代码

package copsec.safecheckmonitor.util;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.List;

/**
 * @Author wangchang
 * @Description 字节处理工具类
 * @Date 2024/6/19 9:47
 */
public class ByteUtils {


    /**
     * 两个数组合并
     *
     * @param byte1
     * @param byte2
     * @return
     */
    public static byte[] mergeByteArrays(byte[] byte1, byte[] byte2) {
        byte[] result = new byte[byte1.length + byte2.length];
        System.arraycopy(byte1, 0, result, 0, byte1.length);
        System.arraycopy(byte2, 0, result, byte1.length, byte2.length);
        return result;
    }

    public static byte[] mergeBytebyteAndArr(byte byte1, byte[] bytes2) {
        byte[] result = new byte[1 + bytes2.length];
        Arrays.fill(result, 0, 1, byte1);
        System.arraycopy(bytes2, 0, result, 1, bytes2.length);
        return result;
    }

    /**
     * 多个数组合并
     *
     * @param byteArrays
     * @return
     */
    public static byte[] mergeByteArrays(List<byte[]> byteArrays) {
        int length = 0;
        for (byte[] byteArray : byteArrays) {
            length += byteArray.length;
        }

        byte[] mergedArray = new byte[length];
        int destPos = 0;
        for (byte[] byteArray : byteArrays) {
            System.arraycopy(byteArray, 0, mergedArray, destPos, byteArray.length);
            destPos += byteArray.length;
        }

        return mergedArray;
    }


    public static String[] listToStringArr(List<byte[]> listByteArr) {

        //初始化需要得到的数组
        String[] array = new String[listByteArr.size()];
        //使用for循环得到数组
        for (int i = 0; i < listByteArr.size(); i++) {
            array[i] = String.valueOf(listByteArr.get(i));
        }
        return array;
    }


    /**
     * 指定开始结束位置读取文件字节数组
     *
     * @param start
     * @param end
     * @return
     */
    public static byte[] readFileBytes(RandomAccessFile raf, long start, long end) {
        try {

            // 计算要读取的字节数组长度
            int bufferSize = (int) (end - start);
            byte[] buffer = new byte[bufferSize];

            // 设置读取的起始位置
            raf.seek(start);
            // 读取字节数组到缓冲区
            int bytesRead = raf.read(buffer, 0, bufferSize);

            if (bytesRead == bufferSize) {
                return buffer;
            } else {
                // 文件读取异常,返回空数组或其他处理方式
                return new byte[0];
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new byte[0];
        }
    }

    /**
     * 截取字节数组中的一部分
     *
     * @param bytes
     * @param start
     * @param len
     * @return
     */
    public static byte[] bytesSubStr(byte[] bytes, int start, int len) {
        if ((start + len) > bytes.length) {
            len = bytes.length - start;
        }

        byte[] endBytes = new byte[len];
        for (int i = 0; i < len; i++) {
            endBytes[i] = bytes[start + i];
        }
        return endBytes;
    }

    /**
     * 转为固定长度字节数组
     *
     * @param str
     * @param length
     * @return
     */
    public static byte[] getBytes_0(String str, int length) {
        int fixLen = length - str.getBytes().length;
        if (str.getBytes().length < length) {
            byte[] S_bytes = new byte[length];
            System.arraycopy(str.getBytes(), 0, S_bytes, 0, str.getBytes().length);
            for (int x = length - fixLen; x < length; x++) {
                S_bytes[x] = 0x00;
            }
            return S_bytes;
        }
        return str.getBytes();
    }

    /**
     * 数组转字符串,忽略末位为0部分
     *
     * @param bytes
     * @return
     */
    public static String bytesToString(byte[] bytes) {
        if (bytes == null) {
            throw new IllegalArgumentException("The byte array cannot be null.");
        }
        int length = bytes.length;
        int endIndex = length;

        // 寻找字节数组末尾不为零的索引
        for (int i = length - 1; i >= 0; i--) {
            if (bytes[i] != 0) {
                endIndex = i + 1;
                break;
            }
        }
        return new String(bytes, 0, endIndex, StandardCharsets.UTF_8);
    }


    /**
     * 字节数组转文件
     *
     * @param bytes
     * @param filePath
     */
    public static void byteArrToFile(byte[] bytes, String filePath) {
        BufferedOutputStream bos = null;
        FileOutputStream fos = null;
        File file = null;
        try {
            file = new File(filePath);
            fos = new FileOutputStream(file);
            bos = new BufferedOutputStream(fos);
            bos.write(bytes);
            bos.close();
            fos.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    /**
     * 分段写入数据
     *
     * @param bos
     * @param fos
     * @param file
     * @param bytes
     * @param filePath
     * @param isEndPackage
     */
    public static void byteArrToFileChunk(BufferedOutputStream bos, FileOutputStream fos, File file, byte[] bytes, String filePath, boolean isEndPackage) {
        try {

            bos.write(bytes);
            if (isEndPackage) {
                bos.close();
                fos.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    /**
     * 将int转为高字节在前,低字节在后的byte数组(大端)
     *
     * @param n int
     * @return byte[]
     */
    public static byte[] intToByteBig(int n) {
        byte[] b = new byte[4];
        b[3] = (byte) (n & 0xff);
        b[2] = (byte) (n >> 8 & 0xff);
        b[1] = (byte) (n >> 16 & 0xff);
        b[0] = (byte) (n >> 24 & 0xff);
        return b;
    }

    /**
     * 将int转为低字节在前,高字节在后的byte数组(小端)
     *
     * @param n int
     * @return byte[]
     */
    public static byte[] intToByteLittle(int n) {
        byte[] b = new byte[4];
        b[0] = (byte) (n & 0xff);
        b[1] = (byte) (n >> 8 & 0xff);
        b[2] = (byte) (n >> 16 & 0xff);
        b[3] = (byte) (n >> 24 & 0xff);
        return b;
    }


    /**
     * byte数组到int的转换(小端)
     *
     * @param bytes
     * @return
     */
    public static int bytesToIntLittle(byte[] bytes) {
        int int1 = bytes[0] & 0xff;
        int int2 = (bytes[1] & 0xff) << 8;
        int int3 = (bytes[2] & 0xff) << 16;
        int int4 = (bytes[3] & 0xff) << 24;

        return int1 | int2 | int3 | int4;
    }

    /**
     * byte数组到int的转换(大端)
     *
     * @param bytes
     * @return
     */
    public static int bytes2IntBig(byte[] bytes) {
        int int1 = bytes[3] & 0xff;
        int int2 = (bytes[2] & 0xff) << 8;
        int int3 = (bytes[1] & 0xff) << 16;
        int int4 = (bytes[0] & 0xff) << 24;

        return int1 | int2 | int3 | int4;
    }

    /**
     * 将short转为高字节在前,低字节在后的byte数组(大端)
     *
     * @param n short
     * @return byte[]
     */
    public static byte[] shortToByteBig(short n) {
        byte[] b = new byte[2];
        b[1] = (byte) (n & 0xff);
        b[0] = (byte) (n >> 8 & 0xff);
        return b;
    }

    /**
     * 将short转为低字节在前,高字节在后的byte数组(小端)
     *
     * @param n short
     * @return byte[]
     */
    public static byte[] shortToByteLittle(short n) {
        byte[] b = new byte[2];
        b[0] = (byte) (n & 0xff);
        b[1] = (byte) (n >> 8 & 0xff);
        return b;
    }

    /**
     * 读取小端byte数组为short
     *
     * @param b
     * @return
     */
    public static short byteToShortLittle(byte[] b) {
        return (short) (((b[1] << 8) | b[0] & 0xff));
    }

    /**
     * 读取大端byte数组为short
     *
     * @param b
     * @return
     */
    public static short byteToShortBig(byte[] b) {
        return (short) (((b[0] << 8) | b[1] & 0xff));
    }

    /**
     * long类型转byte[] (大端)
     *
     * @param n
     * @return
     */
    public static byte[] longToBytesBig(long n) {
        byte[] b = new byte[8];
        b[7] = (byte) (n & 0xff);
        b[6] = (byte) (n >> 8 & 0xff);
        b[5] = (byte) (n >> 16 & 0xff);
        b[4] = (byte) (n >> 24 & 0xff);
        b[3] = (byte) (n >> 32 & 0xff);
        b[2] = (byte) (n >> 40 & 0xff);
        b[1] = (byte) (n >> 48 & 0xff);
        b[0] = (byte) (n >> 56 & 0xff);
        return b;
    }

    /**
     * long类型转byte[] (小端)
     *
     * @param n
     * @return
     */
    public static byte[] longToBytesLittle(long n) {
        byte[] b = new byte[8];
        b[0] = (byte) (n & 0xff);
        b[1] = (byte) (n >> 8 & 0xff);
        b[2] = (byte) (n >> 16 & 0xff);
        b[3] = (byte) (n >> 24 & 0xff);
        b[4] = (byte) (n >> 32 & 0xff);
        b[5] = (byte) (n >> 40 & 0xff);
        b[6] = (byte) (n >> 48 & 0xff);
        b[7] = (byte) (n >> 56 & 0xff);
        return b;
    }

    /**
     * byte[]转long类型(小端)
     *
     * @param array
     * @return
     */
    public static long bytesToLongLittle(byte[] array) {
        return ((((long) array[0] & 0xff) << 0)
                | (((long) array[1] & 0xff) << 8)
                | (((long) array[2] & 0xff) << 16)
                | (((long) array[3] & 0xff) << 24)
                | (((long) array[4] & 0xff) << 32)
                | (((long) array[5] & 0xff) << 40)
                | (((long) array[6] & 0xff) << 48)
                | (((long) array[7] & 0xff) << 56));
    }

    /**
     * byte[]转long类型(大端)
     *
     * @param array
     * @return
     */
    public static long bytesToLongBig(byte[] array) {
        return ((((long) array[0] & 0xff) << 56)
                | (((long) array[1] & 0xff) << 48)
                | (((long) array[2] & 0xff) << 40)
                | (((long) array[3] & 0xff) << 32)
                | (((long) array[4] & 0xff) << 24)
                | (((long) array[5] & 0xff) << 16)
                | (((long) array[6] & 0xff) << 8)
                | (((long) array[7] & 0xff) << 0));
    }


}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

王之蔑视.

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值