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

24 Jun, 2019
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 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);        }    }}
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);        }    }}
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);        }    }}
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-
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);        }    }}
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);        }    }}
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);        }    }}
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-
Java-ByteBuffer
Java-Functions
Java-NIO package
Java
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Stream In Java
Different ways of Reading a text file in Java
Constructors in Java
Exceptions in Java
Generics in Java
Functional Interfaces in Java
Comparator Interface in Java with Examples
HashMap get() Method in Java
Introduction to Java
Difference between Abstract Class and Interface in Java