Class Piece

    • 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 - The AbstractFileSet which owns this piece.
        hash - The hash of the data of this piece.
        index - The piece number within the AbstractFileSet
        pieceSize - 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 IOException
        Loads a bit of data from the file but it is not strictly a block as I use it
        Parameters:
        offset - The offset in the piece
        length - 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 IOException
        Checks 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 IOException
        Writes the block into the correct file(s)
        Parameters:
        blockIndex - The index of the block to write
        blockData - 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 true when at least 1 block has the given status, otherwise false
      • 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
      • hashCode

        public int hashCode()
        Overrides:
        hashCode in class Object