using System; using System.IO; namespace Nikse.SubtitleEdit.Core { /// /// Exposes a around a file, supporting synchronous read operations. /// internal class FastFileStream : FileStream { private readonly long _length; private long _position; /// /// Initializes a new instance of the class with the specified path. /// /// A relative or absolute path for the file that the current object will encapsulate. public FastFileStream(string path) : base(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite) { _length = base.Length; } /// /// Gets the length in bytes of the stream. /// public override long Length => _length; /// /// Gets or sets the current position of the stream. /// public override long Position { get => _position; set => Seek(value, SeekOrigin.Begin); } /// /// Sets the current position of this stream to the given value. /// /// The point relative to from which to begin seeking. /// Specifies the beginning, the end, or the current position as a reference point for origin, using a value of type . /// The new position in the stream. public override long Seek(long offset, SeekOrigin origin) { switch (origin) { case SeekOrigin.Begin: _position = offset; base.Seek(offset, SeekOrigin.Begin); break; case SeekOrigin.Current: _position += offset; base.Seek(_position, SeekOrigin.Begin); break; default: throw new NotImplementedException(); } return _position; } /// /// Reads a block of bytes from the stream and writes the data in a given buffer. /// /// When this method returns, contains the specified byte array with the values between and ( + - 1) replaced by the bytes read from the current source. /// The byte offset in at which the read bytes will be placed. /// The maximum number of bytes to read. /// 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. public override int Read(byte[] array, int offset, int count) { var bytesRead = base.Read(array, offset, count); _position += bytesRead; return bytesRead; } /// /// Reads a byte from the file and advances the read position one byte. /// /// The byte, cast to an Int32, or -1 if the end of the stream has been reached. public override int ReadByte() { _position++; return base.ReadByte(); } } }