简体   繁体   English

当参数中已提供字节数组对象时,为什么某些Java函数需要字节数组长度?

[英]Why some Java functions requires bytes array length when byte arrays object was already provided in the argument?

While writing Java code, I really wonder why some functions require byte arrays length as an argument when the first argument was byte arrays object. 在编写Java代码时,我真的想知道为什么当第一个参数是字节数组对象时,某些函数为什么需要字节数组长度作为参数。 Why they don't get the length from the object provided? 为什么它们没有从提供的对象中得到长度?

For example: 例如:

// E.g.: 1. Bitmap
byte[] bytes = task.getResult();
Bitmap bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);    

// E.g.: 2. Datagram
byte[] data = new byte[1024];
DatagramPacket request = new DatagramPacket(data, data.length);

If they want the length, why they don't use data.length? 如果他们想要长度,为什么不使用data.length?

The byte array is a buffer to which data, the length of which is less than the length of the buffer, is read. 字节数组是一个缓冲区,可读取长度小于缓冲区长度的数据。 The length parameter defines the amount of bytes in the buffer that are relevant. length参数定义缓冲区中相关的字节数。 You're not supposed to pass the length of the buffer in the parameter, that would be redundant. 您不应该在参数中传递缓冲区的长度,那将是多余的。 You're supposed to pass the number of bytes in the buffer that contain actual data. 您应该传递缓冲区中包含实际数据的字节数。

The API documentation of DatagramPacket, for example, reveals this. 例如,DatagramPacket的API文档对此进行了说明。

length - the number of bytes to read

The simple answer is: most read methods (in Java, and any other language) that operate on buffer arrays have to tell you the exact number of bytes that were actually read. 简单的答案是:大多数对缓冲区数组进行操作的读取方法(使用Java和任何其他语言)必须告诉您实际读取的确切字节数。

Keep in mind: that array is an buffer. 请记住:该数组是一个缓冲区。 The default behavior is that buffer.length or less bytes can be read. 默认行为是可以读取buffer.length或更少的字节。 So, knowing how long the buffer is doesn't help you. 因此,知道缓冲区有多长时间对您没有帮助。 You have to know how many bytes were actually put into the buffer. 您必须知道实际上将多少字节放入缓冲区。

Broadly a buffer is used as a temporary data in a data loading processing. 在数据加载处理中,广泛地将缓冲区用作临时数据。
You fill the buffer until its size or less but never more than its capacity of course. 您将缓冲区填满,直到其大小或更小,但是当然不会超过其容量。
The DatagramPacket javadoc confirms that : DatagramPacket javadoc确认:

The length argument must be less than or equal to buf.length. length参数必须小于或等于buf.length。

And a thing that you don't have to forget : conceptually you use a buffer because the data has to be progressively loaded or only a specific part of that. 还有一件您不必忘记的事情:从概念上讲,您使用缓冲区是因为必须逐步加载数据或仅其中的特定部分。
In some cases you will read as much data as its maximal capacity but in some other cases you need to read only the X first bytes or the bytes from X to Y offset. 在某些情况下,您将读取与其最大容量一样多的数据,但在其他情况下,您只需要读取X个前字节或X到Y偏移量的字节。
So the buffer class methods provide generally multiple way to read from the buffer. 因此,缓冲区类方法通常提供多种从缓冲区读取的方法。
Such as : 如 :

public DatagramPacket(byte buf[], int length);
public DatagramPacket(byte buf[], int offset, int length);

Now conceptually you are not wrong, sometimes you want to fill the whole buffer because you know that you will need to read exactly this size of data. 现在从概念上讲,您并没有错,有时您想填充整个缓冲区,因为您知道将需要精确地读取此大小的数据。 The java.net.DatagramSocket confirms that : java.net.DatagramSocket确认:

public synchronized void receive(DatagramPacket p) throws IOException {
...
   tmp = new DatagramPacket(new byte[1024], 1024);
...
}

So an additional overloading such as : 因此,还有一个额外的重载,例如:

public DatagramPacket(byte buf[]);

would make sense. 会很有意义。

Because the data that you want to read can be less than or equal to byte[] buf 's length. 因为您要读取的数据可以小于或等于 byte[] buf的长度。

Below is the API documentation : 以下是API文档:

public DatagramPacket(byte[] buf,
              int length)

Constructs a DatagramPacket for receiving packets of length length.

The length argument must be less than or equal to buf.length.

Parameters:
    buf - buffer for holding the incoming datagram.
    length - the number of bytes to read.

https://docs.oracle.com/javase/7/docs/api/java/net/DatagramPacket.html https://docs.oracle.com/javase/7/docs/api/java/net/DatagramPacket.html

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

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