Open In App

DoubleBuffer wrap() method in Java with Examples

Improve
Improve
Like Article
Like
Save
Share
Report

wrap(double[] array)

The wrap() method of java.nio.DoubleBuffer Class is used to wraps a double array into a buffer. The new buffer will be backed by the given double array; that is, modifications to the buffer will cause the array to be modified and vice versa. The new buffer’s capacity and limit will be array.length, its position will be zero, and its mark will be undefined. Its backing array will be the given array, and its array offset will be zero.

Syntax:

public static DoubleBuffer wrap(double[] array)

Parameters: This method takes array, the array that will back this buffer, as a parameter.

Return Value: This method returns the new double buffer.

Below are the examples to illustrate the wrap() method:




// Java program to demonstrate
// wrap() method
  
import java.nio.*;
import java.util.*;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // Declaire and initialize the float array
        double[] fbb = { 1.23D, 2.34D, 4.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.23, 2.34, 4.56]

doubleBuffer : [1.23, 2.34, 4.56]

doublebuffer capacity : 3

doublebuffer position:  0

wrap(double[] array, int offset, int length)

The new buffer will be backed by the given double array; that is, modifications to the buffer will cause the array to be modified and vice versa. The new buffer’s capacity will be array.length, its position will be offset, its limit will be offset + length, and its mark will be undefined. Its backing array will be the given array, and its array offset will be zero.

Syntax:

public static FloatBuffer wrap (double[] array, int offset, int length)

Parameters: This method takes the following parameters:

  • array: The array that will back the new buffer.
  • offset: The offset of the subarray to be used; must be non-negative and no larger than array.length. The new buffer’s position will be set to this value.
  • length: The length of the subarray to be used; must be non-negative and no larger than array.length – offset. The new buffer’s limit will be set to offset + length.

Return Value: This method returns the new double buffer.

Exception: This method throws the IndexOutOfBoundsException, if the preconditions on the offset and length parameters do not hold.

Below are the examples to illustrate the wrap() method:

Examples 1:




// Java program to demonstrate
// wrap() method
  
import java.nio.*;
import java.util.*;
  
public class GFG {
  
    public static void main(String[] args)
    {
  
        // Declare and initialize the float array
        double[] fbb = { 1.23D, 2.34D, 4.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 double array into floatBuffer
        // using wrap() method
        DoubleBuffer doubleBuffer = DoubleBuffer.wrap(fbb, 0,
                                                      fbb.length);
  
        // Rewind the doublebuffer
        doubleBuffer.rewind();
  
        // print the float buffer
        System.out.println("\ndoubleBuffer : "
                           + Arrays.toString(doubleBuffer.array()));
  
        // print the FloatBuffer capacity
        System.out.println("\ndoublebuffer capacity : "
                           + doubleBuffer.capacity());
  
        // print the FloatBuffer position
        System.out.println("\ndoublebuffer position:  "
                           + doubleBuffer.position());
    }
}


Output:

Array length : 3

Array element : [1.23, 2.34, 4.56]

doubleBuffer : [1.23, 2.34, 4.56]

doublebuffer capacity : 3

doublebuffer position:  0

Examples 2: To demonstrate IndexOutOfBoundsException




// Java program to demonstrate
// asReadOnlyBuffer() method
  
import java.nio.*;
import java.util.*;
  
public class GFG {
  
    public static void main(String[] args)
    {
  
        // Declare and initialize the Double array
        double[] fbb = { 1.23D, 2.34D, 4.56D };
  
        // print the Double array length
        System.out.println("Array length : " + fbb.length);
  
        // print the Double array element
        System.out.println("\nArray element : " + Arrays.toString(fbb));
  
        try {
            // wrap the Double array into floatBuffer
            // using wrap() method
            System.out.println("\nHere "
                               + "offset and length does not hold"
                               + " the required condition ");
            DoubleBuffer doubleBuffer = DoubleBuffer.wrap(fbb,
                                                          1,
                                                          fbb.length);
  
            // Rewind the Doublebuffer
            doubleBuffer.rewind();
  
            // print the Double 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());
        }
        catch (IndexOutOfBoundsException e) {
            System.out.println("Exception throws:  " + e);
        }
    }
}


Output:

Array length : 3

Array element : [1.23, 2.34, 4.56]

Here offset and length does not hold the required condition 
Exception throws:  java.lang.IndexOutOfBoundsException


Last Updated : 27 Nov, 2019
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads