我在看inputstream对象的read方法的定义,我对每次读取多少数据感到困惑,因为它说“读取一些字节”
public int read(byte[] b)
throws IOException
Reads some number of bytes from the 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.
假设我有一个大小为 200 的缓冲区数组,输入流中的数据为 100 字节。是否保证 inputStream.read 获取全部 100 个字节?
最佳答案
这个接口(interface)(或者更准确地说:抽象类)的全部要点:您可以绝对不依赖假设读取了多少字节。您总是总是总是必须检查该方法的返回值才能知道。
背景:这个接口(interface)有很多不同的实现。有些是我的缓冲区,有些可能不是。有些读取“固定”输入(可能来自内存中的现有数据)。有人可能会决定给你一个转向互联网的流,下载一个 10 GB 的文件,然后开始一个字节一个字节地发送给你。
你唯一知道的是:方法返回
the total number of bytes read into the buffer
故事结束。
关于java - java中inputstream.read读取了多少数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45837998/