JAVA将byte数组(byte[])按照指定大小分割成多个byte数组

 

/**
     * 将byte数组按照指定大小分割成多个数组
     * @param bytes   要分割的byte数组
     * @param subSize  分割的块大小  单位:字节
     * @return 指定大小的byte数组
     */

    public static Object[] splitByteArr(byte[] bytes, int subSize) {
        int count = bytes.length % subSize == 0 ? bytes.length / subSize : bytes.length / subSize + 1;

        List<List<Byte>> subAryList = new ArrayList<List<Byte>>();

        for (int i = 0; i < count; i++) {
            int index = i * subSize;
            List<Byte> list = new ArrayList<Byte>();
            int j = 0;
            while (j < subSize && index < bytes.length) {
                list.add(bytes[index++]);
                j++;
            }
            subAryList.add(list);
        }

        Object[] subAry = new Object[subAryList.size()];

        for (int i = 0; i < subAryList.size(); i++) {
            List<Byte> subList = subAryList.get(i);
            byte[] subAryItem = new byte[subList.size()];
            for (int j = 0; j < subList.size(); j++) {
                subAryItem[j] = subList.get(j);
            }
            subAry[i] = subAryItem;
        }
        return subAry;
    }

 

然后通过方法将object转成byte数组 参考 :https://www.cnblogs.com/pxblog/p/14919068.html

posted @ 2021-06-22 16:17  yvioo  阅读(4183)  评论(0编辑  收藏  举报