DoubleBuffer put() methods in Java with Examples | Set 1
Last Updated :
04 Dec, 2018
put(double f)
The put(double f) method of java.nio.DoubleBuffer Class is used to write the given double into the newly created double buffer at the current position, and then increments the position.
Syntax:
public abstract DoubleBuffer put(double f)
Parameters: This method takes the double value f as a parameter which is to be written in double buffer.
Return Value: This method returns this buffer, in which the double value is inserted.
Exception: This method throws the following exceptions:
- BufferOverflowException– If this buffer’s current position is not smaller than its limit
- ReadOnlyBufferException– If this buffer is read-only
Below are the examples to illustrate the put(double f) method:
Example 1:
import java.nio.*;
import java.util.*;
public class GFG {
public static void main(String[] args)
{
int capacity = 3 ;
try {
DoubleBuffer db = DoubleBuffer.allocate(capacity);
db.put( 8 .56D);
db.put( 9 .61D);
db.put( 7 .86D);
db.rewind();
System.out.println( "Original DoubleBuffer: "
+ Arrays.toString(db.array()));
}
catch (BufferOverflowException e) {
System.out.println( "Exception throws : " + e);
}
catch (ReadOnlyBufferException e) {
System.out.println( "Exception throws : " + e);
}
}
}
|
Output:
Original DoubleBuffer: [8.56, 9.61, 7.86]
Example 2: To demonstrate BufferOverflowException.
import java.nio.*;
import java.util.*;
public class GFG {
public static void main(String[] args)
{
int capacity = 3 ;
try {
DoubleBuffer db = DoubleBuffer.allocate(capacity);
db.put( 8 .56F);
db.put( 9 .61F);
db.put( 7 .86F);
System.out.println( "Trying to put the Double at the "
+ "position more than its limit" );
db.put( 7 .86F);
db.rewind();
System.out.println( "Original DoubleBuffer: "
+ Arrays.toString(db.array()));
}
catch (BufferOverflowException e) {
System.out.println( "Exception throws : " + e);
}
catch (ReadOnlyBufferException e) {
System.out.println( "Exception throws : " + e);
}
}
}
|
Output:
Trying to put the Double at the position more than its limit
Exception throws : java.nio.BufferOverflowException
Examples 3: To demonstrate ReadOnlyBufferException.
import java.nio.*;
import java.util.*;
public class GFG {
public static void main(String[] args)
{
int capacity = 3 ;
try {
DoubleBuffer db = DoubleBuffer.allocate(capacity);
DoubleBuffer db1 = db.asReadOnlyBuffer();
System.out.println( "Trying to put theDouble value"
+ " in read only buffer" );
db1.put( 8 .56F);
}
catch (BufferOverflowException e) {
System.out.println( "Exception throws : " + e);
}
catch (ReadOnlyBufferException e) {
System.out.println( "Exception throws : " + e);
}
}
}
|
Output:
Trying to put theDouble value in read only buffer
Exception throws : java.nio.ReadOnlyBufferException
put(int index, double f)
The put(int index, double f) method of java.nio.DoubleBuffer Class is used to write the given double into the buffer at the given index.
Syntax:
public abstract DoubleBuffer put(int index, double f)
Parameters: This method takes the following arguments as a parameter:
- index: The index at which the double will be written
- f: The double value to be written
Return Value: This method returns the this buffer.
Exception: This method throws the following exception:
- IndexOutOfBoundsException– If index is negative or not smaller than the buffer’s limit
- ReadOnlyBufferException– If this buffer is read-only
Below are the examples to illustrate the put(int index, double f) method:
Example 1:
import java.nio.*;
import java.util.*;
public class GFG {
public static void main(String[] args)
{
int capacity = 3 ;
try {
DoubleBuffer db = DoubleBuffer.allocate(capacity);
db.put( 0 , 8 .56F);
db.put( 2 , 9 .61F);
db.put( 1 , 7 .86F);
db.rewind();
System.out.println( "Original DoubleBuffer: "
+ Arrays.toString(db.array()));
}
catch (IndexOutOfBoundsException e) {
System.out.println( "Exception throws : " + e);
}
catch (ReadOnlyBufferException e) {
System.out.println( "Exception throws : " + e);
}
}
}
|
Output:
Original DoubleBuffer: [8.5600004196167, 7.860000133514404, 9.609999656677246]
Example 2: To demonstrate IndexOutOfBoundsException.
import java.nio.*;
import java.util.*;
public class GFG {
public static void main(String[] args)
{
int capacity = 3 ;
try {
DoubleBuffer db = DoubleBuffer.allocate(capacity);
db.put( 0 , 8 .56F);
db.put( 2 , 9 .61F);
System.out.println( "Trying to put the value"
+ " at the negative index" );
db.put(- 1 , 7 .86F);
}
catch (IndexOutOfBoundsException e) {
System.out.println( "Exception throws : " + e);
}
catch (ReadOnlyBufferException e) {
System.out.println( "Exception throws : " + e);
}
}
}
|
Output:
Trying to put the value at the negative index
Exception throws : java.lang.IndexOutOfBoundsException
Example 3: To demonstrate ReadOnlyBufferException.
import java.nio.*;
import java.util.*;
public class GFG {
public static void main(String[] args)
{
int capacity = 3 ;
try {
DoubleBuffer db = DoubleBuffer.allocate(capacity);
DoubleBuffer db1 = db.asReadOnlyBuffer();
System.out.println( "Trying to put the Double value"
+ " in read only buffer" );
db1.put( 0 , 8 .56F);
}
catch (BufferOverflowException e) {
System.out.println( "Exception throws : " + e);
}
catch (ReadOnlyBufferException e) {
System.out.println( "Exception throws : " + e);
}
}
}
|
Output:
Trying to put the Double value in read only buffer
Exception throws : java.nio.ReadOnlyBufferException