@@ -40,7 +40,7 @@ public class MongoGridFSStream : Stream
40
40
private long _length ;
41
41
private long _position ;
42
42
private byte [ ] _chunk ;
43
- private int _chunkIndex = - 1 ; // -1 means no chunk is loaded
43
+ private long _chunkIndex = - 1 ; // -1 means no chunk is loaded
44
44
private BsonValue _chunkId ;
45
45
private bool _chunkIsDirty ;
46
46
private bool _fileIsDirty ;
@@ -248,7 +248,7 @@ public override int Read(byte[] buffer, int offset, int count)
248
248
if ( _disposed ) { throw new ObjectDisposedException ( "MongoGridFSStream" ) ; }
249
249
var available = _length - _position ;
250
250
if ( count > available ) { count = ( int ) available ; }
251
- var chunkIndex = ( int ) ( _position / _fileInfo . ChunkSize ) ;
251
+ var chunkIndex = _position / _fileInfo . ChunkSize ;
252
252
var chunkOffset = ( int ) ( _position % _fileInfo . ChunkSize ) ;
253
253
var bytesRead = 0 ;
254
254
while ( count > 0 )
@@ -276,7 +276,7 @@ public override int ReadByte()
276
276
if ( _disposed ) { throw new ObjectDisposedException ( "MongoGridFSStream" ) ; }
277
277
if ( _position < _length )
278
278
{
279
- var chunkIndex = ( int ) ( _position / _fileInfo . ChunkSize ) ;
279
+ var chunkIndex = _position / _fileInfo . ChunkSize ;
280
280
var chunkOffset = ( int ) ( _position % _fileInfo . ChunkSize ) ;
281
281
if ( _chunkIndex != chunkIndex ) { LoadChunk ( chunkIndex ) ; }
282
282
var b = _chunk [ chunkOffset ] ;
@@ -328,7 +328,7 @@ public override void SetLength(long value)
328
328
}
329
329
_fileIsDirty = true ;
330
330
331
- var lastChunkIndex = ( int ) ( ( _length + _fileInfo . ChunkSize - 1 ) / _fileInfo . ChunkSize ) - 1 ;
331
+ var lastChunkIndex = ( ( _length + _fileInfo . ChunkSize - 1 ) / _fileInfo . ChunkSize ) - 1 ;
332
332
if ( _chunkIndex == lastChunkIndex )
333
333
{
334
334
// if the current chunk is the new last chunk mark it dirty (its size has changed)
@@ -352,7 +352,7 @@ public override void SetLength(long value)
352
352
public override void Write ( byte [ ] buffer , int offset , int count )
353
353
{
354
354
if ( _disposed ) { throw new ObjectDisposedException ( "MongoGridFSStream" ) ; }
355
- var chunkIndex = ( int ) ( _position / _fileInfo . ChunkSize ) ;
355
+ var chunkIndex = _position / _fileInfo . ChunkSize ;
356
356
var chunkOffset = ( int ) ( _position % _fileInfo . ChunkSize ) ;
357
357
while ( count > 0 )
358
358
{
@@ -398,7 +398,7 @@ public override void Write(byte[] buffer, int offset, int count)
398
398
public override void WriteByte ( byte value )
399
399
{
400
400
if ( _disposed ) { throw new ObjectDisposedException ( "MongoGridFSStream" ) ; }
401
- var chunkIndex = ( int ) ( _position / _fileInfo . ChunkSize ) ;
401
+ var chunkIndex = _position / _fileInfo . ChunkSize ;
402
402
var chunkOffset = ( int ) ( _position % _fileInfo . ChunkSize ) ;
403
403
if ( _chunkIndex != chunkIndex )
404
404
{
@@ -448,12 +448,12 @@ private void AddMissingChunks()
448
448
{
449
449
var query = Query . EQ ( "files_id" , _fileInfo . Id ) ;
450
450
var fields = Fields . Include ( "n" ) ;
451
- var chunkCount = ( int ) ( ( _length + _fileInfo . ChunkSize - 1 ) / _fileInfo . ChunkSize ) ;
452
- var chunksFound = new HashSet < int > ( ) ;
451
+ var chunkCount = ( _length + _fileInfo . ChunkSize - 1 ) / _fileInfo . ChunkSize ;
452
+ var chunksFound = new HashSet < long > ( ) ;
453
453
var foundExtraChunks = false ;
454
454
foreach ( var chunk in _gridFS . Chunks . Find ( query ) . SetFields ( fields ) )
455
455
{
456
- var n = chunk [ "n" ] . ToInt32 ( ) ;
456
+ var n = chunk [ "n" ] . ToInt64 ( ) ;
457
457
chunksFound . Add ( n ) ;
458
458
if ( n >= chunkCount )
459
459
{
@@ -468,7 +468,7 @@ private void AddMissingChunks()
468
468
}
469
469
470
470
BsonBinaryData zeros = null ; // delay creating it until it's actually needed
471
- for ( var n = 0 ; n < chunkCount ; n ++ )
471
+ for ( var n = 0L ; n < chunkCount ; n ++ )
472
472
{
473
473
if ( ! chunksFound . Contains ( n ) )
474
474
{
@@ -480,15 +480,15 @@ private void AddMissingChunks()
480
480
{
481
481
{ "_id" , ObjectId . GenerateNewId ( ) } ,
482
482
{ "files_id" , _fileInfo . Id } ,
483
- { "n" , n } ,
483
+ { "n" , ( n < int . MaxValue ) ? ( BsonValue ) BsonInt32 . Create ( ( int ) n ) : BsonInt64 . Create ( n ) } ,
484
484
{ "data" , zeros }
485
485
} ;
486
486
_gridFS . Chunks . Insert ( missingChunk ) ;
487
487
}
488
488
}
489
489
}
490
490
491
- private void LoadChunk ( int chunkIndex )
491
+ private void LoadChunk ( long chunkIndex )
492
492
{
493
493
if ( _chunkIsDirty ) { SaveChunk ( ) ; }
494
494
var query = Query . And ( Query . EQ ( "files_id" , _fileInfo . Id ) , Query . EQ ( "n" , chunkIndex ) ) ;
@@ -526,7 +526,7 @@ private void LoadChunk(int chunkIndex)
526
526
_chunkIndex = chunkIndex ;
527
527
}
528
528
529
- private void LoadChunkNoData ( int chunkIndex )
529
+ private void LoadChunkNoData ( long chunkIndex )
530
530
{
531
531
if ( _chunkIsDirty ) { SaveChunk ( ) ; }
532
532
if ( _chunk == null )
@@ -599,7 +599,7 @@ private void OpenTruncate()
599
599
600
600
private void SaveChunk ( )
601
601
{
602
- var lastChunkIndex = ( int ) ( ( _length + _fileInfo . ChunkSize - 1 ) / _fileInfo . ChunkSize ) - 1 ;
602
+ var lastChunkIndex = ( _length + _fileInfo . ChunkSize - 1 ) / _fileInfo . ChunkSize - 1 ;
603
603
if ( _chunkIndex == - 1 || _chunkIndex > lastChunkIndex )
604
604
{
605
605
var message = string . Format ( "Invalid chunk index {0}." , _chunkIndex ) ;
@@ -629,7 +629,7 @@ private void SaveChunk()
629
629
{
630
630
{ "_id" , _chunkId } ,
631
631
{ "files_id" , _fileInfo . Id } ,
632
- { "n" , _chunkIndex } ,
632
+ { "n" , ( _chunkIndex < int . MaxValue ) ? ( BsonValue ) BsonInt32 . Create ( ( int ) _chunkIndex ) : BsonInt64 . Create ( _chunkIndex ) } ,
633
633
{ "data" , data }
634
634
} ;
635
635
_gridFS . Chunks . Update ( query , update , UpdateFlags . Upsert ) ;
0 commit comments