org.klomp.snark

## Class BitField

- java.lang.Object
- - org.klomp.snark.BitField

-     public class BitField
      extends Object

  Container of a byte array representing set and unset bits for torrent piece availability.

  This class manages a bit field where each bit represents whether a particular piece of a torrent is available (set) or not available (unset). It's used to:

  - Track which pieces a peer has available
  - Track which pieces the local client has downloaded
  - Determine which pieces are still needed
  - Optimize piece selection strategies

  The bit field is stored efficiently in a byte array, with 8 bits per byte.

- - ### Constructor Summary

    Constructors
    Constructor and Description
    `BitField(byte[] bitfield, int size)` Creates a new BitField that represents the given number of bits as set by the given byte array.
    `BitField(int size)` Creates a new BitField that represents the given number of unset bits.

  - ### Method Summary

    All Methods Instance Methods Concrete Methods
    Modifier and Type | Method and Description
    --- | ---
    `void` | `clear(int bit)` Sets the given bit to false.
    `boolean` | `complete()` Returns true if all bits are set.
    `int` | `count()` Returns the number of set bits.
    `boolean` | `equals(Object o)` Compares this BitField to another object for equality.
    `boolean` | `get(int bit)` Returns true if the bit is set or false if it is not.
    `byte[]` | `getFieldBytes()` Returns the actual byte array used.
    `int` | `hashCode()` Returns a hash code value for this BitField.
    `void` | `set(int bit)` Sets the given bit to true.
    `void` | `setAll()` Sets all bits to true.
    `int` | `size()` Returns the size of the BitField.
    `String` | `toString()`

    - ### Methods inherited from class java.lang.Object

      `clone, finalize, getClass, notify, notifyAll, wait, wait, wait`

- - ### Constructor Detail

    - #### BitField

          public BitField(byte[] bitfield,
                          int size)

      Creates a new BitField that represents the given number of bits as set by the given byte array. This will make a copy of the array. Extra bytes will be ignored.
      Parameters:
        `bitfield` - the byte array containing the bit field data
        `size` - the number of bits represented
      Throws:
        `IndexOutOfBoundsException` - if the byte array is not large enough

    - #### BitField

          public BitField(int size)

      Creates a new BitField that represents the given number of unset bits.
      Parameters:
        `size` - the number of bits in the bit field

  - ### Method Detail

    - #### clear

          public void clear(int bit)

      Sets the given bit to false.
      Parameters:
        `bit` - the bit index to clear
      Throws:
        `IndexOutOfBoundsException` - if bit is smaller then zero or bigger then or equal to size

    - #### complete

          public boolean complete()

      Returns true if all bits are set.
      Returns:
        true if all bits are set (bit field is complete), false otherwise

    - #### count

          public int count()

      Returns the number of set bits.
      Returns:
        the count of bits that are set to true

    - #### equals

          public boolean equals(Object o)

      Compares this BitField to another object for equality.
      Overrides:
        `equals` in class `Object`
      Parameters:
        `o` - the object to compare with
      Returns:
        true if the objects are equal, false otherwise

    - #### get

          public boolean get(int bit)

      Returns true if the bit is set or false if it is not.
      Parameters:
        `bit` - the bit index to check
      Returns:
        true if the bit is set, false otherwise
      Throws:
        `IndexOutOfBoundsException` - if bit is smaller then zero or bigger then or equal to size

    - #### getFieldBytes

          public byte[] getFieldBytes()

      Returns the actual byte array used. Changes to this array affect this BitField. Note that some bits at the end of the byte array are supposed to be always unset if they represent bits bigger then the size of the bitfield.

      Caller should synchronize on this and copy!

      Returns:
        the internal byte array representing the bit field

    - #### hashCode

          public int hashCode()

      Returns a hash code value for this BitField.
      Overrides:
        `hashCode` in class `Object`
      Returns:
        a hash code value for this BitField

    - #### set

          public void set(int bit)

      Sets the given bit to true.
      Parameters:
        `bit` - the bit index to set
      Throws:
        `IndexOutOfBoundsException` - if bit is smaller then zero or bigger then or equal to size

    - #### setAll

          public void setAll()

      Sets all bits to true.

    - #### size

          public int size()

      Returns the size of the BitField. The returned value is one bigger then the last valid bit number (since bit numbers are counted from zero).
      Returns:
        the number of bits in the bit field

    - #### toString

          public String toString()

      Overrides:
        `toString` in class `Object`