class PartialPiece extends Object implements Comparable<PartialPiece>
Handles out-of-order chunk reception and manages a BitField tracking received chunks. Implements Comparable for prioritization by piece number and completeness.
Usage Notes: - Not thread-safe except for synchronized public methods. - Objects for the same piece should not be shared between peers. - Used extensively by PeerCoordinator and PeerState for request management.
| Constructor and Description |
|---|
PartialPiece(Piece piece,
int len,
File tempDir)Constructs a PartialPiece for the given piece with specified length. |
| Modifier and Type | Method and Description |
|---|---|
int | compareTo(PartialPiece other)Compares this piece with another by piece number, highest priority first, then by highest
downloaded first. |
boolean | equals(Object o)Equality based on piece ID only. |
int | getDownloaded()Returns the total number of bytes downloaded in this piece, accurately accounting for holes. |
byte[] | getHash()Computes and returns the SHA1 hash of the complete piece data. |
int | getLength()Returns the length of the piece in bytes. |
int | getPiece()Returns the piece number. |
Request | getRequest()Creates a Request object for the next missing chunk in this piece. |
boolean | hasChunk(int chunk)Checks if the given chunk index has been downloaded. |
boolean | hasData()Checks if any chunks of this piece have been downloaded. |
int | hashCode() |
static void | info(String s)Logs an info message for this class. |
boolean | isComplete()Checks if the entire piece has been downloaded. |
void | read(DataInputStream din,
int offset,
int len,
BandwidthListener bwl)Reads data from the input stream into this piece starting at offset. |
void | release()Releases temporary file and resources associated with this piece. |
String | toString() |
static void | warn(String s)Logs a warning message for this class. |
void | write(DataOutput out,
int offset,
int len)Writes piece data from the given offset and length to the output. |
public PartialPiece(Piece piece, int len, File tempDir)
piece - The Piece identifier objectlen - Length of this piece in bytes (must match piece length)tempDir - Directory for temporary file storage if neededpublic int compareTo(PartialPiece other)
compareTo in interface Comparable<PartialPiece>other - the other PartialPiece to compare topublic boolean equals(Object o)
public int getDownloaded()
public byte[] getHash()
throws IOExceptionIOException - if data is incomplete or read failspublic int getLength()
public int getPiece()
public Request getRequest()
public boolean hasChunk(int chunk)
chunk - zero-based chunk indexpublic boolean hasData()
public static void info(String s)
s - info message stringpublic boolean isComplete()
public void read(DataInputStream din, int offset, int len, BandwidthListener bwl) throws IOException
din - input stream to read fromoffset - offset to read into the piece (must be multiple of chunk size)len - length of data to readbwl - bandwidth listener to report downloaded bytesIOException - on I/O errors or incorrect offsetpublic void release()
public static void warn(String s)
s - warning message stringpublic void write(DataOutput out, int offset, int len) throws IOException
out - destination output to write data tooffset - offset in this piece to start writing fromlen - length of data to writeIOException - on I/O failure