Creates a view of this byte buffer as a double buffer.


 

Creates a view of this byte buffer as a double buffer.

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

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

Creates a view of this byte buffer as a double buffer.

 In this tutorial, we will see how to creates a view of byte buffer as a double 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 Doublebuffer asDoubleBuffer() The asDoubleBuffer() method creates a view of byte buffer as a double 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.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.DoubleBuffer;
public class AsDouble {
  public static void main(String[] args) {
    File fileName = new File("bharat.txt");
    FileOutputStream outStream = null;
    try {
      outStream = new FileOutputStream(fileName, true);
    catch (FileNotFoundException e) {
      System.out.println("File not found Error.");
    }
    ByteBuffer byteBuf = ByteBuffer.allocate(256);
    System.out.println("\nInformation related to byte buffer :");
    System.out
    .printf(
"ByteBuffer Limit =%4d ByteBuffer position =%2d  
ByteBuffer capacity =%4d%n"
,
        byteBuf.limit(), byteBuf.position(), byteBuf.capacity());
    DoubleBuffer doubleBuf = byteBuf.asDoubleBuffer();
    System.out.println("Information related to double buffer :");
    System.out
    .printf(
"DoubleBuffer Limit = %4d DoubleBuffer position =%2d

 DoubleBuffer capacity =%4d%n",
         doubleBuf.limit(),doubleBuf.position(),doubleBuf.capacity());
    try {
      outStream.close();
    catch (IOException ioe) {
      System.out.println("IOException is " + ioe);
    }
  }
}

Output

C:\>java AsDouble
Information related to byte buffer :
ByteBuffer Limit = 256 ByteBuffer position = 0 ByteBuffer capacity = 256
Information related to double buffer :
DoubleBuffer Limit = 32 DoubleBuffer position = 0 DoubleBuffer capacity = 32

Download this code

Ads