java.lang.Object
org.klomp.snark.BitField
- Direct Known Subclasses:
CompleteBitField
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
Constructor Summary
ConstructorsModifierConstructorDescriptionBitField(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.protectedBitField(int size, int dummy) For CompleteBitField.Method Summary
All MethodsModifier and TypeMethodDescriptionvoidclear(int bit) Mark the given bit false.booleancomplete()Returns true if all bits are set.intcount()Returns the number of set bits.booleanCompares this BitField to another object for equality.booleanget(int bit) Returns true if the bit is set or false if it is not.byte[]Returns the actual byte array used.inthashCode()Returns a hash code value for this BitField.voidset(int bit) Mark the given bit true.voidsetAll()Mark all bits true.intsize()Returns the size of the BitField.toString()Compact range representation - "BitField(128)[0-4 9 22-30]" instead of enumerating every set bit.
Field Details
data
private final byte[] dataThe bits, packed into bytes.size
protected final int sizeNumber of bits this field spans, one per piece of the torrent.count
private int countHow 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 datasize- 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 exceptsize()must be overridden.- Parameters:
size- the number of bits in the bit fielddummy- 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
equals
toString