266 lines
13 KiB
C#
266 lines
13 KiB
C#
/*
|
|
* VirtualFS - Virtual File System library.
|
|
* Copyright (c) 2013-2026, Grzegorz Russek (grzegorz.russek@gmail.com)
|
|
* All rights reserved.
|
|
*
|
|
* Redistribution and use in source and binary forms, with or without
|
|
* modification, are permitted provided that the following conditions are met:
|
|
*
|
|
* Redistributions of source code must retain the above copyright notice,
|
|
* this list of conditions and the following disclaimer.
|
|
*
|
|
* Redistributions in binary form must reproduce the above copyright notice,
|
|
* this list of conditions and the following disclaimer in the documentation
|
|
* and/or other materials provided with the distribution.
|
|
*
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
|
|
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
|
|
* THE POSSIBILITY OF SUCH DAMAGE.
|
|
*/
|
|
|
|
using System;
|
|
using System.IO;
|
|
using VirtualFS.Base;
|
|
|
|
namespace VirtualFS.Implementation
|
|
{
|
|
/// <summary>Provides a generic view of a sequence
|
|
/// of bytes in virtual file system.</summary>
|
|
public class VirtualStream : Stream
|
|
{
|
|
/// <summary>Gets the internal stream.</summary>
|
|
/// <remarks>Do not close internal stream by yourself.</remarks>
|
|
public Stream InternalStream { get; private set; }
|
|
|
|
/// <summary>Gets the path of file.</summary>
|
|
public Path Path { get; private set; }
|
|
|
|
/// <summary>Gets the file system.</summary>
|
|
public IFileSystem FileSystem { get; private set; }
|
|
|
|
/// <summary>Gets or sets some unspecified kind of data.</summary>
|
|
internal object Unspecified { get; set; }
|
|
|
|
/// <summary>Occurs when stream is closed.</summary>
|
|
public event EventHandler OnClose;
|
|
|
|
/// <summary>Initializes a new instance of the <see cref="VirtualStream"/> class.</summary>
|
|
/// <param name="fileSystem">The file system.</param>
|
|
/// <param name="stream">The internal stream.</param>
|
|
/// <param name="path">The path of file.</param>
|
|
internal VirtualStream(BaseFileSystem fileSystem, Stream stream, Path path)
|
|
{
|
|
fileSystem.IncreaseOpened();
|
|
InternalStream = stream;
|
|
Path = path;
|
|
FileSystem = fileSystem;
|
|
}
|
|
|
|
/// <summary>Closes the current stream and releases any resources
|
|
/// (such as sockets and file handles) associated with the current
|
|
/// stream.</summary>
|
|
/// <remarks>This wrapped method will decrease number of opened
|
|
/// files in file system.</remarks>
|
|
public override void Close()
|
|
{
|
|
((BaseFileSystem)FileSystem).DecreaseOpened();
|
|
InternalStream.Close();
|
|
|
|
if (OnClose != null)
|
|
OnClose(this, EventArgs.Empty);
|
|
}
|
|
|
|
#region Wrapped Properties
|
|
|
|
/// <summary>
|
|
/// When overridden in a derived class, gets a value indicating whether the current stream supports reading.
|
|
/// </summary>
|
|
/// <returns>true if the stream supports reading; otherwise, false.</returns>
|
|
public override bool CanRead { get { return InternalStream.CanRead; } }
|
|
|
|
/// <summary>
|
|
/// When overridden in a derived class, gets a value indicating whether the current stream supports seeking.
|
|
/// </summary>
|
|
/// <returns>true if the stream supports seeking; otherwise, false.</returns>
|
|
public override bool CanSeek { get { return InternalStream.CanSeek; } }
|
|
|
|
/// <summary>
|
|
/// Gets a value that determines whether the current stream can time out.
|
|
/// </summary>
|
|
/// <returns>A value that determines whether the current stream can time out.</returns>
|
|
public override bool CanTimeout { get { return InternalStream.CanTimeout; } }
|
|
|
|
/// <summary>
|
|
/// When overridden in a derived class, gets a value indicating whether the current stream supports writing.
|
|
/// </summary>
|
|
/// <returns>true if the stream supports writing; otherwise, false.</returns>
|
|
public override bool CanWrite { get { return InternalStream.CanWrite; } }
|
|
|
|
/// <summary>
|
|
/// When overridden in a derived class, gets the length in bytes of the stream.
|
|
/// </summary>
|
|
/// <returns>A long value representing the length of the stream in bytes.</returns>
|
|
public override long Length { get { return InternalStream.Length; } }
|
|
|
|
/// <summary>
|
|
/// When overridden in a derived class, gets or sets the position within the current stream.
|
|
/// </summary>
|
|
/// <returns>The current position within the stream.</returns>
|
|
public override long Position { get { return InternalStream.Position; } set { InternalStream.Position = value; } }
|
|
|
|
/// <summary>
|
|
/// Gets or sets a value, in milliseconds, that determines how long the stream will attempt to read before timing out.
|
|
/// </summary>
|
|
/// <returns>A value, in milliseconds, that determines how long the stream will attempt to read before timing out.</returns>
|
|
public override int ReadTimeout { get { return InternalStream.ReadTimeout; } set { InternalStream.ReadTimeout = value; } }
|
|
|
|
/// <summary>
|
|
/// Gets or sets a value, in milliseconds, that determines how long the stream will attempt to write before timing out.
|
|
/// </summary>
|
|
/// <returns>A value, in milliseconds, that determines how long the stream will attempt to write before timing out.</returns>
|
|
public override int WriteTimeout { get { return InternalStream.WriteTimeout; } set { InternalStream.WriteTimeout = value; } }
|
|
|
|
#endregion Wrapped Properties
|
|
|
|
#region Wrapped Methods
|
|
|
|
/// <summary>
|
|
/// Begins an asynchronous read operation.
|
|
/// </summary>
|
|
/// <param name="buffer">The buffer to read the data into.</param>
|
|
/// <param name="offset">The byte offset in <paramref name="buffer" /> at which to begin writing data read from the stream.</param>
|
|
/// <param name="count">The maximum number of bytes to read.</param>
|
|
/// <param name="callback">An optional asynchronous callback, to be called when the read is complete.</param>
|
|
/// <param name="state">A user-provided object that distinguishes this particular asynchronous read request from other requests.</param>
|
|
/// <returns>
|
|
/// An <see cref="T:System.IAsyncResult" /> that represents the asynchronous read, which could still be pending.
|
|
/// </returns>
|
|
public override System.IAsyncResult BeginRead(byte[] buffer, int offset, int count, System.AsyncCallback callback, object state)
|
|
{
|
|
return InternalStream.BeginRead(buffer, offset, count, callback, state);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Begins an asynchronous write operation.
|
|
/// </summary>
|
|
/// <param name="buffer">The buffer to write data from.</param>
|
|
/// <param name="offset">The byte offset in <paramref name="buffer" /> from which to begin writing.</param>
|
|
/// <param name="count">The maximum number of bytes to write.</param>
|
|
/// <param name="callback">An optional asynchronous callback, to be called when the write is complete.</param>
|
|
/// <param name="state">A user-provided object that distinguishes this particular asynchronous write request from other requests.</param>
|
|
/// <returns>
|
|
/// An IAsyncResult that represents the asynchronous write, which could still be pending.
|
|
/// </returns>
|
|
public override System.IAsyncResult BeginWrite(byte[] buffer, int offset, int count, System.AsyncCallback callback, object state)
|
|
{
|
|
return InternalStream.BeginWrite(buffer, offset, count, callback, state);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Waits for the pending asynchronous read to complete.
|
|
/// </summary>
|
|
/// <param name="asyncResult">The reference to the pending asynchronous request to finish.</param>
|
|
/// <returns>
|
|
/// The number of bytes read from the stream, between zero (0) and the number of bytes you requested. Streams return zero (0) only at the end of the stream, otherwise, they should block until at least one byte is available.
|
|
/// </returns>
|
|
public override int EndRead(System.IAsyncResult asyncResult)
|
|
{
|
|
return InternalStream.EndRead(asyncResult);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Ends an asynchronous write operation.
|
|
/// </summary>
|
|
/// <param name="asyncResult">A reference to the outstanding asynchronous I/O request.</param>
|
|
public override void EndWrite(System.IAsyncResult asyncResult)
|
|
{
|
|
InternalStream.EndWrite(asyncResult);
|
|
}
|
|
|
|
/// <summary>
|
|
/// When overridden in a derived class, clears all buffers for this
|
|
/// stream and causes any buffered data to be written to the underlying device.
|
|
/// </summary>
|
|
public override void Flush()
|
|
{
|
|
InternalStream.Flush();
|
|
}
|
|
|
|
/// <summary>
|
|
/// 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.
|
|
/// </summary>
|
|
/// <param name="buffer">An array of bytes. When this method returns, the buffer contains the specified byte array with the values between <paramref name="offset" /> and (<paramref name="offset" /> + <paramref name="count" /> - 1) replaced by the bytes read from the current source.</param>
|
|
/// <param name="offset">The zero-based byte offset in <paramref name="buffer" /> at which to begin storing the data read from the current stream.</param>
|
|
/// <param name="count">The maximum number of bytes to be read from the current stream.</param>
|
|
/// <returns>
|
|
/// The total number of bytes read into the buffer. This can be less than the number of bytes requested if that many bytes are not currently available, or zero (0) if the end of the stream has been reached.
|
|
/// </returns>
|
|
public override int Read(byte[] buffer, int offset, int count)
|
|
{
|
|
return InternalStream.Read(buffer, offset, count);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Reads 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.
|
|
/// </summary>
|
|
/// <returns>
|
|
/// The unsigned byte cast to an Int32, or -1 if at the end of the stream.
|
|
/// </returns>
|
|
public override int ReadByte()
|
|
{
|
|
return InternalStream.ReadByte();
|
|
}
|
|
|
|
/// <summary>
|
|
/// When overridden in a derived class, sets the position within the current stream.
|
|
/// </summary>
|
|
/// <param name="offset">A byte offset relative to the <paramref name="origin" /> parameter.</param>
|
|
/// <param name="origin">A value of type <see cref="T:System.IO.SeekOrigin" /> indicating the reference point used to obtain the new position.</param>
|
|
/// <returns>
|
|
/// The new position within the current stream.
|
|
/// </returns>
|
|
public override long Seek(long offset, SeekOrigin origin)
|
|
{
|
|
return InternalStream.Seek(offset, origin);
|
|
}
|
|
|
|
/// <summary>
|
|
/// When overridden in a derived class, sets the length of the current stream.
|
|
/// </summary>
|
|
/// <param name="value">The desired length of the current stream in bytes.</param>
|
|
public override void SetLength(long value)
|
|
{
|
|
InternalStream.SetLength(value);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 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.
|
|
/// </summary>
|
|
/// <param name="buffer">An array of bytes. This method copies <paramref name="count" /> bytes from <paramref name="buffer" /> to the current stream.</param>
|
|
/// <param name="offset">The zero-based byte offset in <paramref name="buffer" /> at which to begin copying bytes to the current stream.</param>
|
|
/// <param name="count">The number of bytes to be written to the current stream.</param>
|
|
public override void Write(byte[] buffer, int offset, int count)
|
|
{
|
|
InternalStream.Write(buffer, offset, count);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Writes a byte to the current position in the stream and advances the position within the stream by one byte.
|
|
/// </summary>
|
|
/// <param name="value">The byte to write to the stream.</param>
|
|
public override void WriteByte(byte value)
|
|
{
|
|
base.WriteByte(value);
|
|
}
|
|
|
|
#endregion Wrapped Methods
|
|
}
|
|
} |