Friday, 7 June 2019

Buffer capacity, size, position, flip and clear methods


capacity, limit and position are the essential properties associated with the buffers.

a. capacity: It represents the number of elements that a buffer can contain. This value never changes through the life cycle. This value is set at the time of buffer creation.

b. limit: It specifies the index of the first element that should not be read or written. It must not greater than the capacity. It simple terms, it represents the number of live elements in the buffer.

c. position: it specifies the index of the next element to be read (or) written. A buffer's position is never negative and is never greater than its limit.

Let me explain with an example.

I am going to create a buffer with capacity 10.


When I define a buffer with capacity 10, then the position is set to 0 and limit, capacity are set to 10th element in the buffer.

Let’s write 3 elements into the buffer.

After writing 3 elements into the buffer, position is moved to 4th element (Since indexes are start from 0, position is pointed to 3rd element).

Let’s write 2 more elements.


After writing 2 more elements into the buffer, position is moved to 5th element (Since indexes are start from 0, position is pointed to 5th element).

Flip the buffer
When you call flip() method on the buffer, it performs below operations.
a.   It sets the limit to the current position.
b.   It sets the position to 0.



Clear the buffer
When you call clear() method on the buffer, it performs below operations.
a.   It sets the position to 0
b.   It set the limit to capacity.


Find the below working application.

Test.java
package com.sample.app;

import java.io.IOException;
import java.nio.ByteBuffer;

public class Test {
 
 private static void printMetaInfo(String message, ByteBuffer byteBuffer) {
  System.out.println(message);
  System.out.printf("Limit : %d\n",byteBuffer.limit());
  System.out.printf("Capacity : %d\n",byteBuffer.capacity());
  System.out.printf("Position : %d\n",byteBuffer.position()); 
 }

 
 public static void main(String... args) throws IOException {
  int bufferCapacity = 10;
  ByteBuffer byteBuffer = ByteBuffer.allocate(bufferCapacity);
  
  String message = String.format("Bytebuffer is initialized with capcity %d", bufferCapacity);
  printMetaInfo(message, byteBuffer);
  
  byteBuffer.put((byte)64);
  byteBuffer.put((byte)65);
  byteBuffer.put((byte)66);
  
  message = String.format("\nInserted %d elements", 3);
  printMetaInfo(message, byteBuffer);
  
  byteBuffer.put((byte)67);
  byteBuffer.put((byte)68);
  
  message = String.format("\nInserted %d more elements", 2);
  printMetaInfo(message, byteBuffer);
  
  byteBuffer.flip();
  message="\nFlipping the buffer";
  printMetaInfo(message, byteBuffer);
  
  byteBuffer.clear();
  message="\nClearning the buffer";
  printMetaInfo(message, byteBuffer);
 }
}


Output

If you can
 keep your
 head when
 all about
 you   
 
   Are los
ing theirs
 and blami
ng it on y
ou,   
If
 you can t
rust yours
elf when a
ll men dou
bt you,
 
   But mak
e allowanc
e for thei
r doubting
 too;   ng


Previous                                                 Next                                                 Home

No comments:

Post a Comment