Skip to content

Commit c3e55d9

Browse files
author
rich.quackenbush
committed
Fix for NullReferenceException for seekable download stream:
https://jira.mongodb.org/browse/CSHARP-1489
1 parent fc62bab commit c3e55d9

File tree

2 files changed

+33
-6
lines changed

2 files changed

+33
-6
lines changed

src/MongoDB.Driver.GridFS.Tests/GridFSDownloadStreamBaseTests.cs

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,35 @@ namespace MongoDB.Driver.GridFS.Tests
2929
{
3030
[TestFixture]
3131
public class GridFSDownloadStreamBaseTests
32-
{
33-
// public methods
32+
{
33+
// public methods
34+
[Test]
35+
public void Read_should_read(
36+
[Values(0.5, 1.0, 1.5, 2.0, 2.5)] double contentSizeMultiple,
37+
[Values(false, true)] bool async,
38+
[Values(false, true)] bool seekable)
39+
{
40+
var bucket = CreateBucket(128);
41+
var contentSize = (int)(bucket.Options.ChunkSizeBytes * contentSizeMultiple);
42+
var content = CreateContent(contentSize);
43+
var id = CreateGridFSFile(bucket, content);
44+
var options = new GridFSDownloadOptions() { Seekable = seekable };
45+
var subject = bucket.OpenDownloadStream(id, options);
46+
47+
var destination = new byte[contentSize];
48+
49+
if (async)
50+
{
51+
subject.ReadAsync(destination, 0, contentSize).GetAwaiter().GetResult();
52+
}
53+
else
54+
{
55+
subject.Read(destination, 0, contentSize);
56+
}
57+
58+
destination.Should().Equal(content);
59+
}
60+
3461
[Test]
3562
public void CopyTo_should_copy_stream(
3663
[Values(0.0, 0.5, 1.0, 1.5, 2.0, 2.5)] double contentSizeMultiple,

src/MongoDB.Driver.GridFS/GridFSSeekableDownloadStream.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ private byte[] GetChunkHelper(long n, List<BsonDocument> documents)
186186
var data = document["data"].AsBsonBinaryData.Bytes;
187187

188188
var chunkSizeBytes = FileInfo.ChunkSizeBytes;
189-
var lastChunk = 0;
189+
var lastChunk = FileInfo.Length / FileInfo.ChunkSizeBytes;
190190
var expectedChunkSize = n == lastChunk ? FileInfo.Length % chunkSizeBytes : chunkSizeBytes;
191191
if (data.Length != expectedChunkSize)
192192
{
@@ -201,12 +201,12 @@ private byte[] GetChunkHelper(long n, List<BsonDocument> documents)
201201
private ArraySegment<byte> GetSegment(CancellationToken cancellationToken)
202202
{
203203
var n = _position / FileInfo.ChunkSizeBytes;
204-
if (_n != n)
204+
if (_n != n || _chunk == null)
205205
{
206206
GetChunk(n, cancellationToken);
207207
}
208208

209-
var segmentOffset = (int)(_position % FileInfo.ChunkSizeBytes);
209+
var segmentOffset = (int)(_position % FileInfo.ChunkSizeBytes);
210210
var segmentCount = _chunk.Length - segmentOffset;
211211

212212
return new ArraySegment<byte>(_chunk, segmentOffset, segmentCount);
@@ -215,7 +215,7 @@ private ArraySegment<byte> GetSegment(CancellationToken cancellationToken)
215215
private async Task<ArraySegment<byte>> GetSegmentAsync(CancellationToken cancellationToken)
216216
{
217217
var n = _position / FileInfo.ChunkSizeBytes;
218-
if (_n != n)
218+
if (_n != n || _chunk == null)
219219
{
220220
await GetChunkAsync(n, cancellationToken).ConfigureAwait(false);
221221
}

0 commit comments

Comments
 (0)