2025-02-21 13:20:52 +01:00
|
|
|
// Copyright (C) 2025, The Duplicati Team
|
|
|
|
|
// https://duplicati.com, hello@duplicati.com
|
|
|
|
|
//
|
|
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining a
|
|
|
|
|
// copy of this software and associated documentation files (the "Software"),
|
|
|
|
|
// to deal in the Software without restriction, including without limitation
|
|
|
|
|
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
|
|
|
|
// and/or sell copies of the Software, and to permit persons to whom the
|
|
|
|
|
// Software is furnished to do so, subject to the following conditions:
|
|
|
|
|
//
|
|
|
|
|
// The above copyright notice and this permission notice shall be included in
|
|
|
|
|
// all copies or substantial portions of the Software.
|
|
|
|
|
//
|
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
|
|
|
|
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
|
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
|
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
|
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
|
|
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
|
|
|
|
// DEALINGS IN THE SOFTWARE.
|
2024-02-28 15:45:30 +01:00
|
|
|
|
2009-02-10 23:29:01 +00:00
|
|
|
using System;
|
|
|
|
|
using System.IO;
|
2025-02-21 13:20:52 +01:00
|
|
|
using System.Threading.Tasks;
|
2009-02-10 23:29:01 +00:00
|
|
|
|
2010-11-21 11:08:15 +00:00
|
|
|
namespace Duplicati.Library.Utility
|
2009-02-10 23:29:01 +00:00
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// This class is a wrapper for a stream.
|
|
|
|
|
/// It's only purpose is to free other wrappers from implementing the boilerplate functions,
|
|
|
|
|
/// and allow the derived classes to override single functions.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public class OverrideableStream : Stream
|
|
|
|
|
{
|
2025-02-21 13:20:52 +01:00
|
|
|
/// <summary>
|
|
|
|
|
/// The base stream that is wrapped
|
|
|
|
|
/// </summary>
|
|
|
|
|
protected Stream m_basestream;
|
2009-02-10 23:29:01 +00:00
|
|
|
|
2025-02-21 13:20:52 +01:00
|
|
|
/// <summary>
|
|
|
|
|
/// Creates a new <see cref="OverrideableStream"/> instance
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="basestream">The stream to wrap</param>
|
|
|
|
|
/// <exception cref="ArgumentNullException"></exception>
|
2009-02-10 23:29:01 +00:00
|
|
|
public OverrideableStream(Stream basestream)
|
|
|
|
|
{
|
2025-02-21 13:20:52 +01:00
|
|
|
m_basestream = basestream ?? throw new ArgumentNullException(nameof(basestream));
|
2009-02-10 23:29:01 +00:00
|
|
|
}
|
|
|
|
|
|
2025-02-21 13:20:52 +01:00
|
|
|
/// <inheritdoc/>
|
|
|
|
|
public override bool CanRead => m_basestream.CanRead;
|
2009-02-10 23:29:01 +00:00
|
|
|
|
2025-02-21 13:20:52 +01:00
|
|
|
/// <inheritdoc/>
|
|
|
|
|
public override bool CanSeek => m_basestream.CanSeek;
|
2009-02-10 23:29:01 +00:00
|
|
|
|
2025-02-21 13:20:52 +01:00
|
|
|
/// <inheritdoc/>
|
|
|
|
|
public override bool CanWrite => m_basestream.CanWrite;
|
2009-02-10 23:29:01 +00:00
|
|
|
|
2025-02-21 13:20:52 +01:00
|
|
|
/// <inheritdoc/>
|
|
|
|
|
public override void Flush() => m_basestream.Flush();
|
2009-02-10 23:29:01 +00:00
|
|
|
|
2025-02-21 13:20:52 +01:00
|
|
|
/// <inheritdoc/>
|
|
|
|
|
public override long Length => m_basestream.Length;
|
2009-02-10 23:29:01 +00:00
|
|
|
|
2025-02-21 13:20:52 +01:00
|
|
|
/// <inheritdoc/>
|
2009-02-10 23:29:01 +00:00
|
|
|
public override long Position
|
|
|
|
|
{
|
2025-02-21 13:20:52 +01:00
|
|
|
get => m_basestream.Position;
|
|
|
|
|
set => m_basestream.Position = value;
|
2009-02-10 23:29:01 +00:00
|
|
|
}
|
|
|
|
|
|
2025-02-21 13:20:52 +01:00
|
|
|
/// <inheritdoc/>
|
2009-02-10 23:29:01 +00:00
|
|
|
public override int Read(byte[] buffer, int offset, int count)
|
2025-02-21 13:20:52 +01:00
|
|
|
=> m_basestream.Read(buffer, offset, count);
|
2009-02-10 23:29:01 +00:00
|
|
|
|
2025-02-21 13:20:52 +01:00
|
|
|
/// <inheritdoc/>
|
2009-02-10 23:29:01 +00:00
|
|
|
public override long Seek(long offset, System.IO.SeekOrigin origin)
|
2025-02-21 13:20:52 +01:00
|
|
|
=> m_basestream.Seek(offset, origin);
|
2009-02-10 23:29:01 +00:00
|
|
|
|
2025-02-21 13:20:52 +01:00
|
|
|
/// <inheritdoc/>
|
2009-02-10 23:29:01 +00:00
|
|
|
public override void SetLength(long value)
|
2025-02-21 13:20:52 +01:00
|
|
|
=> m_basestream.SetLength(value);
|
2009-02-10 23:29:01 +00:00
|
|
|
|
2025-02-21 13:20:52 +01:00
|
|
|
/// <inheritdoc/>
|
2009-02-10 23:29:01 +00:00
|
|
|
public override void Write(byte[] buffer, int offset, int count)
|
2025-02-21 13:20:52 +01:00
|
|
|
=> m_basestream.Write(buffer, offset, count);
|
|
|
|
|
|
|
|
|
|
/// <inheritdoc/>
|
|
|
|
|
public override Task<int> ReadAsync(byte[] buffer, int offset, int count, System.Threading.CancellationToken cancellationToken)
|
|
|
|
|
=> m_basestream.ReadAsync(buffer, offset, count, cancellationToken);
|
|
|
|
|
|
|
|
|
|
/// <inheritdoc/>
|
|
|
|
|
public override Task WriteAsync(byte[] buffer, int offset, int count, System.Threading.CancellationToken cancellationToken)
|
|
|
|
|
=> m_basestream.WriteAsync(buffer, offset, count, cancellationToken);
|
2009-02-10 23:29:01 +00:00
|
|
|
|
2025-02-21 13:20:52 +01:00
|
|
|
/// <inheritdoc/>
|
|
|
|
|
public override Task FlushAsync(System.Threading.CancellationToken cancellationToken)
|
|
|
|
|
=> m_basestream.FlushAsync(cancellationToken);
|
|
|
|
|
|
|
|
|
|
/// <inheritdoc/>
|
2009-02-10 23:29:01 +00:00
|
|
|
protected override void Dispose(bool disposing)
|
|
|
|
|
{
|
2025-02-21 13:20:52 +01:00
|
|
|
m_basestream?.Dispose();
|
2009-02-10 23:29:01 +00:00
|
|
|
m_basestream = null;
|
|
|
|
|
base.Dispose(disposing);
|
|
|
|
|
}
|
2025-02-21 13:20:52 +01:00
|
|
|
|
2009-02-10 23:29:01 +00:00
|
|
|
}
|
|
|
|
|
}
|