gpt4 book ai didi

java - DataInputStream.read() 与 DataInputStream.readFully()

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:43:22 28 4
gpt4 key购买 nike

我正在制作一个简单的 TCP/IP 套接字应用

这样做有什么不同:

DataInputStream in = new DataInputStream(clientSocket.getInputStream());

byte[] buffer = new byte[100];
in.readFully(buffer);

与这样做相比:

DataInputStream in = new DataInputStream(clientSocket.getInputStream());

byte[] buffer = new byte[100];
in.read(buffer);

我查看了文档,它们具有完全相同的描述。 readFully()read()那么我可以假设它是同一件事吗?

最佳答案

DataInput.readFully(byte[] b) 的 Javadoc 指出:

Reads some bytes from an input stream and stores them into the buffer array b. The number of bytes read is equal to the length of b.

DataInputStream.read(byte[] b) 的 Javadoc 指出:

Reads some number of bytes from the contained input stream and stores them into the buffer array b. The number of bytes actually read is returned as an integer. This method blocks until input data is available, end of file is detected, or an exception is thrown.

基本上,readFully() 将读取 exactly b.length 字节,而 read() 将读取最多 b.length,可能更少,无论输入流中可用什么。

关于java - DataInputStream.read() 与 DataInputStream.readFully(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25897627/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com