java.lang.Object
org.klomp.snark.BitField
Direct Known Subclasses:
CompleteBitField

public class BitFieldextends 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.

  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    private int
    How many bits are set.
    private final byte[]
    The bits, packed into bytes.
    protected final int
    Number of bits this field spans, one per piece of the torrent.
  • Constructor Summary

    Constructors
    Modifier
    Constructor
    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.
    protected
    BitField(int size, int dummy)
    For CompleteBitField.
  • Method Summary

    All Methods
    Modifier and Type
    Method
    Description
    void
    clear(int bit)
    Mark the given bit false.
    boolean
    Returns true if all bits are set.
    int
    Returns the number of set bits.
    boolean
    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[]
    Returns the actual byte array used.
    int
    Returns a hash code value for this BitField.
    void
    set(int bit)
    Mark the given bit true.
    void
    Mark all bits true.
    int
    Returns the size of the BitField.
    Compact range representation - "BitField(128)[0-4 9 22-30]" instead of enumerating every set bit.

    Methods inherited from class Object

    clone, getClass, notify, notifyAll, wait, wait, wait
  • Field Details

    • data

      private final byte[] data
      The bits, packed into bytes.
    • size

      protected final int size
      Number of bits this field spans, one per piece of the torrent.
    • count

      private int count
      How many bits are set.
  • Constructor Details

    • 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
    • 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

      protected BitField(int size, int dummy)
      For CompleteBitField. All methods except size() must be overridden.
      Parameters:
      size - the number of bits in the bit field
      dummy - an ignored marker, here only to tell this constructor apart from the one taking a byte array
  • Method Details

    • 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
    • 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
    • set

      public void set(int bit)
      Mark the given bit true.
      Parameters:
      bit - the zero-based piece index to flip, in the range 0 to size-1
      Throws:
      IndexOutOfBoundsException - if bit is smaller then zero or bigger then or equal to size
    • clear

      public void clear(int bit)
      Mark the given bit false.
      Parameters:
      bit - the zero-based piece index to flip, in the range 0 to size-1
      Throws:
      IndexOutOfBoundsException - if bit is smaller then zero or bigger then or equal to size
    • setAll

      public void setAll()
      Mark all bits true.
    • get

      public boolean get(int bit)
      Returns true if the bit is set or false if it is not.
      Parameters:
      bit - the zero-based piece index to flip, in the range 0 to size-1
      Returns:
      true if the bit is set, false otherwise
      Throws:
      IndexOutOfBoundsException - if bit is smaller then zero or bigger then or equal to size
    • count

      public int count()
      Returns the number of set bits.
      Returns:
      the count of bits that are set to true
    • complete

      public boolean complete()
      Returns true if all bits are set.
      Returns:
      true if all bits are set (bit field is complete), false otherwise
    • 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
    • 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
    • toString

      public String toString()
      Compact range representation - "BitField(128)[0-4 9 22-30]" instead of enumerating every set bit. Piece counts run into the thousands, and this is called from logging on hot paths.
      Overrides:
      toString in class Object