Buffer classes Heirarchy

Java introduced the New I/O (NIO) package (java.nio) to provide more efficient data handling, especially for large-scale, non-blocking input/output operations. Central to this NIO architecture is the Buffer class and its subclasses, which are used for storing and manipulating data in memory.

1. The Root Class – java.nio.Buffer

At the top of the hierarchy is the Buffer class, which is an abstract class. It is not meant to be used directly but serves as the foundation for all type-specific buffer classes. This class provides the essential properties and methods needed to read from or write to a buffer.

Core Properties:

  • capacity – the total number of elements the buffer can contain (fixed at creation).
  • position – the index of the next element to be read or written.
  • limit – the index just beyond the last readable or writable element.
  • mark – a remembered position used for resetting the buffer.

Common Methods:

  • clear() – prepares the buffer for writing.
  • flip() – switches the buffer from writing mode to reading mode.
  • rewind() – resets the position to zero for re-reading data.
  • mark() and reset() – bookmark a position and return to it.
  • hasRemaining() – checks if there are more elements to read/write.
  • remaining() – returns how many elements are left.

2. Specialized Subclasses for Primitive Types

The Buffer class has several subclasses, each specialized for handling a specific Java primitive data type. These subclasses add type-specific methods for reading and writing values.

ByteBuffer

  • Used for handling sequences of bytes.
  • Most versatile buffer; supports I/O with channels and can create direct buffers (memory outside the JVM heap).
  • Used extensively for file, socket, and memory-mapped operations.

CharBuffer

  • Stores characters (char type).
  • Useful in text processing and Unicode manipulation.

ShortBuffer

  • Handles short values.
  • Used where data is in 2-byte quantities.

IntBuffer

  • Stores int values.
  • Commonly used in graphics, numeric computation, and network operations.

LongBuffer

  • Used for storing long values (64-bit integers).
  • Useful in applications needing large number representations.

FloatBuffer

  • Stores floating-point numbers of type float.
  • Useful for scientific, simulation, and graphics applications.

DoubleBuffer

  • Stores double-precision floating-point numbers (double).
  • Preferred when higher precision is required for calculations.

Each of these subclasses inherits the core properties and behaviors from Buffer but adds type-specific methods:

  • get(), get(index)
  • put(value), put(index, value)

The Buffer class hierarchy in Java NIO is a well-organized system designed to handle various types of primitive data efficiently. By providing a unified structure through the abstract Buffer class and type-specific subclasses like ByteBuffer, CharBuffer, etc., Java NIO makes it easier to manage low-level data manipulation while supporting high-performance I/O operations.

Scroll to Top