Open In App

java.nio.DoubleBuffer Class in Java

Improve
Improve
Like Article
Like
Save
Share
Report

DoubleBuffer holds a sequence of double values to be used in an I/O operation. The DoubleBuffer class provides the following four categories of operations upon long buffers:

  • Absolute and relative get method that read single doubles.
  • Absolute and relative put methods that write single doubles.
  • Relative bulk put and get methods that transfer contiguous sequences of doubles from an int array or some other doubles buffer into this buffer and from this buffer into an array.

Short buffers can be created by:

  • allocate(): This allocates space for the buffer’s content.
  • wrap(): This wraps an existing long array into a buffer.

Most of the methods of DoubleBuffer class are directly analogous to the methods defined by ByteBuffer.

Class Declaration:

public abstract class DoubleBuffer

extends Buffer

implements Comparable<DoubleBuffer>

Methods of ShortBuffer class:

Method Description
allocate(int capacity) This method allocates a new double buffer.
array() This method returns the double array that backs this buffer. 
arrayOffset() This method returns the offset within this buffer’s backing array of the first element of the buffer.
asReadOnlyBuffer() This method creates a new, read-only short buffer that shares this buffer’s content.
compact() This method compacts this buffer.
compareTo(DoubleBuffer that) This method compares this buffer to another.
duplicate() This method creates a new double buffer that shares this buffer’s content.
equals(Object ob) This method tells whether this buffer is equal to another object.
get() This method is a relative get method and returns the double at the buffer’s current position.
get(double[] dst) This method is a relative bulk get method and returns this buffer.
get(double[] dst, int offset, int length) This method is a relative bulk get method and this buffer.
get(int index) This method is an absolute get method and this buffer.
hasArray() This method tells whether this buffer is backed by an accessible int array.
hashCode() This method returns the current hash code of this buffer.
isDirect() This method tells whether this int buffer is direct.
order() This method retrieves this buffer’s byte order.
put(double d) This method is a relative put method and returns this buffer.
put(double[] src) This method is a relative bulk put method and returns this buffer.
put(double[] src, int offset, int length) This method is a relative bulk put method and returns this buffer.
put(DoubleBuffer src) This method is a relative bulk put method and returns this buffer.
put(int index, double d) This method is an absolute put method for this buffer.
slice() This method creates a new double buffer whose content is a shared subsequence of this buffer’s content.
toString() This method returns a string summarizing the state of this buffer.
wrap(double[] array) This method wraps a double array into a buffer.
wrap(double[] array, int offset, int length) This method wraps a double array into a buffer.

Following are some programs to demonstrate DoubleBuffer class and its methods:

Example 1:

Java




// Java program to demonstrate
// DoubleBuffer class
  
import java.nio.*;
import java.util.*;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // Declaring the capacity of the DoubleBuffer
        int capacity = 5;
  
        // Creating the DoubleBuffer
  
        // creating object of Doublebuffer
        // and allocating size capacity
        DoubleBuffer db = DoubleBuffer.allocate(capacity);
  
        // putting the value in Doublebuffer
        db.put(9.26F);
        db.put(2, 5.61F);
  
        System.out.println("DoubleBuffer: "
                           + Arrays.toString(db.array()));
    }
}


Output

DoubleBuffer: [9.260000228881836, 0.0, 5.610000133514404, 0.0, 0.0]

Example 2:

Java




// Java program to demonstrate
// DoubleBuffer class
  
import java.nio.*;
import java.util.*;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // Declaire and initialize the float array
        double[] fbb = { 1.25D, 5.34D, 8.56D };
  
        // print the float array length
        System.out.println("Array length : " + fbb.length);
  
        // print the float array element
        System.out.println("\nArray element : "
                           + Arrays.toString(fbb));
  
        // wrap the float array into floatBuffer
        // using wrap() method
        DoubleBuffer doubleBuffer = DoubleBuffer.wrap(fbb);
  
        // Rewind the floatbuffer
        doubleBuffer.rewind();
  
        // print the float buffer
        System.out.println(
            "\ndoubleBuffer : "
            + Arrays.toString(doubleBuffer.array()));
  
        // print the DoubleBuffer capacity
        System.out.println("\ndoublebuffer capacity : "
                           + doubleBuffer.capacity());
  
        // print the DoubleBuffer position
        System.out.println("\ndoublebuffer position: "
                           + doubleBuffer.position());
    }
}


Output

Array length : 3

Array element : [1.25, 5.34, 8.56]

doubleBuffer : [1.25, 5.34, 8.56]

doublebuffer capacity : 3

doublebuffer position: 0


Last Updated : 29 Mar, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads