/* */ #include "PiecedSegment.h" #include "Piece.h" PiecedSegment::PiecedSegment(int32_t pieceLength, const PieceHandle& piece): _pieceLength(pieceLength), _overflowLength(0), _piece(piece) { _writtenLength = _piece->getAllMissingBlockIndexes().front()*BLOCK_LENGTH; } PiecedSegment::~PiecedSegment() {} bool PiecedSegment::complete() const { return _piece->pieceComplete(); } int32_t PiecedSegment::getIndex() const { return _piece->getIndex(); } int64_t PiecedSegment::getPosition() const { return ((int64_t)_piece->getIndex())*_pieceLength; } int64_t PiecedSegment::getPositionToWrite() const { return getPosition()+_writtenLength; } int32_t PiecedSegment::getLength() const { return _piece->getLength(); } void PiecedSegment::updateWrittenLength(int32_t bytes) { int32_t newWrittenLength = _writtenLength+bytes; if(newWrittenLength > _piece->getLength()) { _overflowLength = newWrittenLength-_piece->getLength(); newWrittenLength = _piece->getLength(); } for(int32_t i = _writtenLength/BLOCK_LENGTH; i < newWrittenLength/BLOCK_LENGTH; ++i) { _piece->completeBlock(i); } if(newWrittenLength == _piece->getLength()) { _piece->completeBlock(_piece->countBlock()-1); } _writtenLength = newWrittenLength; } void PiecedSegment::clear() { _writtenLength = 0; _overflowLength = 0; _piece->clearAllBlock(); } PieceHandle PiecedSegment::getPiece() const { return _piece; } bool PiecedSegment::operator==(const PiecedSegment& segment) const { return _piece == segment._piece; } bool PiecedSegment::operator!=(const PiecedSegment& segment) const { return !(*this == segment); }