IntBuffer put() methods in Java | Set 1
Last Updated :
20 Sep, 2018
put(int i)
The put(int i) method of java.nio.IntBuffer Class is used to write the given int into the newly created int buffer at the current position, and then increments the position.
Syntax :
public abstract IntBuffer put(int i)
Parameters: This method takes the int value i as a parameter which is to be written in int buffer.
Return Value: This method returns this buffer, in which the int 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(int i) method:
Example 1:
import java.nio.*;
import java.util.*;
public class GFG {
public static void main(String[] args)
{
int capacity = 3 ;
try {
IntBuffer ib = IntBuffer.allocate(capacity);
ib.put( 8 );
ib.put( 9 );
ib.put( 7 );
ib.rewind();
System.out.println( "Original IntBuffer: "
+ Arrays.toString(ib.array()));
}
catch (BufferOverflowException e) {
System.out.println( "Exception throws : " + e);
}
catch (ReadOnlyBufferException e) {
System.out.println( "Exception throws : " + e);
}
}
}
|
Output:
Original IntBuffer: [8, 9, 7]
Example 2: To demonstrate BufferOverflowException.
import java.nio.*;
import java.util.*;
public class GFG {
public static void main(String[] args)
{
int capacity = 3 ;
try {
IntBuffer ib = IntBuffer.allocate(capacity);
ib.put( 8 );
ib.put( 9 );
ib.put( 7 );
System.out.println( "Trying to put the Int at the "
+ "position more than its limit" );
ib.put( 7 );
ib.rewind();
System.out.println( "Original IntBuffer: "
+ Arrays.toString(ib.array()));
}
catch (BufferOverflowException e) {
System.out.println( "Exception throws : " + e);
}
catch (ReadOnlyBufferException e) {
System.out.println( "Exception throws : " + e);
}
}
}
|
Output:
Trying to put the Int 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 {
IntBuffer ib = IntBuffer.allocate(capacity);
IntBuffer ib1 = ib.asReadOnlyBuffer();
System.out.println( "Trying to put the Int value"
+ " in read only buffer" );
ib1.put( 8 );
}
catch (BufferOverflowException e) {
System.out.println( "Exception throws : " + e);
}
catch (ReadOnlyBufferException e) {
System.out.println( "Exception throws : " + e);
}
}
}
|
Output:
Trying to put the Int value in read only buffer
Exception throws : java.nio.ReadOnlyBufferException
put(int index, int i)
The put(int index, int i) method of java.nio.IntBuffer Class is used to write the given int into the buffer at the given index.
Syntax:
public abstract IntBuffer put(int index, int i)
Parameters: This method takes the following arguments as a parameter:
- index: The index at which the int will be written
- i: The int value to be written
Return Value: This method returns 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, int i) method:
Example 1:
import java.nio.*;
import java.util.*;
public class GFG {
public static void main(String[] args)
{
int capacity = 3 ;
try {
IntBuffer ib = IntBuffer.allocate(capacity);
ib.put( 0 , 8 );
ib.put( 2 , 9 );
ib.put( 1 , 7 );
ib.rewind();
System.out.println( "Original IntBuffer: "
+ Arrays.toString(ib.array()));
}
catch (IndexOutOfBoundsException e) {
System.out.println( "Exception throws : " + e);
}
catch (ReadOnlyBufferException e) {
System.out.println( "Exception throws : " + e);
}
}
}
|
Output:
Original IntBuffer: [8, 7, 9]
Example 2: To demonstrate IndexOutOfBoundsException.
import java.nio.*;
import java.util.*;
public class GFG {
public static void main(String[] args)
{
int capacity = 3 ;
try {
IntBuffer ib = IntBuffer.allocate(capacity);
ib.put( 0 , 8 );
ib.put( 2 , 9 );
System.out.println( "Trying to put the value"
+ " at the negative index" );
ib.put(- 1 , 7 );
}
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 {
IntBuffer ib = IntBuffer.allocate(capacity);
IntBuffer ib1 = ib.asReadOnlyBuffer();
System.out.println( "Trying to put the Int value"
+ " in read only buffer" );
ib1.put( 0 , 8 );
}
catch (BufferOverflowException e) {
System.out.println( "Exception throws : " + e);
}
catch (ReadOnlyBufferException e) {
System.out.println( "Exception throws : " + e);
}
}
}
|
Output:
Trying to put the Int value in read only buffer
Exception throws : java.nio.ReadOnlyBufferException
Similar Reads
IntBuffer get() methods in Java | Set 1
get() The get() method of java.nio.IntBuffer Class is used to reads the int at the given buffer's current position, and then increments the position. Syntax : public abstract int get() Return Value: This method returns the int value at the buffer's current position. Throws: This method throws Buffer
5 min read
IntBuffer slice() method in Java
The slice() method of java.nio.IntBuffer Class is used to creates a new int buffer whose content is a shared subsequence of the given buffer's content. The content of the new buffer will start at this buffer's current position. Changes to this buffer's content will be visible in the new buffer, and
3 min read
IntBuffer wrap() method in Java
wrap(int[] array) The wrap() method of java.nio.IntBuffer Class is used to wrap a int array into a buffer. The new buffer will be backed by the given int 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.
4 min read
IntBuffer array() method in Java
The array() method of java.nio.IntBuffer Class is used to Return the int array that backs this buffer. Modifications to this buffer's content will cause the returned array's content to be modified, and vice versa. Invoke() the hasArray() method are used before invoking this method in order to ensure
3 min read
IntBuffer equals() method in Java
The equals() method of java.nio.IntBuffer Class is used to check whether or not the given buffer is equal to another object. Two int buffers are equal if, and only if, They have the same element type, They have the same number of remaining elements, and The two sequences of remaining elements, consi
4 min read
IntBuffer compact() method in Java
The compact() method of java.nio.IntBuffer Class is used to compact the given buffer. The values between the buffer's current position and its limit are copied to the beginning of the buffer. The buffer's position is then set to n+1 and its limit is set to its capacity. The buffer's position is set
3 min read
IntBuffer allocate() method in Java
The allocate() method of java.nio.IntBuffer Class is used to allocate a new int buffer next to the existing buffer. The new buffer's position will be zero. Its limit will be its capacity. Its mark will be undefined. And each of its elements will be initialized to zero. It will have a backing array,
2 min read
IntBuffer hasArray() method in Java
The hasArray() method of java.nio.IntBuffer class is used to ensure whether or not the given buffer is backed by an accessible int array. It returns true if there is an accessible backing array to this buffer, else it returns false. If this method returns true, then the array() and arrayOffset() met
2 min read
IntBuffer compareTo() method in Java
The compareTo() method of java.nio.IntBuffer class is used to compare one buffer to another. Two int buffers are compared by comparing their sequences of remaining elements lexicographically, without regard to the starting position of each sequence within its corresponding buffer. Pairs of int eleme
4 min read
CharBuffer put() methods in Java
put(char i) The put(char i) method of java.nio.CharBuffer Class is used to write the given character into the newly created char buffer at the current position and then increments the position. Syntax : public abstract CharBuffer put(char i) Parameters: This method takes the char value i as a parame
6 min read