public class Piece extends Object
AbstractFileSet.| Modifier and Type | Field and Description |
|---|---|
protected AbstractFileSet |
files
The files associated with this piece
|
| Constructor and Description |
|---|
Piece(AbstractFileSet files,
byte[] hash,
int index,
int pieceSize,
int blockSize)
Creates a new piece.
|
| Modifier and Type | Method and Description |
|---|---|
boolean |
checkHash()
Checks if the received bytes hash matches with the hash which was given in the metadata
|
int |
countBlocksWithStatus(BlockStatus status) |
long |
countRemainingBytes()
Counts all block sizes which are not done yet
|
boolean |
equals(Object obj) |
int |
getBlockCount()
Gets the amount of blocks in this piece
|
int |
getBlockSize(int blockIndex)
Gets the size of the specified block
|
BlockStatus |
getBlockStatus(int blockIndex)
Gets the block status for the block at the given index
|
int |
getIndex()
Gets the index of this Piece
The index is equal to the offset in the file divided by the default piece size |
Optional<Block> |
getRequestBlock()
Gets a new block to be requested
|
int |
getSize()
Gets the total size of all blocks in bytes
|
boolean |
hasBlockWithStatus(BlockStatus status)
Tests if any of the blocks have the given status.
|
int |
hashCode() |
boolean |
isDone()
Checks if all blocks are done
|
boolean |
isStarted()
Checks all blockStates if they have been started
|
byte[] |
loadPiece(int offset,
int length)
Loads a bit of data from the file but it is not strictly a block as I use it
|
void |
onHashMismatch()
Drops ceil(10%) of the blocks in order to maintain speed and still try to *not* redownload the entire piece
|
void |
setBlockStatus(int blockIndex,
BlockStatus blockStatus)
Updates the block status of the block at the given index.
|
void |
storeBlock(int blockIndex,
byte[] blockData)
Writes the block into the correct file(s)
|
String |
toString() |
protected AbstractFileSet files
public Piece(AbstractFileSet files, byte[] hash, int index, int pieceSize, int blockSize)
files - The AbstractFileSet which owns this piece.hash - The hash of the data of this piece.index - The piece number within the AbstractFileSetpieceSize - The amount of bytes this piece has.blockSize - The size of the blocks.public void onHashMismatch()
public byte[] loadPiece(int offset,
int length)
throws IOException
offset - The offset in the piecelength - The amount of bytes to readIOException - When the underlying IO causes an error.public boolean checkHash()
throws IOException
IOExceptionpublic void storeBlock(int blockIndex,
byte[] blockData)
throws IOException
blockIndex - The index of the block to writeblockData - The data of the blockIOExceptionpublic long countRemainingBytes()
public void setBlockStatus(int blockIndex,
BlockStatus blockStatus)
blockIndex - The index of the block.blockStatus - The new status of the block.public BlockStatus getBlockStatus(int blockIndex)
blockIndex - The index of the blockpublic boolean isDone()
public boolean isStarted()
public int getBlockCount()
public int getIndex()
public int getSize()
public int countBlocksWithStatus(BlockStatus status)
status - The status which much be equal.public boolean hasBlockWithStatus(BlockStatus status)
status - The status expectedtrue when at least 1 block has the given status, otherwise falsepublic Optional<Block> getRequestBlock()
public int getBlockSize(int blockIndex)
blockIndex - The index of the block to get the size ofCopyright © 2016. All rights reserved.