public class BitField extends Object
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:
The bit field is stored efficiently in a byte array, with 8 bits per byte.
| 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. |
| 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() |
public BitField(byte[] bitfield,
int size)bitfield - the byte array containing the bit field datasize - the number of bits representedIndexOutOfBoundsException - if the byte array is not large enoughpublic BitField(int size)
size - the number of bits in the bit fieldpublic void clear(int bit)
bit - the bitIndexOutOfBoundsException - if bit is smaller then zero or bigger then or equal to sizepublic boolean complete()
public int count()
public boolean equals(Object o)
public boolean get(int bit)
bit - the bitIndexOutOfBoundsException - if bit is smaller then zero or bigger then or equal to sizepublic byte[] getFieldBytes()
Caller should synchronize on this and copy!
public int hashCode()
public void set(int bit)
bit - the bitIndexOutOfBoundsException - if bit is smaller then zero or bigger then or equal to sizepublic void setAll()
public int size()