Friday, 7 June 2019

Nio: Reading data from buffers


All the Buffer subclasses provide get() methods to read the data from the buffer.

For example, ByteBuffer class provides below methods to read the data from ByteBuffer instance.

public abstract byte get()
Reads the byte at this buffer's current position, and then increments the position.

public abstract byte get(int index)
Reads the byte at the given index.

public ByteBuffer get(byte[] dst, int offset, int length)
Copies length bytes from this buffer into the given array, starting at the current position of this buffer and at the given offset in the array. The position of the buffer is incremented by length.

src.get(dst, off, len)

for (int i = off; i < off + len; i++)
         dst[i] = src.get();
        
public ByteBuffer get(byte[] dst)
Copies the dst.length bytes from current position of the buffer.

Let’s see it by an example.

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 the data into buffer");
  printStats(byteBuffer);
  
  System.out.println("resetting the byte buffer");
  byteBuffer.clear();
  printStats(byteBuffer);
  
  /* Reseting the position back to 0 */
  byte element = byteBuffer.get();
  System.out.println("Element at index 0 is " + element);
  printStats(byteBuffer);

  byte[] dst = new byte[10];
  byteBuffer.get(dst); // pos = 11;
  System.out.println("Characters from 1 to 10 : " + new String(dst));
  printStats(byteBuffer);

  // Replace last five characters with byte buffer next five characters
  byteBuffer.get(dst, 5, 5); // pos = 16
  System.out.println("Replacing the last five charcters of previous output : " + new String(dst));
  printStats(byteBuffer);

  element = byteBuffer.get(23);
  System.out.println("Element at 23rd position : " + element);
  printStats(byteBuffer);

 }
}

Output

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


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


resetting the byte buffer
position : 0, limit : 26


Element at index 0 is 97
position : 1, limit : 26


Characters from 1 to 10 : bcdefghijk
position : 11, limit : 26


Replacing the last five charcters of previous output : bcdeflmnop
position : 16, limit : 26


Element at 23rd position : 120
position : 16, limit : 26




Previous                                                 Next                                                 Home

No comments:

Post a Comment