put(byte b)
The put(byte b) method of java.nio.ByteBuffer Class is used to write the given byte into the newly created byte buffer at the current position, and then increments the position.
Syntax :
public abstract ByteBuffer put(byte f)
Parameters: This method takes the byte value b as a parameter which is to be written in byte buffer.
Return Value: This method returns this buffer, in which the byte 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(byte b) method:
Example 1:
Java
import java.nio.*;
import java.util.*;
public class GFG {
public static void main(String[] args)
{
int capacity = 3 ;
try {
ByteBuffer bb = ByteBuffer.allocate(capacity);
bb.put(( byte ) 10 )
.put(( byte ) 20 )
.put(( byte ) 30 )
.rewind();
System.out.println("Original ByteBuffer: "
+ Arrays.toString(bb.array()));
}
catch (BufferOverflowException e) {
System.out.println("Exception throws : " + e);
}
catch (ReadOnlyBufferException e) {
System.out.println("Exception throws : " + e);
}
}
}
|
Output:
Original ByteBuffer: [10, 20, 30]
Example 2: To demonstrate BufferOverflowException.
Java
import java.nio.*;
import java.util.*;
public class GFG {
public static void main(String[] args)
{
int capacity = 3 ;
try {
ByteBuffer bb = ByteBuffer.allocate(capacity);
bb.put(( byte ) 10 )
.put(( byte ) 20 )
.put(( byte ) 30 );
System.out.println("Original ByteBuffer: "
+ Arrays.toString(bb.array()));
System.out.println("\nBuffer position : "
+ bb.position());
bb.put(( byte ) 40 );
}
catch (BufferOverflowException e) {
System.out.println("buffer's current position "
+ "is not smaller than its limit");
System.out.println("Exception throws : " + e);
}
catch (ReadOnlyBufferException e) {
System.out.println("Exception throws : " + e);
}
}
}
|
Output:
Original ByteBuffer: [10, 20, 30]
Buffer position : 3
buffer's current position is not smaller than its limit
Exception throws : java.nio.BufferOverflowException
Examples 3: To demonstrate ReadOnlyBufferException.
Java
import java.nio.*;
import java.util.*;
public class GFG {
public static void main(String[] args)
{
int capacity = 3 ;
try {
ByteBuffer bb = ByteBuffer.allocate(capacity);
bb.put(( byte ) 10 )
.put(( byte ) 20 )
.put(( byte ) 30 );
System.out.println("Original ByteBuffer: "
+ Arrays.toString(bb.array()));
ByteBuffer bb1 = bb.asReadOnlyBuffer();
System.out.println("\nTrying to put the byte value"
+ " in read only buffer");
bb1.put(( byte ) 40 );
}
catch (BufferOverflowException e) {
System.out.println("buffer's current position "
+ "is not smaller than its limit");
System.out.println("Exception throws : " + e);
}
catch (ReadOnlyBufferException e) {
System.out.println("Exception throws : " + e);
}
}
}
|
Output:
Original ByteBuffer: [10, 20, 30]
Trying to put the byte value in read only buffer
Exception throws : java.nio.ReadOnlyBufferException
Reference: https://docs.oracle.com/javase/9/docs/api/java/nio/ByteBuffer.html#put-byte-
put(int index, byte f)
The put(int index, byte f) method of java.nio.ByteBuffer Class is used to write the given byte into the buffer at the given index.
Syntax:
public abstract ByteBuffer put(int index, byte f)
Parameters: This method takes the following arguments as a parameter:
- index: The index at which the byte will be written
- f: The byte 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, byte f) method:
Example 1:
Java
import java.nio.*;
import java.util.*;
public class GFG {
public static void main(String[] args)
{
int capacity = 3 ;
try {
ByteBuffer bb = ByteBuffer.allocate(capacity);
bb.put( 0 , ( byte ) 10 );
bb.put( 2 , ( byte ) 20 );
bb.put( 1 , ( byte ) 30 );
bb.rewind();
System.out.println("Original ByteBuffer: "
+ Arrays.toString(bb.array()));
}
catch (IndexOutOfBoundsException e) {
System.out.println("Exception throws : " + e);
}
catch (ReadOnlyBufferException e) {
System.out.println("Exception throws : " + e);
}
}
}
|
Output:
Original ByteBuffer: [10, 30, 20]
Example 2: To demonstrate IndexOutOfBoundsException.
Java
import java.nio.*;
import java.util.*;
public class GFG {
public static void main(String[] args)
{
int capacity = 3 ;
try {
ByteBuffer bb = ByteBuffer.allocate(capacity);
bb.put( 0 , ( byte ) 10 );
bb.put( 2 , ( byte ) 20 );
bb.put( 1 , ( byte ) 30 );
bb.rewind();
System.out.println("Original ByteBuffer: "
+ Arrays.toString(bb.array()));
bb.put(- 1 , ( byte ) 40 );
}
catch (IndexOutOfBoundsException e) {
System.out.println("\nindex is negative or not smaller "
+ "than the buffer's limit");
System.out.println("Exception throws : " + e);
}
catch (ReadOnlyBufferException e) {
System.out.println("Exception throws : " + e);
}
}
}
|
Output:
Original ByteBuffer: [10, 30, 20]
index is negative or not smaller than the buffer's limit
Exception throws : java.lang.IndexOutOfBoundsException
Example 3: To demonstrate ReadOnlyBufferException.
Java
import java.nio.*;
import java.util.*;
public class GFG {
public static void main(String[] args)
{
int capacity = 3 ;
try {
ByteBuffer bb = ByteBuffer.allocate(capacity);
ByteBuffer bb1 = bb.asReadOnlyBuffer();
System.out.println("Trying to put the byte value"
+ " in read only buffer");
bb1.put( 0 , ( byte ) 10 );
}
catch (IndexOutOfBoundsException e) {
System.out.println("Exception throws : " + e);
}
catch (ReadOnlyBufferException e) {
System.out.println("Exception throws : " + e);
}
}
}
|
Output:
Trying to put the byte value in read only buffer
Exception throws : java.nio.ReadOnlyBufferException
Reference: https://docs.oracle.com/javase/9/docs/api/java/nio/ByteBuffer.html#put-int-byte-
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
19 Jan, 2023
Like Article
Save Article