Share on Google+Share on Google+

Creates a view of  byte buffer as a int buffer.

In this tutorial you will see how to creates a view of byte buffer as a int buffer.

Creates a view of  byte buffer as a int buffer.

 In this tutorial, we will see how to creates a view of byte buffer as a int 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 IntBuffer asIntBuffer() The asIntBuffer() method creates a view of byte buffer as a int 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.


import java.nio.*;
import java.nio.ByteBuffer;
import java.nio.IntBuffer;

public class AsInt{
  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());
IntBuffer intBuf = byteBuf.asIntBuffer();
System.out.print("Information related to Int buffer :");
System.out.printf("\nIntBuffer Limit = %4d", intBuf.limit());
System.out.printf("\nIntBuffer position = %2d ", intBuf.position());
System.out.printf("\nIntBuffer capacity = %4d%n", intBuf.capacity());


C:\>java AsInt
Information related to byte buffer :
ByteBuffer Limit = 1024
ByteBuffer position = 0
ByteBuffer capacity = 1024
Information related to Int buffer :
IntBuffer Limit = 256
IntBuffer position = 0
IntBuffer capacity = 256

Download this code


Posted on: July 28, 2010 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial 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.


Advertisement null