Click or drag to resize

CachedFileStream Class

Represents a file stream that caches recently used blocks in memory.
Inheritance Hierarchy
SystemObject
  SystemMarshalByRefObject
    System.IOStream
      GSF.IOCachedFileStream

Namespace: GSF.IO
Assembly: GSF.Core (in GSF.Core.dll) Version: 2.4.207-beta+1781b796b2aa7a54013a031eb432fe4ccee31867
Syntax
public class CachedFileStream : Stream
View Source

The CachedFileStream type exposes the following members.

Constructors
 NameDescription
Public methodCachedFileStream(String, FileMode) Initializes a new instance of the FileStream class with the specified path, creation mode, read/write and sharing permission, the access other FileStreams can have to the same file, the buffer size, and additional file options.
Public methodCachedFileStream(String, FileMode, FileAccess) Initializes a new instance of the FileStream class with the specified path, creation mode, read/write and sharing permission, the access other FileStreams can have to the same file, the buffer size, and additional file options.
Public methodCachedFileStream(String, FileMode, FileAccess, FileShare) Initializes a new instance of the FileStream class with the specified path, creation mode, read/write and sharing permission, the access other FileStreams can have to the same file, the buffer size, and additional file options.
Public methodCachedFileStream(String, FileMode, FileAccess, FileShare, Int32) Initializes a new instance of the FileStream class with the specified path, creation mode, read/write and sharing permission, the access other FileStreams can have to the same file, the buffer size, and additional file options.
Public methodCachedFileStream(String, FileMode, FileAccess, FileShare, Int32, Boolean) Initializes a new instance of the FileStream class with the specified path, creation mode, read/write and sharing permission, the access other FileStreams can have to the same file, the buffer size, and additional file options.
Public methodCachedFileStream(String, FileMode, FileAccess, FileShare, Int32, FileOptions) Initializes a new instance of the FileStream class with the specified path, creation mode, read/write and sharing permission, the access other FileStreams can have to the same file, the buffer size, and additional file options.
Public methodCachedFileStream(String, FileMode, FileSystemRights, FileShare, Int32, FileOptions) Initializes a new instance of the FileStream class with the specified path, creation mode, access rights and sharing permission, the buffer size, and additional file options.
Public methodCachedFileStream(String, FileMode, FileSystemRights, FileShare, Int32, FileOptions, FileSecurity) Initializes a new instance of the FileStream class with the specified path, creation mode, access rights and sharing permission, the buffer size, additional file options, access control and audit security.
Top
Properties
 NameDescription
Public propertyBlockSize Gets the size of the cached blocks.
Public propertyCacheSize Gets or sets the maximum size of the cache.
Public propertyCanRead When overridden in a derived class, gets a value indicating whether the current stream supports reading.
(Overrides StreamCanRead)
Public propertyCanSeek When overridden in a derived class, gets a value indicating whether the current stream supports seeking.
(Overrides StreamCanSeek)
Public propertyCanTimeoutGets a value that determines whether the current stream can time out.
(Inherited from Stream)
Public propertyCanWrite When overridden in a derived class, gets a value indicating whether the current stream supports writing.
(Overrides StreamCanWrite)
Public propertyLength When overridden in a derived class, gets the length in bytes of the stream.
(Overrides StreamLength)
Public propertyPosition When overridden in a derived class, gets or sets the position within the current stream.
(Overrides StreamPosition)
Public propertyReadTimeoutGets or sets a value, in miliseconds, that determines how long the stream will attempt to read before timing out.
(Inherited from Stream)
Public propertyWriteTimeoutGets or sets a value, in miliseconds, that determines how long the stream will attempt to write before timing out.
(Inherited from Stream)
Top
Methods
 NameDescription
Public methodBeginReadBegins an asynchronous read operation. (Consider using ReadAsync(Byte, Int32, Int32) instead.)
(Inherited from Stream)
Public methodBeginWriteBegins an asynchronous write operation. (Consider using WriteAsync(Byte, Int32, Int32) instead.)
(Inherited from Stream)
Public methodCloseCloses the current stream and releases any resources (such as sockets and file handles) associated with the current stream. Instead of calling this method, ensure that the stream is properly disposed.
(Inherited from Stream)
Public methodCopyTo(Stream)Reads the bytes from the current stream and writes them to another stream.
(Inherited from Stream)
Public methodCopyTo(Stream, Int32)Reads the bytes from the current stream and writes them to another stream, using a specified buffer size.
(Inherited from Stream)
Public methodCopyToAsync(Stream)Asynchronously reads the bytes from the current stream and writes them to another stream.
(Inherited from Stream)
Public methodCopyToAsync(Stream, Int32)Asynchronously reads the bytes from the current stream and writes them to another stream, using a specified buffer size.
(Inherited from Stream)
Public methodCopyToAsync(Stream, Int32, CancellationToken)Asynchronously reads the bytes from the current stream and writes them to another stream, using a specified buffer size and cancellation token.
(Inherited from Stream)
Public methodCreateObjRefCreates an object that contains all the relevant information required to generate a proxy used to communicate with a remote object.
(Inherited from MarshalByRefObject)
Protected methodCreateWaitHandleObsolete.
Allocates a WaitHandle object.
(Inherited from Stream)
Public methodDisposeReleases all resources used by the Stream.
(Inherited from Stream)
Protected methodDispose(Boolean) Releases the unmanaged resources used by the Stream and optionally releases the managed resources.
(Overrides StreamDispose(Boolean))
Public methodEndReadWaits for the pending asynchronous read to complete. (Consider using ReadAsync(Byte, Int32, Int32) instead.)
(Inherited from Stream)
Public methodEndWriteEnds an asynchronous write operation. (Consider using WriteAsync(Byte, Int32, Int32) instead.)
(Inherited from Stream)
Public methodEqualsDetermines whether the specified object is equal to the current object.
(Inherited from Object)
Protected methodFinalizeAllows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection.
(Inherited from Object)
Public methodFlush Clears buffers for this stream and causes any buffered data to be written to the file.
(Overrides StreamFlush)
Public methodFlush(Boolean) Clears buffers for this stream and causes any buffered data to be written to the file, and also clears all intermediate file buffers.
Public methodFlushAsyncAsynchronously clears all buffers for this stream and causes any buffered data to be written to the underlying device.
(Inherited from Stream)
Public methodFlushAsync(CancellationToken)Asynchronously clears all buffers for this stream, causes any buffered data to be written to the underlying device, and monitors cancellation requests.
(Inherited from Stream)
Public methodGetHashCodeServes as the default hash function.
(Inherited from Object)
Public methodGetLifetimeServiceRetrieves the current lifetime service object that controls the lifetime policy for this instance.
(Inherited from MarshalByRefObject)
Public methodGetTypeGets the Type of the current instance.
(Inherited from Object)
Public methodInitializeLifetimeServiceObtains a lifetime service object to control the lifetime policy for this instance.
(Inherited from MarshalByRefObject)
Protected methodMemberwiseCloneCreates a shallow copy of the current Object.
(Inherited from Object)
Protected methodMemberwiseClone(Boolean)Creates a shallow copy of the current MarshalByRefObject object.
(Inherited from MarshalByRefObject)
Protected methodObjectInvariantObsolete.
Provides support for a Contract.
(Inherited from Stream)
Public methodRead When overridden in a derived class, reads a sequence of bytes from the current stream and advances the position within the stream by the number of bytes read.
(Overrides StreamRead(Byte, Int32, Int32))
Public methodReadAsync(Byte, Int32, Int32)Asynchronously reads a sequence of bytes from the current stream and advances the position within the stream by the number of bytes read.
(Inherited from Stream)
Public methodReadAsync(Byte, Int32, Int32, CancellationToken)Asynchronously reads a sequence of bytes from the current stream, advances the position within the stream by the number of bytes read, and monitors cancellation requests.
(Inherited from Stream)
Public methodReadByteReads a byte from the stream and advances the position within the stream by one byte, or returns -1 if at the end of the stream.
(Inherited from Stream)
Public methodSeek When overridden in a derived class, sets the position within the current stream.
(Overrides StreamSeek(Int64, SeekOrigin))
Public methodSetLength When overridden in a derived class, sets the length of the current stream.
(Overrides StreamSetLength(Int64))
Public methodToStringReturns a string that represents the current object.
(Inherited from Object)
Public methodWrite When overridden in a derived class, writes a sequence of bytes to the current stream and advances the current position within this stream by the number of bytes written.
(Overrides StreamWrite(Byte, Int32, Int32))
Public methodWriteAsync(Byte, Int32, Int32)Asynchronously writes a sequence of bytes to the current stream and advances the current position within this stream by the number of bytes written.
(Inherited from Stream)
Public methodWriteAsync(Byte, Int32, Int32, CancellationToken)Asynchronously writes a sequence of bytes to the current stream, advances the current position within this stream by the number of bytes written, and monitors cancellation requests.
(Inherited from Stream)
Public methodWriteByteWrites a byte to the current position in the stream and advances the position within the stream by one byte.
(Inherited from Stream)
Top
Fields
 NameDescription
Public fieldStatic memberDefaultBlockSize Default value for the BlockSize property.
Public fieldStatic memberDefaultCacheSize Default value for the CacheSize property.
Top
Extension Methods
 NameDescription
Public Extension MethodCompress Compress a stream using specified compression strength.
(Defined by CompressionExtensions)
Public Extension MethodCompress Compress a stream onto given output stream using specified compression strength.
(Defined by CompressionExtensions)
Public Extension MethodCopyStream Copies input Stream onto output Stream.
(Defined by StreamExtensions)
Public Extension MethodCopyTo Writes the contents of a stream to the provided stream.
(Defined by StreamExtensions)
Public Extension MethodDecompress Decompress a stream.
(Defined by CompressionExtensions)
Public Extension MethodDecompress Decompress a stream onto given output stream.
(Defined by CompressionExtensions)
Public Extension MethodDecrypt Returns a stream of decrypted data for the given parameters.
(Defined by Cipher)
Public Extension MethodDecrypt Decrypts input stream onto output stream for the given parameters.
(Defined by Cipher)
Public Extension MethodDeserializeToDataSet Deserializes a DataSet from a Stream.
(Defined by DataSetExtensions)
Public Extension MethodEncrypt Returns a stream of encrypted data for the given parameters.
(Defined by Cipher)
Public Extension MethodEncrypt Encrypts input stream onto output stream for the given parameters.
(Defined by Cipher)
Public Extension MethodGetEnumValueOrDefault Gets the enumeration constant for value, if defined in the enumeration, or a default value.
(Defined by EnumExtensions)
Public Extension MethodGetEnumValueOrDefaultT Gets the enumeration constant for this value, if defined in the enumeration, or a default value.
(Defined by EnumExtensions)
Public Extension MethodRead7BitUInt32 Reads the 7-bit encoded value from the stream.
(Defined by StreamExtensions)
Public Extension MethodReadAll Reads all of the provided bytes. Will not return prematurely, but continue to execute a Read(Byte, Int32, Int32) command until the entire length has been read.
(Defined by StreamExtensions)
Public Extension MethodReadBoolean Reads the value from the stream in little endian format.
(Defined by StreamExtensions)
Public Extension MethodReadBytes Reads a byte array from a Stream. The number of bytes should be prefixed in the stream.
(Defined by StreamExtensions)
Public Extension MethodReadBytes Reads a byte array from a Stream. The number of bytes should be prefixed in the stream.
(Defined by StreamExtensions)
Public Extension MethodReadChar Reads the value from the stream in little endian format.
(Defined by StreamExtensions)
Public Extension MethodReadDateTime Reads the value from the stream in little endian format.
(Defined by StreamExtensions)
Public Extension MethodReadDecimal Reads a decimal from the stream in Little Endian bytes.
(Defined by StreamExtensions)
Public Extension MethodReadDouble Reads the value from the stream in little endian format.
(Defined by StreamExtensions)
Public Extension MethodReadGuid Reads a Guid from the stream in Little Endian bytes.
(Defined by StreamExtensions)
Public Extension MethodReadInt16 Reads the value from the stream in little endian format.
(Defined by StreamExtensions)
Public Extension MethodReadInt32 Reads the value from the stream in little endian format.
(Defined by StreamExtensions)
Public Extension MethodReadInt32Collection Writes the supplied string to a Stream in UTF8 encoding.
(Defined by StreamExtensions)
Public Extension MethodReadInt64 Reads the value from the stream in little endian format.
(Defined by StreamExtensions)
Public Extension MethodReadNextByte Read a byte from the stream. Will throw an exception if the end of the stream has been reached.
(Defined by StreamExtensions)
Public Extension MethodReadNullableString Reads a string from a Stream that was encoded in UTF8. Value can be null and is prefixed with a boolean.
(Defined by StreamExtensions)
Public Extension MethodReadObject Reads an object from a stream.
(Defined by StreamExtensions)
Public Extension MethodReadSByte Reads the value from the stream in little endian format.
(Defined by StreamExtensions)
Public Extension MethodReadSingle Reads the value from the stream in little endian format.
(Defined by StreamExtensions)
Public Extension MethodReadStream Reads entire Stream contents, and returns Byte array of data.
(Defined by StreamExtensions)
Public Extension MethodReadString Reads a string from a Stream that was encoded in UTF8.
(Defined by StreamExtensions)
Public Extension MethodReadStringCollection Writes the supplied string to a Stream in UTF8 encoding.
(Defined by StreamExtensions)
Public Extension MethodReadUInt16 Reads the value from the stream in little endian format.
(Defined by StreamExtensions)
Public Extension MethodReadUInt32 Reads the value from the stream in little endian format.
(Defined by StreamExtensions)
Public Extension MethodReadUInt64 Reads the value from the stream in little endian format.
(Defined by StreamExtensions)
Public Extension MethodWrite Writes the supplied value to stream in little endian format.
(Defined by StreamExtensions)
Public Extension MethodWrite Writes the supplied value to in little endian format.
(Defined by StreamExtensions)
Public Extension MethodWrite Writes the entire buffer to the stream
(Defined by StreamExtensions)
Public Extension MethodWrite Writes the supplied value to stream in little endian format.
(Defined by StreamExtensions)
Public Extension MethodWrite Writes the supplied value to stream in little endian format.
(Defined by StreamExtensions)
Public Extension MethodWrite Writes the supplied string to a Stream in UTF8 encoding.
(Defined by StreamExtensions)
Public Extension MethodWrite Writes the supplied value to stream in little endian format.
(Defined by StreamExtensions)
Public Extension MethodWrite Writes a guid in little endian bytes to the stream
(Defined by StreamExtensions)
Public Extension MethodWrite Writes the supplied value to stream in little endian format.
(Defined by StreamExtensions)
Public Extension MethodWrite Writes the supplied value to stream in little endian format.
(Defined by StreamExtensions)
Public Extension MethodWrite Writes the supplied value to stream in little endian format.
(Defined by StreamExtensions)
Public Extension MethodWrite Writes the supplied value to stream in little endian format.
(Defined by StreamExtensions)
Public Extension MethodWrite Writes the supplied value to stream in little endian format.
(Defined by StreamExtensions)
Public Extension MethodWrite Writes the supplied string to a Stream in UTF8 encoding.
(Defined by StreamExtensions)
Public Extension MethodWrite Writes the supplied value to stream in little endian format.
(Defined by StreamExtensions)
Public Extension MethodWrite Writes the supplied value to stream in little endian format.
(Defined by StreamExtensions)
Public Extension MethodWrite Writes the supplied value to stream in little endian format.
(Defined by StreamExtensions)
Public Extension MethodWriteCollection Writes the supplied Collection to a Stream.
(Defined by StreamExtensions)
Public Extension MethodWriteCollection Writes the supplied Collection to a Stream.
(Defined by StreamExtensions)
Public Extension MethodWriteNullable Writes the supplied string to a Stream in UTF8 encoding with a prefix if the value is null
(Defined by StreamExtensions)
Public Extension MethodWriteObject Encodes an object on a stream.
(Defined by StreamExtensions)
Public Extension MethodWriteWithLength Writes the supplied value to stream along with prefixing the length so it can be properly read as a unit.
(Defined by StreamExtensions)
Top
Remarks

This class was developed as a wrapper around FileStream with the same basic functionality. This stream treats the file as a collection of contiguous blocks which can be loaded into an in-memory lookup table to greatly improve seek times. This class generally performs significantly better than the standard FileStream class when maintaining multiple file pointers and frequently seeking back and forth between them. For an example of a good use-case, consider copying a list of serialized objects from one location in the file to another without loading the whole list into memory.

This class likely will not perform significantly better or worse than the standard FileStream when performing strictly sequential reads or writes. For an example of a bad use-case, consider reading a list of objects into memory or simply appending new data to the end of the file. In both of these cases, it may be better to use the standard FileStream for less memory overhead and more predictable flush behavior.

Blocks are loaded into memory as they are used (for read/write operations) and will be kept in memory for as long as possible. Blocks can only be flushed from the cache by decreasing the cache size or by accessing a non-cached block when the cache is already full. In these cases, a least recently used algorithm is executed to determine which blocks should be removed from the cache either to decrease the size of the cache or to make room for a new block.

Write operations are also cached such that they will not be committed to the file until the block is removed from the cache or a call to Flush has been made. This can have some additional implications as compared to the standard FileStream. For instance, write operations may not be committed to the file in the order in which they were executed. Users of this class will need to be judicious about when and how often they call Flush.

See Also