Java - Input Output Buffers

Introduction

A buffer is a fixed-length data container.

Buffer classes that hold primitive values are as follows:

ByteBuffer
ShortBuffer
CharBuffer
IntBuffer
LongBuffer
FloatBuffer
DoubleBuffer

An object of an XxxBuffer class is used to hold data of the Xxx primitive data type. 

Each buffer has four properties

  • Capacity
  • Position
  • Limit
  • Mark

Capacity

The capacity of a buffer is fixed when the buffer is created.

It is like the length of an array.

A buffer is not necessarily backed by an array.

You can check if a buffer is backed by an array by calling its hasArray() method.

You can get access to the backing array of a buffer by using the array() method.

Any changes made to that array will be reflected in the buffer.

capacity() method that returns its capacity.

Related Topics