예제 #1
0
        /// <summary>
        /// A stream that buffers the underlying stream to contiguous memory, reading until the whole file is eventually memory-backed.
        /// </summary>
        /// <param name="stream">The underlying stream to read from.</param>
        /// <param name="blocksToReadAhead">The amount of blocks to read ahead of the read position.</param>
        /// <param name="shared">Another AsyncBufferStream which is backing the same underlying stream. Allows shared usage of memory-backing.</param>
        public AsyncBufferStream(Stream stream, int blocksToReadAhead, AsyncBufferStream shared = null)
        {
            Debug.Assert(stream != null);

            this.blocksToReadAhead = blocksToReadAhead;
            underlyingStream       = stream;

            if (underlyingStream.CanSeek)
            {
                underlyingStream.Seek(0, SeekOrigin.Begin);
            }

            if (shared?.Length != stream.Length)
            {
                data = new byte[underlyingStream.Length];
                blockLoadedStatus = new bool[data.Length / block_size + 1];
            }
            else
            {
                data = shared.data;
                blockLoadedStatus = shared.blockLoadedStatus;
                isLoaded          = shared.isLoaded;
            }


            loadThread = new Thread(loadRequiredBlocks)
            {
                IsBackground = true
            };
            loadThread.Start();
        }
        public AsyncBufferStream(Stream stream, int blocksToReadAhead, AsyncBufferStream shared = null)
        {
            underlyingStream       = stream ?? throw new ArgumentNullException(nameof(stream));
            this.blocksToReadAhead = blocksToReadAhead;

            if (underlyingStream.CanSeek)
            {
                underlyingStream.Seek(0, SeekOrigin.Begin);
            }

            if (shared?.Length != stream.Length)
            {
                data = new byte[underlyingStream.Length];
                blockLoadedStatus = new bool[data.Length / block_size + 1];
            }
            else
            {
                data = shared.data;
                blockLoadedStatus = shared.blockLoadedStatus;
                isLoaded          = shared.isLoaded;
            }

            cancellationToken = new CancellationTokenSource();
            Task.Factory.StartNew(loadRequiredBlocks, cancellationToken.Token, TaskCreationOptions.LongRunning, TaskScheduler.Default);
        }
예제 #3
0
        /// <summary>
        /// A stream that buffers the underlying stream to contiguous memory, reading until the whole file is eventually memory-backed.
        /// </summary>
        /// <param name="stream">The underlying stream to read from.</param>
        /// <param name="blocksToReadAhead">The amount of blocks to read ahead of the read position.</param>
        /// <param name="shared">Another AsyncBufferStream which is backing the same underlying stream. Allows shared usage of memory-backing.</param>
        public AsyncBufferStream(Stream stream, int blocksToReadAhead, AsyncBufferStream shared = null)
        {
            if (stream == null)
            {
                throw new ArgumentNullException(nameof(stream));
            }

            this.blocksToReadAhead = blocksToReadAhead;
            underlyingStream       = stream;

            if (underlyingStream.CanSeek)
            {
                underlyingStream.Seek(0, SeekOrigin.Begin);
            }

            if (shared?.Length != stream.Length)
            {
                data = new byte[underlyingStream.Length];
                blockLoadedStatus = new bool[data.Length / block_size + 1];
            }
            else
            {
                data = shared.data;
                blockLoadedStatus = shared.blockLoadedStatus;
                isLoaded          = shared.isLoaded;
            }


            loadThread = new Thread(loadRequiredBlocks)
            {
                Name         = @"AsyncBufferStream_Load",
                IsBackground = true
            };
            loadThread.Start();
        }