2016-04-04 03:48:39 +00:00
|
|
|
package metainfo
|
|
|
|
|
2018-07-11 23:15:15 +00:00
|
|
|
import (
|
|
|
|
"github.com/anacrolix/missinggo"
|
|
|
|
)
|
2016-04-04 03:48:39 +00:00
|
|
|
|
|
|
|
type Piece struct {
|
2016-08-26 10:29:05 +00:00
|
|
|
Info *Info
|
2018-07-11 23:15:15 +00:00
|
|
|
i pieceIndex
|
2016-04-04 03:48:39 +00:00
|
|
|
}
|
|
|
|
|
2018-07-11 23:15:15 +00:00
|
|
|
type pieceIndex = int
|
|
|
|
|
2016-04-19 04:11:11 +00:00
|
|
|
func (p Piece) Length() int64 {
|
2018-07-11 23:15:15 +00:00
|
|
|
if int(p.i) == p.Info.NumPieces()-1 {
|
2016-04-19 04:11:11 +00:00
|
|
|
return p.Info.TotalLength() - int64(p.i)*p.Info.PieceLength
|
2016-04-04 03:48:39 +00:00
|
|
|
}
|
2016-04-19 04:11:11 +00:00
|
|
|
return p.Info.PieceLength
|
2016-04-04 03:48:39 +00:00
|
|
|
}
|
|
|
|
|
2016-04-19 04:11:11 +00:00
|
|
|
func (p Piece) Offset() int64 {
|
|
|
|
return int64(p.i) * p.Info.PieceLength
|
2016-04-04 03:48:39 +00:00
|
|
|
}
|
|
|
|
|
2016-04-19 04:11:11 +00:00
|
|
|
func (p Piece) Hash() (ret Hash) {
|
2017-12-29 01:17:58 +00:00
|
|
|
missinggo.CopyExact(&ret, p.Info.Pieces[p.i*HashSize:(p.i+1)*HashSize])
|
2016-04-04 03:48:39 +00:00
|
|
|
return
|
|
|
|
}
|
2016-06-20 07:51:05 +00:00
|
|
|
|
2018-07-11 23:15:15 +00:00
|
|
|
func (p Piece) Index() pieceIndex {
|
2016-06-20 07:51:05 +00:00
|
|
|
return p.i
|
|
|
|
}
|