Class MultiBlockBitmap

java.lang.Object
io.bytestreams.codec.iso8583.MultiBlockBitmap
All Implemented Interfaces:
Bitmap

public class MultiBlockBitmap extends Object implements Bitmap
A Bitmap backed by a BitSet divided into fixed-size blocks. Blocks are automatically allocated when bits are set, and deallocated when all bits in trailing blocks are cleared.

The first bit of each block (bit 1, bit size * 8 + 1, etc.) is the extension indicator, which signals whether a subsequent block is present. Extension bits are managed automatically and cannot be set or cleared directly.


 // A standard ISO 8583 bitmap with 8-byte blocks, up to 3 extensions
 MultiBlockBitmap bitmap = new MultiBlockBitmap(8, 3);
 bitmap.set(3);   // sets bit 3 in block 0
 bitmap.set(65);  // auto-expands to block 1, extension bit (bit 1) is set automatically
 bitmap.get(1);   // true — extension indicator was auto-set
 bitmap.toByteArray(); // 16 bytes (2 blocks)
 
  • Constructor Summary

    Constructors
    Constructor
    Description
    MultiBlockBitmap(int size)
    Creates a new MultiBlockBitmap with the maximum number of blocks calculated from the block size.
    MultiBlockBitmap(int size, int maxBlocks)
    Creates a new MultiBlockBitmap.
  • Method Summary

    Modifier and Type
    Method
    Description
    int
    Returns the total number of bits in the bitmap.
    int
    Returns the number of bits that are set.
    boolean
    clear(int bit)
    Clears the given bit.
    boolean
    get(int bit)
    Returns whether the given bit is set.
    boolean
    isExtensionBit(int bit)
    Returns whether the given bit is an extension indicator.
    boolean
    set(int bit)
    Sets the given bit.
    Returns a stream of the 1-based indices of all set bits.
    byte[]
    Converts the bitmap to a byte array in big-endian bit order.
     

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
  • Constructor Details

    • MultiBlockBitmap

      public MultiBlockBitmap(int size)
      Creates a new MultiBlockBitmap with the maximum number of blocks calculated from the block size.
      Parameters:
      size - the number of bytes per block.
      Throws:
      IllegalArgumentException - if size is less than 1 or exceeds the maximum capacity.
    • MultiBlockBitmap

      public MultiBlockBitmap(int size, int maxBlocks)
      Creates a new MultiBlockBitmap.
      Parameters:
      size - the number of bytes per block.
      maxBlocks - the maximum number of blocks.
      Throws:
      IllegalArgumentException - if size is less than 1 or exceeds the maximum capacity, or if maxBlocks is less than 1 or exceeds the maximum allowed for the given size.
  • Method Details

    • capacity

      public int capacity()
      Returns the total number of bits in the bitmap.
      Specified by:
      capacity in interface Bitmap
      Returns:
      the total number of bits.
    • get

      public boolean get(int bit)
      Returns whether the given bit is set.
      Specified by:
      get in interface Bitmap
      Parameters:
      bit - the 1-based bit index to check.
      Returns:
      true if the bit is set, false otherwise.
      Throws:
      IllegalArgumentException - if bit is less than 1 or greater than capacity().
    • set

      public boolean set(int bit)
      Sets the given bit. Blocks are automatically allocated as needed. Extension indicators are updated automatically.
      Specified by:
      set in interface Bitmap
      Parameters:
      bit - the 1-based bit index to set.
      Returns:
      true if the bit was not already set, false otherwise.
      Throws:
      IllegalArgumentException - if bit is less than 1 or greater than capacity(), or if bit is an extension indicator.
    • clear

      public boolean clear(int bit)
      Clears the given bit. Extension indicators are updated automatically. Trailing empty blocks are deallocated.
      Specified by:
      clear in interface Bitmap
      Parameters:
      bit - the 1-based bit index to clear.
      Returns:
      true if the bit was set, false otherwise.
      Throws:
      IllegalArgumentException - if bit is less than 1 or greater than capacity(), or if bit is an extension indicator.
    • cardinality

      public int cardinality()
      Returns the number of bits that are set.
      Specified by:
      cardinality in interface Bitmap
      Returns:
      the number of bits set.
    • stream

      public IntStream stream()
      Returns a stream of the 1-based indices of all set bits.
      Specified by:
      stream in interface Bitmap
      Returns:
      a stream of set bit indices.
    • toByteArray

      public byte[] toByteArray()
      Converts the bitmap to a byte array in big-endian bit order. Trailing empty blocks are omitted.
      Specified by:
      toByteArray in interface Bitmap
      Returns:
      the byte array representation of the bitmap.
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • isExtensionBit

      public boolean isExtensionBit(int bit)
      Description copied from interface: Bitmap
      Returns whether the given bit is an extension indicator. Extension indicators signal that additional bitmap blocks follow and cannot be used as data fields.
      Specified by:
      isExtensionBit in interface Bitmap
      Parameters:
      bit - the 1-based bit index to check.
      Returns:
      true if the bit is an extension indicator, false otherwise.