如何将部分预缓存的 MemoryStream 与 FileStream 结合起来?

How to combine partially pre-cached MemoryStream with FileStream?

对于时间紧迫的媒体演示应用程序,重要的是媒体文件在用户选择它时立即显示。这些文件驻留在一个真正庞大的目录结构中,由数千个媒体文件组成。

显然,将媒体文件缓存在 MemoryStream 中是可行的方法;然而,由于文件的数量庞大,完全缓存每个文件是不可行的。相反,我的想法是预先缓存每个文件的某个缓冲区,一旦文件出现,就从该缓存中播放,直到文件的其余部分从硬盘加载。

我没有看到的是如何“连接”MemoryStreamFileStream 以提供无缝播放体验。我在数据流方面还不是很强,我看到了几个问题:

请注意,我不需要写入权限——读取权限足以解决手头的问题。此外,这个问题类似于 Composite Stream Wrapper providing partial MemoryStream and full original Stream,但解决方案提供了 Windows Phone 8 的错误修复,但不适用于我的情况。

我非常想拓宽我对此相当有限的理解,因此非常感谢任何帮助。

我会建议类似以下解决方案:

  • FileStream
  • 继承你自己的CachableFileStream
  • 实现一个非常简单的 Cache,它使用您喜欢的数据结构(例如 Queue
  • 允许Preload将数据写入内部缓存
  • 允许Reload将数据写入内部缓存
  • 以某种方式修改原始 Read 行为,使用您的缓存

为了让您了解我的想法,我会建议一些实现方式,如下所示:

用法可能是这样的:

CachableFileStream cachedStream = new CachableFileStream(...)
{
    PreloadSize = 8192,
    ReloadSize = 4096,
};

// Force preloading data into the cache
cachedStream.Preload();

...
cachedStream.Read(buffer, 0, buffer.Length);
...

警告:下面的代码既没有经过正确测试也不理想 - 这只是给你一个想法!

CachableFileStream class:

using System;
using System.IO;
using System.Threading.Tasks;

/// <summary>
/// Represents a filestream with cache.
/// </summary>
public class CachableFileStream : FileStream
{
    private Cache<byte> cache;
    private int preloadSize;
    private int reloadSize;

    /// <summary>
    /// Gets or sets the amount of bytes to be preloaded.
    /// </summary>
    public int PreloadSize
    {
        get
        {
            return this.preloadSize;
        }

        set
        {
            if (value <= 0)
                throw new ArgumentOutOfRangeException(nameof(value), "The specified preload size must not be smaller than or equal to zero.");

            this.preloadSize = value;
        }
    }

    /// <summary>
    /// Gets or sets the amount of bytes to be reloaded.
    /// </summary>
    public int ReloadSize
    {
        get
        {
            return this.reloadSize;
        }

        set
        {
            if (value <= 0)
                throw new ArgumentOutOfRangeException(nameof(value), "The specified reload size must not be smaller than or equal to zero.");

            this.reloadSize = value;
        }
    }

    /// <summary>
    /// Initializes a new instance of the <see cref="CachableFileStream"/> class with the specified path and creation mode.
    /// </summary>
    /// <param name="path">A relative or absolute path for the file that the current CachableFileStream object will encapsulate</param>
    /// <param name="mode">A constant that determines how to open or create the file.</param>
    /// <exception cref="System.ArgumentException">
    /// Path is an empty string (""), contains only white space, or contains one or more invalid characters.
    /// -or- path refers to a non-file device, such as "con:", "com1:", "lpt1:", etc. in an NTFS environment.
    /// </exception>
    /// <exception cref="System.NotSupportedException">
    /// Path refers to a non-file device, such as "con:", "com1:", "lpt1:", etc. in a non-NTFS environment.
    /// </exception>
    /// <exception cref="System.ArgumentNullException">
    /// Path is null.
    /// </exception>
    /// <exception cref="System.Security.SecurityException">
    /// The caller does not have the required permission.
    /// </exception>
    /// <exception cref="System.IO.FileNotFoundException">
    /// The file cannot be found, such as when mode is FileMode.Truncate or FileMode.Open, and the file specified by path does not exist.
    /// The file must already exist in these modes.
    /// </exception>
    /// <exception cref="System.IO.IOException">
    /// An I/O error, such as specifying FileMode.CreateNew when the file specified by path already exists, occurred.-or-The stream has been closed.
    /// </exception>
    /// <exception cref="System.IO.DirectoryNotFoundException">
    /// The specified path is invalid, such as being on an unmapped drive.
    /// </exception>
    /// <exception cref="System.IO.PathTooLongException">
    /// The specified path, file name, or both exceed the system-defined maximum length.
    /// For example, on Windows-based platforms, paths must be less than 248 characters, and file names must be less than 260 characters.
    /// </exception>
    /// <exception cref="System.ArgumentOutOfRangeException">
    /// Mode contains an invalid value
    /// </exception>
    public CachableFileStream(string path, FileMode mode) : base(path, mode)
    {
        this.cache = new Cache<byte>();
        this.cache.CacheIsRunningLow += CacheIsRunningLow;
    }

    /// <summary>
    /// Reads a block of bytes from the stream and writes the data in a given buffer.
    /// </summary>
    /// <param name="array">
    /// When this method returns, contains the specified byte array with the values between
    /// offset and (offset + count - 1) replaced by the bytes read from the current source.
    /// </param>
    /// <param name="offset">The byte offset in array at which the read bytes will be placed.</param>
    /// <param name="count">The maximum number of bytes to read.</param>
    /// <returns>
    /// The total number of bytes read into the buffer. This might be less than the number
    /// of bytes requested if that number of bytes are not currently available, or zero
    /// if the end of the stream is reached.
    /// </returns>
    /// <exception cref="System.ArgumentNullException">
    /// Array is null.
    /// </exception>
    /// <exception cref="System.ArgumentOutOfRangeException">
    /// Offset or count is negative.
    /// </exception>
    /// <exception cref="System.NotSupportedException">
    /// The stream does not support reading.
    /// </exception>
    /// <exception cref="System.IO.IOException">
    /// An I/O error occurred.
    /// </exception>
    /// <exception cref="System.ArgumentException">
    /// Offset and count describe an invalid range in array.
    /// </exception>
    /// <exception cref="System.ObjectDisposedException">
    /// Methods were called after the stream was closed.
    /// </exception>
    public override int Read(byte[] array, int offset, int count)
    {
        int readBytesFromCache;

        for (readBytesFromCache = 0; readBytesFromCache < count; readBytesFromCache++)
        {
            if (this.cache.Size == 0)
                break;

            array[offset + readBytesFromCache] = this.cache.Read();
        }

        if (readBytesFromCache < count)
            readBytesFromCache += base.Read(array, offset + readBytesFromCache, count - readBytesFromCache);

        return readBytesFromCache;
    }

    /// <summary>
    /// Preload data into the cache.
    /// </summary>
    public void Preload()
    {
        this.LoadBytesFromStreamIntoCache(this.PreloadSize);
    }

    /// <summary>
    /// Reload data into the cache.
    /// </summary>
    public void Reload()
    {
        this.LoadBytesFromStreamIntoCache(this.ReloadSize);
    }

    /// <summary>
    /// Loads bytes from the stream into the cache.
    /// </summary>
    /// <param name="count">The number of bytes to read.</param>
    private void LoadBytesFromStreamIntoCache(int count)
    {
        byte[] buffer = new byte[count];
        int readBytes = base.Read(buffer, 0, buffer.Length);

        this.cache.AddRange(buffer, 0, readBytes);
    }

    /// <summary>
    /// Represents the event handler for the CacheIsRunningLow event.
    /// </summary>
    /// <param name="sender">The sender of the event.</param>
    /// <param name="e">Event arguments.</param>
    private void CacheIsRunningLow(object sender, EventArgs e)
    {
        this.cache.WarnIfRunningLow = false;

        new Task(() =>
        {
            Reload();
            this.cache.WarnIfRunningLow = true;
        }).Start();
    }
}

Cache class:

using System;
using System.Collections.Concurrent;

/// <summary>
/// Represents a generic cache.
/// </summary>
/// <typeparam name="T">Defines the type of the items in the cache.</typeparam>
public class Cache<T>
{
    private ConcurrentQueue<T> queue;

    /// <summary>
    /// Is executed when the number of items within the cache run below the
    /// specified warning limit and WarnIfRunningLow is set.
    /// </summary>
    public event EventHandler CacheIsRunningLow;

    /// <summary>
    /// Gets or sets a value indicating whether the CacheIsRunningLow event shall be fired or not.
    /// </summary>
    public bool WarnIfRunningLow
    {
        get;
        set;
    }

    /// <summary>
    /// Gets or sets a value that represents the lower warning limit.
    /// </summary>
    public int LowerWarningLimit
    {
        get;
        set;
    }

    /// <summary>
    /// Gets the number of items currently stored in the cache.
    /// </summary>
    public int Size
    {
        get;
        private set;
    }

    /// <summary>
    /// Initializes a new instance of the <see cref="Cache{T}"/> class.
    /// </summary>
    public Cache()
    {
        this.queue = new ConcurrentQueue<T>();
        this.Size = 0;
        this.LowerWarningLimit = 1024;
        this.WarnIfRunningLow = true;
    }

    /// <summary>
    /// Adds an item into the cache.
    /// </summary>
    /// <param name="item">The item to be added to the cache.</param>
    public void Add(T item)
    {
        this.queue.Enqueue(item);
        this.Size++;
    }

    /// <summary>
    /// Adds the items of the specified array to the end of the cache.
    /// </summary>
    /// <param name="items">The items to be added.</param>
    public void AddRange(T[] items)
    {
        this.AddRange(items, 0, items.Length);
    }

    /// <summary>
    /// Adds the specified count of items of the specified array starting
    /// from offset to the end of the cache.
    /// </summary>
    /// <param name="items">The array that contains the items.</param>
    /// <param name="offset">The offset that shall be used.</param>
    /// <param name="count">The number of items that shall be added.</param>
    public void AddRange(T[] items, int offset, int count)
    {
        for (int i = offset; i < count; i++)
            this.Add(items[i]);
    }

    /// <summary>
    /// Reads one item from the cache.
    /// </summary>
    /// <returns>The item that has been read from the cache.</returns>
    /// <exception cref="System.InvalidOperationException">
    /// The cache is empty.
    /// </exception>
    public T Read()
    {
        T item;

        if (!this.queue.TryDequeue(out item))
            throw new InvalidOperationException("The cache is empty.");

        this.Size--;

        if (this.WarnIfRunningLow &&
            this.Size < this.LowerWarningLimit)
        {
            this.CacheIsRunningLow?.Invoke(this, EventArgs.Empty);
        }

        return item;
    }

    /// <summary>
    /// Peeks the next item from cache.
    /// </summary>
    /// <returns>The item that has been read from the cache (without deletion).</returns>
    /// <exception cref="System.InvalidOperationException">
    /// The cache is empty.
    /// </exception>
    public T Peek()
    {
        T item;

        if (!this.queue.TryPeek(out item))
            throw new InvalidOperationException("The cache is empty.");

        return item;
    }
}

希望对您有所帮助,玩得开心 ;-)