Popokoko
Popokoko

Reputation: 6543

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

read will not always fill a buffer

What does it mean? Anyone has some explanation for me?

Upvotes: 1

Views: 530

Answers (3)

user207421
user207421

Reputation: 311023

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.

Upvotes: 1

Hendrik Brummermann
Hendrik Brummermann

Reputation: 8312

The read() method accepts a byte-array that it will fill with from the stream or reader.

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.

Upvotes: 1

Kumar Vivek Mitra
Kumar Vivek Mitra

Reputation: 33544

"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.

For futher details read the SCJP Programmer guide by Kathy Sierra or Thinking in Java's IO chapter.

Upvotes: 2

Related Questions