Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Java Arrays
  • Java Strings
  • Java OOPs
  • Java Collection
  • Java 8 Tutorial
  • Java Multithreading
  • Java Exception Handling
  • Java Programs
  • Java Project
  • Java Collections Interview
  • Java Interview Questions
  • Java MCQs
  • Spring
  • Spring MVC
  • Spring Boot
  • Hibernate
Open In App
Next Article:
ByteBuffer putDouble() methods in Java with Examples
Next article icon

ByteBuffer putLong() methods in Java with Examples

Last Updated : 06 Feb, 2023
Comments
Improve
Suggest changes
Like Article
Like
Report

putLong(int value)

The putLong(int value) method of java.nio.ByteBuffer Class is used to write eight bytes containing the given long value, in the current byte order, into this buffer at the current position, and then increments the position by eight. 

Syntax:

public abstract ByteBuffer putLong(long value)

Parameters: This method takes a long value to be written. 

Return Value: This method returns this buffer. 

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 putLong(long value) method: 

Example 1: 

Java




// Java program to demonstrate
// putLong() method
 
import java.nio.*;
import java.util.*;
 
public class GFG {
    public static void main(String[] args)
    {
 
        // Declaring the capacity of the ByteBuffer
        int capacity = 24;
 
        // Creating the ByteBuffer
        try {
 
            // creating object of ByteBuffer
            // and allocating size capacity
            ByteBuffer bb = ByteBuffer.allocate(capacity);
 
            // putting the value in ByteBuffer
            // using putLong() method
            bb.putLong(23)
                .putLong(24)
                .putLong(30)
                .rewind();
 
            // print the ByteBuffer
            System.out.print("Original ByteBuffer: [ ");
            for (int i = 1; i <= capacity / 8; i++)
                System.out.print(bb.getLong() + " ");
            System.out.print("]");
        }
 
        catch (BufferOverflowException e) {
 
            System.out.println("Exception throws : " + e);
        }
 
        catch (ReadOnlyBufferException e) {
 
            System.out.println("Exception throws : " + e);
        }
    }
}
 
 
Output:
Original ByteBuffer: [ 23 24 30 ]

Example 2: To demonstrate BufferOverflowException. 

Java




// Java program to demonstrate
// putLong() method
 
import java.nio.*;
import java.util.*;
 
public class GFG {
    public static void main(String[] args)
    {
 
        // Declaring the capacity of the ByteBuffer
        int capacity = 24;
 
        // Creating the ByteBuffer
        try {
 
            // creating object of ByteBuffer
            // and allocating size capacity
            ByteBuffer bb = ByteBuffer.allocate(capacity);
 
            // putting the value in ByteBuffer
            // using putLong() method
            bb.putLong(23)
                .putLong(24)
                .putLong(30)
                .rewind();
 
            // print the ByteBuffer
            System.out.print("Original ByteBuffer: [ ");
            for (int i = 1; i <= capacity / 8; i++)
                System.out.print(bb.getLong() + " ");
            System.out.print("]");
 
            // putting the value in ByteBuffer
            // using putLong() method
            bb.putLong(234);
        }
 
        catch (BufferOverflowException e) {
            System.out.println("\n\nbuffer'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: [ 23 24 30 ]  buffer's current position is not smaller than its limit Exception throws : java.nio.BufferOverflowException

Examples 3: To demonstrate ReadOnlyBufferException. 

Java




// Java program to demonstrate
// putLong() method
 
import java.nio.*;
import java.util.*;
 
public class GFG {
    public static void main(String[] args)
    {
 
        // Declaring the capacity of the ByteBuffer
        int capacity = 24;
 
        // Creating the ByteBuffer
        try {
 
            // creating object of ByteBuffer
            // and allocating size capacity
            ByteBuffer bb = ByteBuffer.allocate(capacity);
 
            // putting the value in ByteBuffer
            // using putLong() method
            bb.putLong(23)
                .putLong(24)
                .putLong(30)
                .rewind();
 
            // print the ByteBuffer
            System.out.print("Original ByteBuffer: [ ");
            for (int i = 1; i <= capacity / 8; i++)
                System.out.print(bb.getLong() + " ");
            System.out.print("]");
 
            // Creating a read-only copy of ByteBuffer
            // using asReadOnlyBuffer() method
            ByteBuffer bb1 = bb.asReadOnlyBuffer();
 
            System.out.println("\n\nTrying to put the long value"
                               + " in read-only buffer");
 
            // putting the value in readonly ByteBuffer
            // using putLong() method
            bb1.putLong(234);
        }
 
        catch (BufferOverflowException e) {
 
            System.out.println("Exception throws : " + e);
        }
 
        catch (ReadOnlyBufferException e) {
 
            System.out.println("Exception throws : " + e);
        }
    }
}
 
 
Output:
Original ByteBuffer: [ 23 24 30 ]  Trying to put the long value in read only buffer Exception throws : java.nio.ReadOnlyBufferException

putLong(int index, long value)

The putLong(int index, long value) method of java.nio.ByteBuffer Class is used to write eight bytes containing the given eight-byte value, in the current byte order, into this buffer at the given index. 

Syntax:

public abstract ByteBuffer putLong(int index, long value)

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

  • index: The index at which the byte will be written
  • value: 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 putLong(int index, long value) method: 

Example 1: 

Java




// Java program to demonstrate
// putLong() method
 
import java.nio.*;
import java.util.*;
 
public class GFG {
 
    public static void main(String[] args)
    {
 
        // Declaring the capacity of the ByteBuffer
        int capacity = 24;
 
        // Creating the ByteBuffer
        try {
 
            // creating object of ByteBuffer
            // and allocating size capacity
            ByteBuffer bb = ByteBuffer.allocate(capacity);
 
            // putting the value in ByteBuffer
            // using putLong() at  index 0
            bb.putLong(0, 23);
 
            // putting the value in ByteBuffer
            // using putLong() at  index 8
            bb.putLong(8, 34);
 
            // putting the value in ByteBuffer
            // using putLong() at  index 16
            bb.putLong(16, 27);
 
            // rewinding the ByteBuffer
            bb.rewind();
 
            // print the ByteBuffer
            System.out.print("Original ByteBuffer: [ ");
            for (int i = 1; i <= capacity / 8; i++)
                System.out.print(bb.getLong() + "  ");
            System.out.print("]\n");
        }
 
        catch (IndexOutOfBoundsException e) {
 
            System.out.println("Exception throws : " + e);
        }
 
        catch (ReadOnlyBufferException e) {
 
            System.out.println("Exception throws : " + e);
        }
    }
}
 
 
Output:
Original ByteBuffer: [ 23  34  27  ]

Example 2: To demonstrate IndexOutOfBoundsException. 

Java




// Java program to demonstrate
// putLong() method
 
import java.nio.*;
import java.util.*;
 
public class GFG {
 
    public static void main(String[] args)
    {
 
        // Declaring the capacity of the ByteBuffer
        int capacity = 24;
 
        // Creating the ByteBuffer
        try {
 
            // creating object of ByteBuffer
            // and allocating size capacity
            ByteBuffer bb = ByteBuffer.allocate(capacity);
 
            // putting the value in ByteBuffer
            // using putLong() at  index 0
            bb.putLong(0, 23);
 
            // putting the value in ByteBuffer
            // using putLong() at  index 8
            bb.putLong(8, 34);
 
            // putting the value in ByteBuffer
            // using putLong() at  index 16
            bb.putLong(16, 27);
 
            // rewinding the ByteBuffer
            bb.rewind();
 
            // print the ByteBuffer
            System.out.print("Original ByteBuffer: [ ");
            for (int i = 1; i <= capacity / 8; i++)
                System.out.print(bb.getLong() + "  ");
            System.out.print("]\n");
 
            // putting the value in ByteBuffer
            // using putInt() at  index -1
            bb.putInt(-1, 45);
        }
 
        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: [ 23  34  27  ]  index is negative or not smaller than the buffer's limit Exception throws : java.lang.IndexOutOfBoundsException

Example 3: To demonstrate ReadOnlyBufferException. 

Java




// Java program to demonstrate
// putLong() method
 
import java.nio.*;
import java.util.*;
 
public class GFG {
 
    public static void main(String[] args)
    {
 
        // Declaring the capacity of the ByteBuffer
        int capacity = 24;
 
        // Creating the ByteBuffer
        try {
 
            // creating object of ByteBuffer
            // and allocating size capacity
            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 long value"
                               + " in read-only buffer");
 
            // putting the value in readonly ByteBuffer
            // using putLong() method
            bb1.putLong(0, 23);
        }
 
        catch (IndexOutOfBoundsException 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

Reference:

  • https://docs.oracle.com/javase/9/docs/api/java/nio/ByteBuffer.html#putLong-long-
  • https://docs.oracle.com/javase/9/docs/api/java/nio/ByteBuffer.html#putLong-int-long-


Next Article
ByteBuffer putDouble() methods in Java with Examples

R

RohitPrasad3
Improve
Article Tags :
  • Java
  • Java-ByteBuffer
  • Java-Functions
  • Java-NIO package
Practice Tags :
  • Java

Similar Reads

  • ByteBuffer putFloat() methods in Java with Examples
    putFloat(float value) The putFloat(float value) method of java.nio.ByteBuffer Class is used to write four bytes containing the given float value, in the current byte order, into this buffer at the current position, and then increments the position by four. Syntax: public abstract ByteBuffer putFloat
    7 min read
  • ByteBuffer putInt() methods in Java with Examples
    putInt(int value) The putInt(int value) method of java.nio.ByteBuffer Class is used to write four bytes containing the given int value, in the current byte order, into this buffer at the current position, and then increments the position by four. Syntax : public abstract ByteBuffer putInt(int value)
    7 min read
  • ByteBuffer putShort() methods in Java with Examples
    putShort(int value) The putShort(int value) method of java.nio.ByteBuffer Class is used to write two bytes containing the given short value, in the current byte order, into this buffer at the current position, and then increments the position by two. Syntax: public abstract ByteBuffer putShort(short
    6 min read
  • ByteBuffer position() methods in Java with Examples
    The position(int newPosition) method of java.nio.ByteBuffer Class is used to Sets this buffer's position. If the mark is defined and larger than the new position then it is discarded. Syntax: public ByteBuffer position(int newPosition) Parameters: This method takes the newPosition as parameter which
    2 min read
  • ByteBuffer putDouble() methods in Java with Examples
    putDouble(double value) The putDouble(double value) method of java.nio.ByteBuffer Class is used to write eight bytes containing the given double value, in the current byte order, into this buffer at the current position, and then increments the position by eight. Syntax: public abstract ByteBuffer p
    6 min read
  • ByteBuffer putChar() methods in Java with Examples
    putChar(char value) The putChar(char value) method of java.nio.ByteBuffer Class is used to write two bytes containing the given char value, in the current byte order, into this buffer at the current position, and then increments the position by two. Syntax: public abstract ByteBuffer putChar(char va
    6 min read
  • ByteBuffer toString() method in Java with Examples
    The toString() method of ByteBuffer class is the inbuilt method used to returns a string representing the data contained by ByteBuffer Object. A new String object is created and initialized to get the character sequence from this ByteBuffer object and then String is returned by toString(). Subsequen
    2 min read
  • ByteBuffer put() methods in Java with Examples | Set -1
    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
    6 min read
  • ByteBuffer reset() methods in Java with Examples
    The reset() method of java.nio.ByteBuffer Class is used to reset this buffer's position to the previously-marked position. Invoking this method neither changes nor discards the mark's value. Syntax: public ByteBuffer reset() Return Value: This method returns this buffer. Below are the examples to il
    2 min read
  • Buffer position() methods in Java with Examples
    The position(int newPosition) method of java.nio.Buffer Class is used to set this buffer's position. If the mark is defined and larger than the new position then it is discarded. Syntax: public Buffer position(int newPosition) Parameters: This method takes the newPosition as parameter which is the n
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences