the nio buffer class has two fields:

A buffer's capacity is the number of elements it contains. The capacity of a buffer is never negative and never changes.

A buffer's limit is the index of the first element that should not be read or written. A buffer's limit is never negative and is never greater than its capacity.

what's the use of the limit ?

有帮助吗?

解决方案

A Buffer has three properties:

  1. capacity
  2. position
  3. limit

The meaning of position and limit depends on whether the Buffer is in read or write mode.

In write mode the limit of a Buffer is the limit of how much data you can write into the buffer. In write mode the limit is equal to the capacity of the Buffer.

When flipping the Buffer into read mode, limit means the limit of how much data you can read from the data.

其他提示

When you are writing to a buffer, the limit is the size of the buffer. But when you flip the buffer over to read from it, the limit is how much data was written to the buffer. There's a tutorial on it here: http://tutorials.jenkov.com/java-nio/buffers.html#capacity-position-limit

The limit is the current amount of data in the buffer, or the end of the free space, depending on whether you're writing or reading. The capacity is the maximum amount of a data the buffer can contain.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top