简体   繁体   English

Java:I / O,read()不会填充缓冲区?

[英]Java: I/O, read() will not fill buffer?

I am learning about I/O, Files and Sockets and i don't understand the meaning of this sentence 我正在学习I / O,文件和套接字,我不明白这句话的意思

read will not always fill a buffer read不会总是填充缓冲区

What does it mean? 这是什么意思? Anyone has some explanation for me? 有人对我有一些解释吗?

"read will not always fill a buffer"

The above sentence means that Buffer has a certain size which is AutoFlushed when filled, But suppose the data to be read into the Buffer is not enough to fill the Buffer ... Then you need to manually flush it. 上面的句子意味着Buffer有一定的size填充后 自动刷新 但是假设要读入Buffer的数据不足以填充Buffer ...那么你需要手动刷新它。

For futher details read the SCJP Programmer guide by Kathy Sierra or Thinking in Java's IO chapter . 有关详细信息,请阅读Kathy Sierra的SCJP Programmer guideThinking in Java's IO章节中的 Thinking in Java's

The read() method accepts a byte-array that it will fill with from the stream or reader. read()方法接受将从流或读取器填充的字节数组。

If there is not enough data available to fill the buffer, it can either 如果没有足够的数据可用于填充缓冲区,则可以

  • wait until enough data is available 等到有足够的数据可用
  • return immediately but only provide the available data without filling the buffer completely. 立即返回但仅提供可用数据而不完全填充缓冲区。

The standard implementation does a mixtures of both: It waits until at least one byte is available. 标准实现混合了两者:它等待至少有一个字节可用。

Note: The second case implies that read() may return without any data at all. 注意:第二种情况意味着read()可能在没有任何数据的情况下返回。

It will block until at least one byte is available, and return the number of bytes that can be read at that point without blocking again. 它将阻塞,直到至少有一个字节可用,并返回该点可以读取的字节数,而不会再次阻塞。 See the Javadoc. 见Javadoc。

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

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