Skip to content

Commit 7e803dd

Browse files
committed
Use Pieces.Length for NumberOfPieces calculation
#48
1 parent d8b14fe commit 7e803dd

File tree

2 files changed

+8
-9
lines changed

2 files changed

+8
-9
lines changed

BencodeNET.Tests/Torrents/TorrentTests.cs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -192,20 +192,17 @@ public void TotalSize_UnknownFileMode_IsZero()
192192

193193
[Theory]
194194
[AutoMockedData]
195-
public void NumberOfPieces_ShouldBeTotalSizeDividedByPieceSizeRoundedUp(long fileSize, long pieceSize)
195+
public void NumberOfPieces_ShouldBePiecesLengthDividedByPieceSizeRoundedUp(int piecesByteCount, long pieceSize)
196196
{
197-
var expected = (int)Math.Ceiling((double) fileSize/pieceSize);
197+
var expected = (int)Math.Ceiling((double) piecesByteCount/pieceSize);
198198

199199
var torrent = new Torrent
200200
{
201-
File = new SingleFileInfo
202-
{
203-
FileSize = fileSize
204-
},
201+
Pieces = new byte[piecesByteCount],
205202
PieceSize = pieceSize
206203
};
207204

208-
torrent.TotalSize.Should().Be(fileSize);
205+
torrent.Pieces.Length.Should().Be(piecesByteCount);
209206
torrent.NumberOfPieces.Should().Be(expected);
210207
}
211208

BencodeNET/Torrents/Torrent.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ public virtual string DisplayName
164164
/// A concatenation of all 20-byte SHA1 hash values (one for each piece).
165165
/// Use <see cref="PiecesAsHexString"/> to get/set this value as a hex string instead.
166166
/// </summary>
167-
public virtual byte[] Pieces { get; set; }
167+
public virtual byte[] Pieces { get; set; } = new byte[0];
168168

169169
/// <summary>
170170
/// Gets or sets <see cref="Pieces"/> from/to a hex string (without dashes), e.g. 1C115D26444AEF2A5E936133DCF8789A552BBE9F[...].
@@ -218,7 +218,9 @@ public virtual long TotalSize
218218
/// <summary>
219219
/// The total number of file pieces.
220220
/// </summary>
221-
public virtual int NumberOfPieces => (int)Math.Ceiling((double)TotalSize / PieceSize);
221+
public virtual int NumberOfPieces => Pieces != null
222+
? (int) Math.Ceiling((double) Pieces.Length / PieceSize)
223+
: 0;
222224

223225
/// <summary>
224226
/// Converts the torrent to a <see cref="BDictionary"/>.

0 commit comments

Comments
 (0)