Class Piece
- java.lang.Object
-
- org.johnnei.javatorrent.torrent.files.Piece
-
public class Piece extends Object
Represents a piece within aAbstractFileSet.
-
-
Field Summary
Fields Modifier and Type Field Description protected AbstractFileSetfilesThe files associated with this piece
-
Constructor Summary
Constructors Constructor Description Piece(AbstractFileSet files, byte[] hash, int index, int pieceSize, int blockSize)Creates a new piece.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description booleancheckHash()Checks if the received bytes hash matches with the hash which was given in the metadataintcountBlocksWithStatus(BlockStatus status)longcountRemainingBytes()Counts all block sizes which are not done yetbooleanequals(Object obj)intgetBlockCount()Gets the amount of blocks in this pieceintgetBlockSize(int blockIndex)Gets the size of the specified blockBlockStatusgetBlockStatus(int blockIndex)Gets the block status for the block at the given indexAbstractFileSetgetFileSet()intgetIndex()Gets the index of this Piece
The index is equal to the offset in the file divided by the default piece sizeOptional<Block>getRequestBlock()Gets a new block to be requestedintgetSize()Gets the total size of all blocks in bytesbooleanhasBlockWithStatus(BlockStatus status)Tests if any of the blocks have the given status.inthashCode()booleanisDone()Checks if all blocks are donebooleanisStarted()Checks all blockStates if they have been startedbyte[]loadPiece(int offset, int length)Loads a bit of data from the file but it is not strictly a block as I use itvoidonHashMismatch()Drops ceil(10%) of the blocks in order to maintain speed and still try to *not* redownload the entire piecevoidsetBlockStatus(int blockIndex, BlockStatus blockStatus)Updates the block status of the block at the given index.voidstoreBlock(int blockIndex, byte[] blockData)Writes the block into the correct file(s)StringtoString()
-
-
-
Field Detail
-
files
protected AbstractFileSet files
The files associated with this piece
-
-
Constructor Detail
-
Piece
public Piece(AbstractFileSet files, byte[] hash, int index, int pieceSize, int blockSize)
Creates a new piece.- Parameters:
files- TheAbstractFileSetwhich owns this piece.hash- The hash of the data of this piece.index- The piece number within theAbstractFileSetpieceSize- The amount of bytes this piece has.blockSize- The size of the blocks.
-
-
Method Detail
-
onHashMismatch
public void onHashMismatch()
Drops ceil(10%) of the blocks in order to maintain speed and still try to *not* redownload the entire piece
-
loadPiece
public byte[] loadPiece(int offset, int length) throws IOExceptionLoads a bit of data from the file but it is not strictly a block as I use it- Parameters:
offset- The offset in the piecelength- The amount of bytes to read- Returns:
- The read bytes or an exception
- Throws:
IOException- When the underlying IO causes an error.
-
checkHash
public boolean checkHash() throws IOExceptionChecks if the received bytes hash matches with the hash which was given in the metadata- Returns:
- hashMatched ? true : false
- Throws:
IOException
-
storeBlock
public void storeBlock(int blockIndex, byte[] blockData) throws IOExceptionWrites the block into the correct file(s)- Parameters:
blockIndex- The index of the block to writeblockData- The data of the block- Throws:
IOException
-
countRemainingBytes
public long countRemainingBytes()
Counts all block sizes which are not done yet- Returns:
- The remaining amount of bytes to finish this piece
-
setBlockStatus
public void setBlockStatus(int blockIndex, BlockStatus blockStatus)Updates the block status of the block at the given index.- Parameters:
blockIndex- The index of the block.blockStatus- The new status of the block.
-
getBlockStatus
public BlockStatus getBlockStatus(int blockIndex)
Gets the block status for the block at the given index- Parameters:
blockIndex- The index of the block- Returns:
- The status of the given block.
-
isDone
public boolean isDone()
Checks if all blocks are done- Returns:
- If this piece is completed
-
isStarted
public boolean isStarted()
Checks all blockStates if they have been started- Returns:
- true if any progress is found
-
getBlockCount
public int getBlockCount()
Gets the amount of blocks in this piece- Returns:
- block count
-
getIndex
public int getIndex()
Gets the index of this Piece
The index is equal to the offset in the file divided by the default piece size- Returns:
- The index of the piece within the fileset.
-
getSize
public int getSize()
Gets the total size of all blocks in bytes- Returns:
- The size of this piece
-
countBlocksWithStatus
public int countBlocksWithStatus(BlockStatus status)
- Parameters:
status- The status which much be equal.- Returns:
- The amount of blocks in this piece with the given status.
-
hasBlockWithStatus
public boolean hasBlockWithStatus(BlockStatus status)
Tests if any of the blocks have the given status.- Parameters:
status- The status expected- Returns:
- returns
truewhen at least 1 block has the given status, otherwisefalse
-
getRequestBlock
public Optional<Block> getRequestBlock()
Gets a new block to be requested- Returns:
- an unrequested block
-
getBlockSize
public int getBlockSize(int blockIndex)
Gets the size of the specified block- Parameters:
blockIndex- The index of the block to get the size of- Returns:
- Size of the block in bytes
-
getFileSet
public AbstractFileSet getFileSet()
- Returns:
- The
AbstractFileSetwhich contains this piece.
-
-