用于字节数组操作的 Java 库?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10739448/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 02:19:39  来源:igfitidea点击:

Java library for byte-array manipulations?

javabytearray

提问by Robert

Is there a free library available that contains common methods for manipulating a byte array?

是否有包含操作字节数组的常用方法的免费库?

It should be able to perform the following operations at least ob byte arrays but other array types would be nice, too:

它至少应该能够执行以下操作 ob 字节数组,但其他数组类型也很好:

  • Search for sub-array
  • Search and replace
  • Insert elements at a certain position
  • Delete sub-array at a certain position
  • 搜索子数组
  • 搜索和替换
  • 在特定位置插入元素
  • 删除某个位置的子数组

I know that all those functions are not high magic but implementing them completely and fool & error proof including the correspondent unit tests takes some time.

我知道所有这些功能都不是很神奇,但是完全实现它们并且傻瓜和错误证明,包括相应的单元测试需要一些时间。

Therefore I am searching for a (non-GPL) library that includes those functions. Does anybody know such a library?

因此,我正在寻找包含这些功能的(非 GPL)库。有人知道这样的图书馆吗?

回答by Chandra Sekhar

I think your first three problems can be solved by Java.util.Arraysclass, you don't need to for 3rd party library.

我认为你的前三个问题可以通过Java.util.Arrays类来解决,你不需要第三方库。

Arrays.binarySearch() method is for your first problem.
Arrays.fill() method for your second problem.

For last problem I can suggest some 3rd party util packages that I know.

对于最后一个问题,我可以推荐一些我知道的 3rd 方 util 包。

Google's Guava, Apache's commons API may help.

Google 的 Guava、Apache 的 commons API 可能会有所帮助。

回答by MarcoS

I guess you can do what you need by turning your array into a Collection. If you don't want to use Collections, and you're dealing only with byte[], you could do this something like this:

我想您可以通过将数组转换为Collection. 如果您不想使用Collections,而您只处理byte[],则可以执行以下操作:

public class A {

    public static byte[] deleteSubarray(byte[] array, byte[] subArray) {
        int p = searchFor(array, subArray);
        if (p == -1)
            return array;
        byte[] result = new byte[array.length - subArray.length + 1];
        for (int i = 0; i < p; i++)
            result[i] = array[i];
        for (int i = p + subArray.length - 1; i < array.length; i++) {
            result[p] = array[i];
            p++;
        }
        return result;
    }

    public static byte[] insertElementAt(byte[] array, byte element, int position) {
        byte[] result = new byte[array.length + 1];
        for (int i = 0; i <= position - 1; i++)
            result[i] = array[i];
        result[position] = element;
        for (int i = position + 1; i < array.length; i++) {
            result[i] = array[i];
        }
        return result;
    }

    public static byte[] searchAndReplace(byte[] array, byte[] search, byte[] replace) {
        if (search.length != replace.length)
            return array;
        int p = searchFor(array, search);
        if (p == -1)
            return array;
        byte[] result = Arrays.copyOf(array, array.length);
        for (int i = 0; i < replace.length; i++) {
            result[p] = replace[i];
            p++;
        }
        return result;
    }

    public static int searchFor(byte[] array, byte[] subArray) {
        if (subArray.length > array.length)
            return -1;
        int p = (new String(array)).indexOf(new String(subArray));
        for (int i = 1; i < subArray.length; i++) {
            if (array[p + i] != subArray[i])
                return -1;
        }
        return p;
    }

    public static void main(String[] args) {
        String a = "hello world!";
        String b = "lo w";
        System.out.println(searchFor(a.getBytes(), b.getBytes()));
        System.out.println(new String(searchAndReplace(a.getBytes(), b.getBytes(), "mn x".getBytes())));
        System.out.println(new String(insertElementAt(a.getBytes(), "-".getBytes()[0], 5)));
        System.out.println(new String(deleteSubarray(a.getBytes(), b.getBytes())));
    }

}

Output:

输出:

3
helmn xorld!
hello-world!
helworld!
3
helmn xorld!
hello-world!
helworld!

If you're dealing also with other type of arrays, then the searchFordoesn't work, but you can easily generalize :)

如果您还处理其他类型的数组,则searchFor不起作用,但您可以轻松概括:)