Package io.bytestreams.codec.iso8583
Class MultiBlockBitmap
java.lang.Object
io.bytestreams.codec.iso8583.MultiBlockBitmap
- All Implemented Interfaces:
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
ConstructorsConstructorDescriptionMultiBlockBitmap(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 TypeMethodDescriptionintcapacity()Returns the total number of bits in the bitmap.intReturns the number of bits that are set.booleanclear(int bit) Clears the given bit.booleanget(int bit) Returns whether the given bit is set.booleanisExtensionBit(int bit) Returns whether the given bit is an extension indicator.booleanset(int bit) Sets the given bit.stream()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.toString()
-
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- ifsizeis 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- ifsizeis less than 1 or exceeds the maximum capacity, or ifmaxBlocksis 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. -
get
public boolean get(int bit) Returns whether the given bit is set.- Specified by:
getin interfaceBitmap- Parameters:
bit- the 1-based bit index to check.- Returns:
trueif the bit is set,falseotherwise.- Throws:
IllegalArgumentException- ifbitis less than 1 or greater thancapacity().
-
set
public boolean set(int bit) Sets the given bit. Blocks are automatically allocated as needed. Extension indicators are updated automatically.- Specified by:
setin interfaceBitmap- Parameters:
bit- the 1-based bit index to set.- Returns:
trueif the bit was not already set,falseotherwise.- Throws:
IllegalArgumentException- ifbitis less than 1 or greater thancapacity(), or ifbitis 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:
clearin interfaceBitmap- Parameters:
bit- the 1-based bit index to clear.- Returns:
trueif the bit was set,falseotherwise.- Throws:
IllegalArgumentException- ifbitis less than 1 or greater thancapacity(), or ifbitis an extension indicator.
-
cardinality
public int cardinality()Returns the number of bits that are set.- Specified by:
cardinalityin interfaceBitmap- Returns:
- the number of bits set.
-
stream
Returns a stream of the 1-based indices of all set bits. -
toByteArray
public byte[] toByteArray()Converts the bitmap to a byte array in big-endian bit order. Trailing empty blocks are omitted.- Specified by:
toByteArrayin interfaceBitmap- Returns:
- the byte array representation of the bitmap.
-
toString
-
isExtensionBit
public boolean isExtensionBit(int bit) Description copied from interface:BitmapReturns 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:
isExtensionBitin interfaceBitmap- Parameters:
bit- the 1-based bit index to check.- Returns:
trueif the bit is an extension indicator,falseotherwise.
-