IntBuffer

IntBuffer is a class from the java.nio package in Java that allows you to work efficiently with sequences of 32-bit signed integers. It is part of the NIO (New I/O) library and supports buffer-based data manipulation useful for I/O operations, communication protocols, and performance-critical applications.IntBuffer is abstract; you create an instance using static factory methods like allocate() and wrap().

Commonly Used Methods

Simple Program using IntBuffer(Store and Display Integer Values)

import java.nio.IntBuffer;

public class SimpleIntBufferDemo {
    public static void main(String[] args) {
        // Allocate a buffer for 5 integers
        IntBuffer buffer = IntBuffer.allocate(5);

        // Put values into buffer
        buffer.put(100);
        buffer.put(200);
        buffer.put(300);

        // Switch to read mode
        buffer.flip();

        System.out.println("Reading IntBuffer:");
        while (buffer.hasRemaining()) {
            System.out.println(buffer.get());
        }
    }
}

Problem Statement

LotusJavaPrince is building a performance dashboard that logs CPU load percentages from various nodes in a distributed banking system. Mahesh gives him a task:

  • Store these load readings in an IntBuffer.
  • Filter out any values above 90 (considered CPU overload).
  • Display normal load values with index positions for analysis.

Help LotusJavaPrince implement this using IntBuffer.

import java.nio.IntBuffer;

public class CpuLoadMonitor {
    public static void main(String[] args) {
        // Simulated CPU load data from 10 servers
        int[] cpuLoads = {45, 67, 91, 88, 77, 95, 60, 84, 93, 70};

        // Wrap the CPU load array
        IntBuffer inputBuffer = IntBuffer.wrap(cpuLoads);

        // Allocate a new buffer to store normal loads (<= 90)
        IntBuffer normalLoadBuffer = IntBuffer.allocate(cpuLoads.length);

        // Filter values
        while (inputBuffer.hasRemaining()) {
            int load = inputBuffer.get();
            if (load <= 90) {
                normalLoadBuffer.put(load);
            }
        }

        // Prepare output buffer for reading
        normalLoadBuffer.flip();

        // Display filtered CPU loads
        System.out.println("Normal CPU Load Values (Filtered by LotusJavaPrince):");
        int index = 0;
        while (normalLoadBuffer.hasRemaining()) {
            System.out.println("Node[" + index + "] Load: " + normalLoadBuffer.get());
            index++;
        }
    }
}

Output

Normal CPU Load Values (Filtered by LotusJavaPrince):
Node[0] Load: 45
Node[1] Load: 67
Node[2] Load: 88
Node[3] Load: 77
Node[4] Load: 60
Node[5] Load: 84
Node[6] Load: 70Code language: CSS (css)

The IntBuffer class in Java NIO is a specialized buffer designed for handling sequences of 32-bit int values efficiently. Extending the abstract Buffer class, it provides all the standard buffer features such as position, limit, capacity, and mark, but tailored for integer data.

IntBuffer plays a vital role in high-performance applications that need to manipulate large volumes of integer data, such as numerical simulations, image processing, file I/O, or network protocols. With support for heap and direct buffers, it enables memory-efficient and fast data access, especially in conjunction with NIO channels.

Scroll to Top