ShortBuffer

ShortBuffer is part of the java.nio package and provides a buffer for manipulating 16-bit short integers. It is useful in low-level I/O tasks, such as binary data processing, communication protocols, and memory-mapped files.ShortBuffer is an abstract class; instances are created using the allocate() or wrap() static methods.

Commonly Used Methods

Simple Program using ShortBuffer(Storing and Reading Short Values)

import java.nio.ShortBuffer;

public class SimpleShortBufferDemo {
    public static void main(String[] args) {
        // Create a buffer of capacity 5
        ShortBuffer buffer = ShortBuffer.allocate(5);

        // Add short values
        buffer.put((short)10);
        buffer.put((short)20);
        buffer.put((short)30);

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

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

Problem Statement:

LotusJavaPrince found a binary sensor that outputs values as short integers. Mahesh challenges him to design a monitoring system that:

  1. Stores sensor readings using a ShortBuffer.
  2. Filters out any negative values (sensor noise).
  3. Outputs only valid positive readings to a display.

Implement this using ShortBuffer, demonstrating buffer operations like put(), flip(), and get().

import java.nio.ShortBuffer;

public class SensorFilterCaseStudy {
    public static void main(String[] args) {
        // Simulated sensor data (some are noise - negative)
        short[] sensorData = {12, -3, 25, -7, 18, 0, -1};

        // Wrap sensor data in input buffer
        ShortBuffer inputBuffer = ShortBuffer.wrap(sensorData);

        // Output buffer to hold valid readings
        ShortBuffer outputBuffer = ShortBuffer.allocate(sensorData.length);

        // Process each value
        while (inputBuffer.hasRemaining()) {
            short value = inputBuffer.get();
            if (value >= 0) {
                outputBuffer.put(value); // Keep only non-negative values
            }
        }

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

        // Display the cleaned sensor readings
        System.out.println("Filtered Sensor Data (by LotusJavaPrince):");
        while (outputBuffer.hasRemaining()) {
            System.out.print(outputBuffer.get() + " ");
        }
    }
}

Output

Filtered Sensor Data (by LotusJavaPrince):
12 25 18 0 

The ShortBuffer class in Java NIO provides a specialized, high-performance buffer for handling sequences of 16-bit short values. As a subclass of the abstract Buffer class, it extends core buffer capabilities—like managing position, limit, capacity, and mark—to the domain of primitive short data.

ShortBuffer is particularly useful when working with low-level binary data, such as in scientific computing, graphics processing, or data acquisition systems where short integers are common. It can be backed by either a heap buffer or a direct buffer, enabling flexible memory management and faster I/O when interacting with native code or memory-mapped files.

Scroll to Top