Creates a view of  byte buffer as a long buffer.


 

Creates a view of  byte buffer as a long buffer.

In this tutorial you will see how to create a view of byte buffer as a long buffer.

In this tutorial you will see how to create a view of byte buffer as a long buffer.

Creates a view of  byte buffer as a long buffer.

 In this tutorial, we will see how to create a view of byte buffer as a long buffer.

ByteBuffer API:

The java.nio.ByteBuffer class extends java.nio.Buffer class. It provides the following methods:

Return type Method Description
static ByteBuffer allocate(int capacity)  The allocate(..)method allocate a new byte buffer.
abstract LongBuffer asLongBuffer() The asLongBuffer() method creates a view of byte buffer as a long buffer.
int limit() The limit() method returns the limit of associated buffer.
int position() The position() method returns the position of associated buffer.
final int capacity() The capacity() method returns the capacity of associated buffer.

code

import java.nio.*;
import java.nio.ByteBuffer;
import java.nio.LongBuffer;
public class AsLong {
public static void main(String[] args) {
ByteBuffer byteBuf = ByteBuffer.allocate(1024);
System.out.print("\nInformation related to byte buffer :");
System.out.printf("\nByteBuffer Limit = %4d", byteBuf.limit());
System.out.printf("\nByteBuffer position = %2d ", byteBuf.position());
System.out.printf("\nByteBuffer capacity = %4d%n", byteBuf.capacity());
LongBuffer longBuf = byteBuf.asLongBuffer();
System.out.print("Information related to long buffer :");
System.out.printf("\nLongBuffer Limit = %4d", longBuf.limit());
System.out.printf("\nLongBuffer position = %2d ",longBuf.position());
System.out.printf("\nLongBuffer capacity = %4d%n", longBuf.capacity());
  }
}

Output

C:\>java AsLong
Information related to byte buffer :
ByteBuffer Limit = 1024
ByteBuffer position = 0
ByteBuffer capacity = 1024
Information related to long buffer :
LongBuffer Limit = 128
LongBuffer position = 0
LongBuffer capacity = 128

Download this code

Ads