Open In App

LongBuffer put() methods in Java

Last Updated : 19 Sep, 2018
Improve
Improve
Like Article
Like
Save
Share
Report

put(Long i)

The put(Long i) method of java.nio.LongBuffer Class is used to write the given Long value into the newly created Long buffer at the current position, and then increments the position.

Syntax:

public abstract LongBuffer put(Long i)

Parameters: This method takes the Long value i as a parameter which is to be written in Long buffer.

Return Value: This method returns this buffer, in which the Long 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(Long i) 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 LongBuffer
        int capacity = 3;
  
        // Creating the LongBuffer
        try {
  
            // creating object of Longbuffer
            // and allocating size capacity
            LongBuffer ib = LongBuffer.allocate(capacity);
  
            // putting the value in Longbuffer using put() method
            ib.put(8);
            ib.put(9);
            ib.put(7);
            ib.rewind();
  
            // prLong the LongBuffer
            System.out.println("Original LongBuffer:  "
                               + Arrays.toString(ib.array()));
        }
  
        catch (BufferOverflowException e) {
            System.out.println("Exception throws : " + e);
        }
  
        catch (ReadOnlyBufferException e) {
            System.out.println("Exception throws : " + e);
        }
    }
}


Output:

Original LongBuffer:  [8, 9, 7]

Example 2: To demonstrate BufferOverflowException.




// Java program to demonstrate
// put() method
  
// 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 LongBuffer
        int capacity = 3;
  
        // Creating the LongBuffer
        try {
  
            // creating object of Longbuffer
            // and allocating size capacity
            LongBuffer ib = LongBuffer.allocate(capacity);
  
            // putting the value in Longbuffer using put() method
            ib.put(8);
            ib.put(9);
            ib.put(7);
  
            System.out.println("Trying to put the Long at the "
                               + "position more than its limit");
            ib.put(7);
  
            // rewind the Longbuffer
            ib.rewind();
  
            // prLong the LongBuffer
            System.out.println("Original LongBuffer:  "
                               + 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 Long at the position more 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 LongBuffer
        int capacity = 3;
  
        // Creating the LongBuffer
        try {
  
            // creating object of Longbuffer
            // and allocating size capacity using allocate() method
            LongBuffer ib = LongBuffer.allocate(capacity);
  
            // Creating a read-only copy of LongBuffer
            // using asReadOnlyBuffer() method
            LongBuffer ib1 = ib.asReadOnlyBuffer();
  
            System.out.println("Trying to put the Long value"
                               + " in read only buffer");
  
            // putting the value in readonly Longbuffer
            // using put() method
            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 Long value in read only buffer
Exception throws : java.nio.ReadOnlyBufferException

put(Long index, Long i)

The put(Long index, Long i) method of java.nio.LongBuffer Class is used to write the given Long value the buffer at the given index.

Syntax:

public abstract LongBuffer put(Long index, Long i)

Parameters: This method takes the following arguments as a parameter:

  • index: The index at which the Long will be written
  • i: The Long 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(Long index, Long i) 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 LongBuffer
        int capacity = 3;
  
        // Creating the LongBuffer
        try {
  
            // creating object of Longbuffer
            // and allocating size capacity
            LongBuffer ib = LongBuffer.allocate(capacity);
  
            // putting the value in Longbuffer using put() at  index 0
            ib.put(0, 8);
  
            // putting the value in Longbuffer using put() at  index 2
            ib.put(2, 9);
  
            // putting the value in Longbuffer using put() at  index 1
            ib.put(1, 7);
  
            // rewinding the Longbuffer
            ib.rewind();
  
            // prLong the LongBuffer
            System.out.println("Original LongBuffer:  "
                               + Arrays.toString(ib.array()));
        }
  
        catch (IndexOutOfBoundsException e) {
            System.out.println("Exception throws : " + e);
        }
  
        catch (ReadOnlyBufferException e) {
            System.out.println("Exception throws : " + e);
        }
    }
}


Output:

Original LongBuffer:  [8, 7, 9]

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 LongBuffer
        int capacity = 3;
  
        // Creating the LongBuffer
        try {
  
            // creating object of Longbuffer
            // and allocating size capacity
            LongBuffer ib = LongBuffer.allocate(capacity);
  
            // putting the value in Longbuffer
            // using put() at  index 0
            ib.put(0, 8);
  
            // putting the value in Longbuffer
            // using put() at  index 2
            ib.put(2, 9);
  
            // putting the value in Longbuffer
            // using put() at  index -1
            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.




// 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 LongBuffer
        int capacity = 3;
  
        // Creating the LongBuffer
        try {
  
            // creating object of Longbuffer
            // and allocating size capacity using allocate() method
            LongBuffer ib = LongBuffer.allocate(capacity);
  
            // Creating a read-only copy of LongBuffer
            // using asReadOnlyBuffer() method
            LongBuffer ib1 = ib.asReadOnlyBuffer();
  
            System.out.println("Trying to put the Long value"
                               + " in read only buffer");
  
            // putting the value in readonly Longbuffer
            // using put() method
            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 Long value in read only buffer
Exception throws : java.nio.ReadOnlyBufferException


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads