简体   繁体   English

是否可以扩展字节数组?

[英]Is it possible to extend byte array?

I am implementing AES for an assignment, and would like to encapsulate the logic of checking that a key is always an array of bytes whose length is exactly 16, 24 or 32. 我正在为分配实现AES,并希望封装检查密钥始终是长度为16、24或32的字节数组的逻辑。

I can create a wrapper class that stores the bytes as a byte[] internally, but I wonder if it is possible to extend Array or something similar so that I don't have to reimplement Iterators, toString and other convenience methods that I get from using Java array. 我可以创建一个包装器类,在内部将字节存储为byte [],但我想知道是否可以扩展Array或类似的东西,从而不必重新实现Iterators,toString和其他从我那里得到的便利方法。使用Java数组。

Ie is something like the following possible? 即可能出现以下情况?

public class Key extends Array<Byte>{

    // Valid length of key in bytes
    public static int[] possibleLengths = new int[]{ 16, 24, 32 };

    // Check if a given byte[] is a valid candidate for a key
    public static boolean isValidLength(byte[] input) {
        for (int length: possibleLengths) {
            if (input.length == length) return true;
        }
        return false;
    }

    // Primary constructor passes bytes directly to internal state
    public Key(byte[] input) throws InvalidKeyLengthException {
        if (!isValidLength(input))
            throw new InvalidKeyLengthException;

        setKey(input);
    }
}

Well...no. 好吧...不 You can't extend an array type, because it's dynamically generated upon array instantiation. 您不能扩展数组类型,因为它是在数组实例化时动态生成的

It would make far more sense to have this as a field to a wrapper class (namely Key ). 将其作为包装类(即Key )的字段会更有意义。

If you're concerned about having to use helper methods and the like, then you could simply delegate the major responsibilities (namely exposing the array and exposing a sensible toString() ). 如果您担心必须使用辅助方法等,则可以简单地委派主要职责(即公开数组和公开明智的toString() )。

No, it is not possible to extend the array classes. 不,不可能扩展数组类。

I believe it is possible to extend ArrayList, however, which might get you close enough to what you want. 我相信可以扩展ArrayList,但这可能使您足够接近所需的内容。 You would need to disable ArrayList's ability to arbitrarily expands its capacity, however. 但是,您需要禁用ArrayList的能力以任意扩展其容量。

That said, you should probably follow to the guideline "Prefer aggregation over inheritance" in this case and just make your array or ArrayList a field within your class. 就是说,在这种情况下,您可能应该遵循准则“优先考虑聚合而不是继承”,并且只需将数组或ArrayList设置为类中的一个字段即可。

Array is a weird notion. 数组是一个奇怪的概念。 They do have some properties, like length although they are not a class, so you can't extend them like you are attempting. 它们确实具有一些属性,例如length,尽管它们不是类,所以您不能像尝试那样扩展它们。

You may convert it to ArrayList : 您可以将其转换为ArrayList

new ArrayList<Byte>(Arrays.asList(Key))

so, you may create a class extending ArrayList and add your new functionalists to it. 因此,您可以创建一个扩展ArrayList的类,并向其中添加新的功能主义者。 Then convert the Byte Array as said above to your custom class and check your requirements with your newly extended methods freely! 然后将上述Byte Array转换为自定义类,并使用新扩展的方法自由检查您的要求!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM