How are Java ByteBuffer's limit and position variable's updated?
- by Dummy Derp
There are two scenarios: writing and reading
Writing:
Whenever I write something to the ByteBuffer by calling its put(byte[]) method the position variable is incremented as: current position + size of byte[] and limit stays at the max.
If, however, I put the data in a view buffer then I will have to, manually, calculate and update the position
Before I call the write(ByteBuffer) method of the channel to write something, I will have to flip() the Bytebuffer so that
position points to zero and limit points to the last byte that was written to the ByteBuffer.
Reading:
Whenever I call the read(ByteBuffer) method of a channel to read something, the position variable stays at 0 and the limit variable of the ByteBuffer points to the last byte that was read. So, if the ByteBuffer is smaller than the file being read, the limit variable is pushed to max
This means that the ByteBuffer is already flipped and I can proceed to extracting the values from the ByteBuffer.
Please, correct me where I am wrong :)