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)A PartialPiece for the given piece with the specified length. |
PartialPiece(Piece piece,
int len,
File tempDir,
MetaInfo meta)A PartialPiece for the given piece with the 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. |
static void | debug(String s)Logs a debug message for this class. |
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. |
Request | getRequest(int minOffset)Creates a Request object for the next missing chunk at or after the given offset. |
boolean | hasData()Checks if any chunks of this piece have been downloaded. |
int | hashCode() |
boolean | hasSubBlock(int subBlock)Checks if the given sub-block index has been downloaded. |
static void | info(String s)Logs an info message for this class. |
boolean | isComplete()Checks if the entire piece has been downloaded. |
(package private) boolean | isPaddingSubBlock(int subBlock)True if the sub-block lies entirely within BEP 47 padding files. |
void | markSubBlock(int subBlock)Marks a sub-block as received without any data, for BEP 47 padding-only sub-blocks whose
bytes are all zeros. |
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 PartialPiece(Piece piece, int len, File tempDir, MetaInfo meta)
piece - The Piece identifier objectlen - Length of this piece in bytes (must match piece length)tempDir - Directory for temporary file storage if neededmeta - the torrent meta info, for skipping BEP 47 padding-only chunks, or nullpublic int compareTo(PartialPiece other)
compareTo in interface Comparable<PartialPiece>other - the other PartialPiece to compare topublic static void debug(String s)
s - debug message stringpublic 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 Request getRequest(int minOffset)
minOffset - byte offset to start scanning at, rounded up to a sub-block boundary; the
caller advances it past the last request (callers pass 0 or off + len, both already
sub-block aligned) so in-flight chunks are not re-requestedpublic boolean hasData()
public boolean hasSubBlock(int subBlock)
subBlock - zero-based sub-block indexpublic static void info(String s)
s - info message stringpublic boolean isComplete()
boolean isPaddingSubBlock(int subBlock)
subBlock - zero-based sub-block indexpublic void markSubBlock(int subBlock)
subBlock - zero-based sub-block indexpublic 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