#region License Information (GPL v3) /* ShareX - A program that allows you to take screenshots and share any file type Copyright (c) 2007-2023 ShareX Team This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. Optionally you can also view the license at . */ #endregion License Information (GPL v3) using System; using System.IO; namespace ShareX.HelpersLib { internal sealed class MaxLengthStream : Stream { private readonly Stream stream; private long length = 0L; public MaxLengthStream(Stream stream, long maxLength) { this.stream = stream ?? throw new ArgumentNullException(nameof(stream)); MaxLength = maxLength; } public long MaxLength { get; } public override bool CanRead => stream.CanRead; public override bool CanSeek => false; public override bool CanWrite => false; public override long Length => stream.Length; public override long Position { get => stream.Position; set => throw new NotSupportedException(); } public override int Read(byte[] buffer, int offset, int count) { int result = stream.Read(buffer, offset, count); length += result; if (length > MaxLength) { throw new Exception("Stream is larger than the maximum allowed size."); } return result; } public override void Flush() => throw new NotSupportedException(); public override long Seek(long offset, SeekOrigin origin) => throw new NotSupportedException(); public override void SetLength(long value) => throw new NotSupportedException(); public override void Write(byte[] buffer, int offset, int count) => throw new NotSupportedException(); protected override void Dispose(bool disposing) { stream.Dispose(); base.Dispose(disposing); } } }