Related Tags

java
bytebuffer
communitycreator

# What is the ByteBuffer asDoubleBuffer() method in Java?

Talha Ashar

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

The asDoubleBuffer() method of the ByteBuffer class in Java creates a view of a given ByteBuffer object as a DoubleBuffer.

The content of the DoubleBuffer starts from the ByteBuffer's current buffer position. Any changes made to the ByteBuffer are reflected in the DoubleBuffer, and vice versa.

The position, mark, and limit values of the two buffers are independent.

The process is illustrated below.

To use the asDoubleBuffer() method, you will need to include the ByteBuffer class in your program, as shown below.

import java.nio.ByteBuffer


### Syntax

The prototype of the asDoubleBuffer() method is shown below.

public abstract DoubleBuffer asDoubleBuffer()


### Parameters

The asDoubleBuffer() method does not accept any parameters.

### Return value

The asDoubleBuffer() method returns a new DoubleBuffer object which is a view of a specified ByteBuffer object.

### Properties

The properties of the newly created DoubleBuffer object are as follows.

• The current buffer position will be set to $0$.
• The capacity and limit are defined as the number of bytes that remain in the ByteBuffer object divided by $8$.
• The mark value is undefined.
• The DoubleBuffer will be direct if, and only if, the ByteBuffer object is direct. Similarly, the DoubleBuffer will be read-only if, and only if, the ByteBuffer object is read-only.

### Code

The code below shows how the asDoubleBuffer() method can be used in Java.

import java.nio.*;import java.util.*;class asDoubleBufferMethod {  public static void main(String[] args) {    // initialize ByteBuffer instance    ByteBuffer sourceBuffer = ByteBuffer.allocate(24);        // get double buffer view    DoubleBuffer newBuffer = sourceBuffer.asDoubleBuffer();    double values[] = {2d, 6d};    // add values to buffer    for(int i = 0; i < 2; i++)    {      newBuffer.put(values[i]);    }    // set buffer position to first index    newBuffer.rewind();    // print source buffer    System.out.println("The ByteBuffer is: " + Arrays.toString(sourceBuffer.array()));        //print new buffer    double i;    System.out.print("The DoubleBuffer is: [");    while ((i = newBuffer.get()) != 0)    {      System.out.print(i + " ");    }    System.out.print("]");      }}

#### Explanation

First, a ByteBuffer object called sourceBuffer is initialized through the allocate() method. sourceBuffer has the capacity to store $24$ elements.

Next, a DoubleBuffer view of sourceBuffer is created through the asDoubleBuffer() method in line $11$.

The put() method is repeatedly invoked through a for-loop to add values to newBuffer.

All the changes made to newBuffer are also reflected in sourceBuffer. However, since sourceBuffer holds byte elements, each double value added to newBuffer is the equivalent of $8$ byte values added to sourceBuffer.

For example, the addition of the element $2.0$ to newBuffer is reflected by the addition of $4$ elements, i.e., $[64, 0, 0, 0, 0, 0, 0, 0]$, to sourceBuffer.

RELATED TAGS

java
bytebuffer
communitycreator

CONTRIBUTOR

Talha Ashar