C# Class MongoDB.GridFS.GridFileStream

Stream for reading and writing to a file in GridFS.
When using the stream for random io it is possible to produce chunks in the begining and middle of the file that are not full size followed by other chunks that are full size. This only affects the md5 sum that is calculated on the file on close. Because of this do not rely on the md5 sum of a file when doing random io. Writing to the stream sequentially works fine and will produce a consistent md5.
Inheritance: Stream
Datei anzeigen Open project: sdether/mongodb-csharp Class Usage Examples

Public Methods

Method Description
Close ( ) : void

Close the stream and flush any changes to the database.

Flush ( ) : void

Flushes any changes to current chunk to the database. It can be called in client code at any time or it will automatically be called on Close() and when the stream position moves off the bounds of the current chunk.

GridFileStream ( GridFileInfo gridfileinfo, IMongoCollection files, IMongoCollection chunks, FileAccess access ) : System
Read ( byte array, int offset, int count ) : int

Reads data from the stream into the specified array. It will fill the array in starting at offset and adding count bytes returning the number of bytes read from the stream.

Seek ( long offset, SeekOrigin origin ) : long

Seek to any location in the stream. Seeking past the end of the file is allowed. Any writes to that location will cause the file to grow to that size. Any holes that may be created from the seek will be zero filled on close.

SetLength ( long value ) : void

Sets the length of this stream to the given value.

Write ( byte array, int offset, int count ) : void

Copies from the source array into the grid file.

Protected Methods

Method Description
Dispose ( bool disposing ) : void

Private Methods

Method Description
CalcChunkNum ( long position ) : int
EnsureNoHoles ( ) : void

Makes sure that at least a skelton chunk exists for all numbers. If not the MD5 calculation will fail on a sparse file.

LoadOrCreateChunk ( int num ) : void

Loads a chunk from the chunks collection if it exists. Otherwise it creates a blank chunk Document.

MoveTo ( long position ) : void

Moves the current position to the new position. If this causes a new chunk to need to be loaded it will take care of flushing the buffer and loading a new chunk.

TruncateAfter ( long value ) : void

Deletes all chunks after the specified position and clears out any extra bytes if the position doesn't fall on a chunk boundry.

ValidateReadState ( byte array, int offset, int count ) : void
ValidateWriteState ( byte array, int offset, int count ) : void

Method Details

Close() public method

Close the stream and flush any changes to the database.
public Close ( ) : void
return void

Dispose() protected method

protected Dispose ( bool disposing ) : void
disposing bool
return void

Flush() public method

Flushes any changes to current chunk to the database. It can be called in client code at any time or it will automatically be called on Close() and when the stream position moves off the bounds of the current chunk.
public Flush ( ) : void
return void

GridFileStream() public method

public GridFileStream ( GridFileInfo gridfileinfo, IMongoCollection files, IMongoCollection chunks, FileAccess access ) : System
gridfileinfo GridFileInfo
files IMongoCollection
chunks IMongoCollection
access FileAccess
return System

Read() public method

Reads data from the stream into the specified array. It will fill the array in starting at offset and adding count bytes returning the number of bytes read from the stream.
public Read ( byte array, int offset, int count ) : int
array byte
offset int
count int
return int

Seek() public method

Seek to any location in the stream. Seeking past the end of the file is allowed. Any writes to that location will cause the file to grow to that size. Any holes that may be created from the seek will be zero filled on close.
public Seek ( long offset, SeekOrigin origin ) : long
offset long
origin SeekOrigin
return long

SetLength() public method

Sets the length of this stream to the given value.
public SetLength ( long value ) : void
value long /// A ///
return void

Write() public method

Copies from the source array into the grid file.
public Write ( byte array, int offset, int count ) : void
array byte /// A The source array to copy from. ///
offset int /// A The offset within the source array. ///
count int /// A The number of bytes from within the source array to copy. ///
return void