Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

ByteBuffer put() methods in Java with Examples | Set -1

  • Last Updated : 24 Jun, 2019

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 :

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

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 program to demonstrate
// put() method
  
import java.nio.*;
import java.util.*;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // Declaring the capacity of the ByteBuffer
        int capacity = 3;
  
        // Creating the ByteBuffer
        try {
  
            // creating object of ByteBuffer
            // and allocating size capacity
            ByteBuffer bb = ByteBuffer.allocate(capacity);
  
            // putting the value in ByteBuffer using put() method
            bb.put((byte)10)
                .put((byte)20)
                .put((byte)30)
                .rewind();
  
            // print the ByteBuffer
            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 program to demonstrate
// put() method
  
import java.nio.*;
import java.util.*;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // Declaring the capacity of the ByteBuffer
        int capacity = 3;
  
        // Creating the ByteBuffer
        try {
  
            // creating object of ByteBuffer
            // and allocating size capacity
            ByteBuffer bb = ByteBuffer.allocate(capacity);
  
            // putting the value in ByteBuffer using put() method
            bb.put((byte)10)
                .put((byte)20)
                .put((byte)30);
  
            // print the ByteBuffer
            System.out.println("Original ByteBuffer:  "
                               + Arrays.toString(bb.array()));
  
            // again putting the value in ByteBuffer
            // using put() method
            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 program to demonstrate
// put() method
  
import java.nio.*;
import java.util.*;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // Declaring the capacity of the ByteBuffer
        int capacity = 3;
  
        // Creating the ByteBuffer
        try {
  
            // creating object of ByteBuffer
            // and allocating size capacity
            ByteBuffer bb = ByteBuffer.allocate(capacity);
  
            // putting the value in ByteBuffer using put() method
            bb.put((byte)10)
                .put((byte)20)
                .put((byte)30);
  
            // print the ByteBuffer
            System.out.println("Original ByteBuffer:  "
                               + Arrays.toString(bb.array()));
  
            // Creating a read-only copy of ByteBuffer
            // using asReadOnlyBuffer() method
            ByteBuffer bb1 = bb.asReadOnlyBuffer();
  
            System.out.println("\nTrying to put the byte value"
                               + " in read only buffer");
  
            // putting the value in readonly ByteBuffer
            // using put() method
            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 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, byte f) method:

Example 1:




// Java program to demonstrate
// put() method
  
import java.nio.*;
import java.util.*;
  
public class GFG {
  
    public static void main(String[] args)
    {
  
        // Declaring the capacity of the ByteBuffer
        int capacity = 3;
  
        // Creating the ByteBuffer
        try {
  
            // creating object of ByteBuffer
            // and allocating size capacity
            ByteBuffer bb = ByteBuffer.allocate(capacity);
  
            // putting the value in ByteBuffer using put() at  index 0
            bb.put(0, (byte)10);
  
            // putting the value in ByteBuffer using put() at  index 2
            bb.put(2, (byte)20);
  
            // putting the value in ByteBuffer using put() at  index 1
            bb.put(1, (byte)30);
  
            // rewinding the ByteBuffer
            bb.rewind();
  
            // print the ByteBuffer
            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 program to demonstrate
// put() method
  
import java.nio.*;
import java.util.*;
  
public class GFG {
  
    public static void main(String[] args)
    {
  
        // Declaring the capacity of the ByteBuffer
        int capacity = 3;
  
        // Creating the ByteBuffer
        try {
  
            // creating object of ByteBuffer
            // and allocating size capacity
            ByteBuffer bb = ByteBuffer.allocate(capacity);
  
            // putting the value in ByteBuffer using put() at  index 0
            bb.put(0, (byte)10);
  
            // putting the value in ByteBuffer using put() at  index 2
            bb.put(2, (byte)20);
  
            // putting the value in ByteBuffer using put() at  index 1
            bb.put(1, (byte)30);
  
            // rewinding the ByteBuffer
            bb.rewind();
  
            // print the ByteBuffer
            System.out.println("Original ByteBuffer:  "
                               + Arrays.toString(bb.array()));
  
            // putting the value in ByteBuffer using put() at  index -1
            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 program to demonstrate
// put() method
  
import java.nio.*;
import java.util.*;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // Declaring the capacity of the ByteBuffer
        int capacity = 3;
  
        // Creating the ByteBuffer
        try {
  
            // creating object of ByteBuffer
            // and allocating size capacity using allocate() method
            ByteBuffer bb = ByteBuffer.allocate(capacity);
  
            // Creating a read-only copy of ByteBuffer
            // using asReadOnlyBuffer() method
            ByteBuffer bb1 = bb.asReadOnlyBuffer();
  
            System.out.println("Trying to put the byte value"
                               + " in read only buffer");
  
            // putting the value in readonly ByteBuffer
            // using put() method
            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-




My Personal Notes arrow_drop_up
Recommended Articles
Page :