Showing posts with label buffer clear. Show all posts
Showing posts with label buffer clear. Show all posts

Friday, 7 June 2019

Nio: Buffer: clear() : set the current position back to 0


By calling the clear() method on buffer object, you can perform below things in one shot.
a.   Set the current position back to 0.
b.   Set the limit of the buffer to capacity (Capacity of the buffer is set at the time of creation)
c.    Set the mark value to -1 (mark will be undefined state).

Test.java
package com.sample.nio;

import java.nio.ByteBuffer;

public class Test {

 private static final String ALPHABETS = "abcdefghijklmnopqrstuvwxyz";

 private static void printStats(ByteBuffer byteBuffer) {
  int currentPosition = byteBuffer.position();
  int activeElePosition = byteBuffer.limit();
  System.out.println("position : " + currentPosition + ", limit : " + activeElePosition);
  System.out.println("\n");
 }

 public static void main(String args[]) throws Exception {
  /* Define new byte buffer of capacity 11 */
  ByteBuffer byteBuffer = ByteBuffer.allocate(26);
  System.out.println("Before inserting the data into buffer");
  printStats(byteBuffer);

  for (int i = 0; i < ALPHABETS.length(); i++) {
   byteBuffer.put((byte) ALPHABETS.charAt(i));
  }
  System.out.println("After inserting 26 characters into the buffer");
  printStats(byteBuffer);

  byteBuffer.clear();

  System.out.println("After calling clear() api on the buffer");
  printStats(byteBuffer);

  System.out.println("Setting the limit to 10");
  byteBuffer.limit(10);
  printStats(byteBuffer);

  byteBuffer.clear();

  System.out.println("After calling clear() api on the buffer");
  printStats(byteBuffer);

 }
}


Output

Before inserting the data into buffer
position : 0, limit : 26


After inserting 26 characters into the buffer
position : 26, limit : 26


After calling clear() api on the buffer
position : 0, limit : 26


Setting the limit to 10
position : 0, limit : 10


After calling clear() api on the buffer
position : 0, limit : 26




Previous                                                 Next                                                 Home

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