Home Tutorial Java Corejava Nio Creates a view of  byte buffer as a long buffer.

 
 

Creates a view of  byte buffer as a long buffer.
Posted on: July 28, 2010 at 12:00 AM
Advertisement
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

Advertisement

Related Tags for Creates a view of  byte buffer as a long buffer.:


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: July 28, 2010

Recommend the tutorial

Advertisements Advertisements
 

 

 

DMCA.com