DataInputStream.read()vs DataInputStream.readFully() [英] DataInputStream.read() vs DataInputStream.readFully()

查看:96
本文介绍了DataInputStream.read()vs DataInputStream.readFully()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Im making a simple TCP/IP Socket app

这样做有什么不同:

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()
那么我可以假设它是一样的吗?

I had a look at the documentation, they have the same exact description. readFully() and read() So can I assume its the same thing?

推荐答案

的Javadoc> DataInput.readFully(byte [] b)状态:


从输入流中读取一些字节并将它们存储到缓冲区
array b 中。 读取的字节数等于 b 的长度。

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.

的Javadoc> DataInputStream.read(byte [] b)状态:


从包含的输入流中读取一些字节数,并将
存储到缓冲区数组 b 中。 实际读取的字节数是以整数形式返回的
。此方法将阻塞,直到输入数据为
可用,检测到文件结尾或抛出异常

基本上, readFully()将读取完全 b.length 字节,而 read()将读取最多 b.length ,或许更少,无论从输入流。

Basically, readFully() will read exactly b.length bytes, whereas read() will read up to b.length, maybe less, whatever is available from the input stream.

这篇关于DataInputStream.read()vs DataInputStream.readFully()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆