using System; using System.IO; using System.Threading; namespace Unity.SharpZipLib.Tests.TestSupport { /// /// An extended memory stream /// that tracks closing and disposing /// internal class TrackedMemoryStream : MemoryStream { /// /// Initializes a new instance of the class. /// public TrackedMemoryStream() { } /// /// Initializes a new instance of the class. /// /// The buffer. public TrackedMemoryStream(byte[] buffer) : base(buffer) { } /// /// Write a short value in Little Endian order /// /// public void WriteLEShort(short value) { WriteByte(unchecked((byte)value)); WriteByte(unchecked((byte)(value >> 8))); } /// /// Write an int value in little endian order. /// /// public void WriteLEInt(int value) { WriteLEShort(unchecked((short)value)); WriteLEShort(unchecked((short)(value >> 16))); } /// /// Releases the unmanaged resources used by the class and optionally releases the managed resources. /// /// true to release both managed and unmanaged resources; false to release only unmanaged resources. protected override void Dispose(bool disposing) { isDisposed_ = true; base.Dispose(disposing); } /// /// Closes the current stream and releases any resources (such as sockets and file handles) associated with the current stream. /// public override void Close() { if (isClosed_) { throw new InvalidOperationException("Already closed"); } isClosed_ = true; base.Close(); } /// /// Gets a value indicating whether this instance is closed. /// /// true if this instance is closed; otherwise, false. public bool IsClosed { get { return isClosed_; } } /// /// Gets a value indicating whether this instance is disposed. /// /// /// true if this instance is disposed; otherwise, false. /// public bool IsDisposed { get { return isDisposed_; } } #region Instance Fields private bool isDisposed_; private bool isClosed_; #endregion Instance Fields } /// /// An extended file stream /// that tracks closing and disposing /// internal class TrackedFileStream : FileStream { /// /// Initializes a new instance of the class. /// /// The buffer. public TrackedFileStream(string path, FileMode mode = FileMode.Open, FileAccess access = FileAccess.Read) : base(path, mode, access) { } /// /// Releases the unmanaged resources used by the class and optionally releases the managed resources. /// /// true to release both managed and unmanaged resources; false to release only unmanaged resources. protected override void Dispose(bool disposing) { isDisposed_ = true; base.Dispose(disposing); } /// /// Closes the current stream and releases any resources (such as sockets and file handles) associated with the current stream. /// public override void Close() { if (isClosed_) { throw new InvalidOperationException("Already closed"); } isClosed_ = true; base.Close(); } /// /// Gets a value indicating whether this instance is closed. /// /// true if this instance is closed; otherwise, false. public bool IsClosed { get { return isClosed_; } } /// /// Gets a value indicating whether this instance is disposed. /// /// /// true if this instance is disposed; otherwise, false. /// public bool IsDisposed { get { return isDisposed_; } } #region Instance Fields private bool isDisposed_; private bool isClosed_; #endregion Instance Fields } /// /// A that cannot seek. /// internal class MemoryStreamWithoutSeek : TrackedMemoryStream { /// /// Gets a value indicating whether the current stream supports seeking. /// /// /// true if the stream is open. public override bool CanSeek { get { return false; } } } /// /// A that cannot be read but supports infinite writes. /// internal class NullStream : Stream { /// /// When overridden in a derived class, gets a value indicating whether the current stream supports reading. /// /// /// true if the stream supports reading; otherwise, false. public override bool CanRead { get { return false; } } /// /// When overridden in a derived class, gets a value indicating whether the current stream supports seeking. /// /// /// true if the stream supports seeking; otherwise, false. public override bool CanSeek { get { return false; } } /// /// When overridden in a derived class, gets a value indicating whether the current stream supports writing. /// /// /// true if the stream supports writing; otherwise, false. public override bool CanWrite { get { return true; } } /// /// When overridden in a derived class, clears all buffers for this stream and causes any buffered data to be written to the underlying device. /// /// An I/O error occurs. public override void Flush() { // Do nothing. } /// /// When overridden in a derived class, gets the length in bytes of the stream. /// /// /// A long value representing the length of the stream in bytes. /// A class derived from Stream does not support seeking. /// Methods were called after the stream was closed. public override long Length { get { throw new Exception("The method or operation is not implemented."); } } /// /// When overridden in a derived class, gets or sets the position within the current stream. /// /// /// The current position within the stream. /// An I/O error occurs. /// The stream does not support seeking. /// Methods were called after the stream was closed. public override long Position { get { throw new Exception("The method or operation is not implemented."); } set { throw new Exception("The method or operation is not implemented."); } } /// /// 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. /// /// An array of bytes. When this method returns, the buffer contains the specified byte array with the values between and ( + - 1) replaced by the bytes read from the current source. /// The zero-based byte offset in at which to begin storing the data read from the current stream. /// The maximum number of bytes to be read from the current stream. /// /// 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. /// /// The sum of and is larger than the buffer length. /// /// is null. /// /// or is negative. /// An I/O error occurs. /// The stream does not support reading. /// Methods were called after the stream was closed. public override int Read(byte[] buffer, int offset, int count) { throw new Exception("The method or operation is not implemented."); } /// /// When overridden in a derived class, sets the position within the current stream. /// /// A byte offset relative to the parameter. /// A value of type indicating the reference point used to obtain the new position. /// /// The new position within the current stream. /// /// An I/O error occurs. /// The stream does not support seeking, such as if the stream is constructed from a pipe or console output. /// Methods were called after the stream was closed. public override long Seek(long offset, SeekOrigin origin) { throw new Exception("The method or operation is not implemented."); } /// /// When overridden in a derived class, sets the length of the current stream. /// /// The desired length of the current stream in bytes. /// An I/O error occurs. /// The stream does not support both writing and seeking, such as if the stream is constructed from a pipe or console output. /// Methods were called after the stream was closed. public override void SetLength(long value) { throw new Exception("The method or operation is not implemented."); } /// /// 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. /// /// An array of bytes. This method copies bytes from to the current stream. /// The zero-based byte offset in at which to begin copying bytes to the current stream. /// The number of bytes to be written to the current stream. /// The sum of and is greater than the buffer length. /// /// is null. /// /// or is negative. /// An I/O error occurs. /// The stream does not support writing. /// Methods were called after the stream was closed. public override void Write(byte[] buffer, int offset, int count) { // Do nothing. } } /// /// A that supports reading and writing from a fixed size memory buffer. /// This provides the ability to test writing and reading from very large streams /// without using any disk storeage /// internal class WindowedStream : Stream { /// /// Initializes a new instance of the class. /// /// The size. public WindowedStream(int size, CancellationToken? token = null) { ringBuffer = new ReadWriteRingBuffer(size, token); } /// /// When overridden in a derived class, gets a value indicating whether the current stream supports reading. /// /// /// true if the stream is not closed. /// If the stream is closed, this property returns false. public override bool CanRead => !ringBuffer.IsClosed; /// /// Gets a value indicating whether the current stream supports seeking. /// /// /// false public override bool CanSeek => false; /// /// When overridden in a derived class, gets a value indicating whether the current stream supports writing. /// /// /// true if the stream is not closed. /// If the stream is closed, this property returns false. public override bool CanWrite => !ringBuffer.IsClosed; /// /// When overridden in a derived class, clears all buffers for this stream and causes any buffered data to be written to the underlying device. /// /// An I/O error occurs. public override void Flush() { // Do nothing } /// /// When overridden in a derived class, gets the length in bytes of the stream. /// /// /// A long value representing the length of the stream in bytes. /// A class derived from Stream does not support seeking. /// Methods were called after the stream was closed. public override long Length { get => throw new NotSupportedException(); } /// /// When overridden in a derived class, gets or sets the position within the current stream. /// /// /// The current position within the stream. /// An I/O error occurs. /// The stream does not support seeking. /// Methods were called after the stream was closed. public override long Position { get => throw new NotSupportedException(); set => throw new NotSupportedException(); } /// /// 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. /// /// An array of bytes. When this method returns, the buffer contains the specified byte array with the values between and ( + - 1) replaced by the bytes read from the current source. /// The zero-based byte offset in at which to begin storing the data read from the current stream. /// The maximum number of bytes to be read from the current stream. /// /// 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. /// /// The sum of and is larger than the buffer length. /// /// is null. /// /// or is negative. /// An I/O error occurs. /// The stream does not support reading. /// Methods were called after the stream was closed. public override int Read(byte[] buffer, int offset, int count) { int bytesRead = 0; while (count > 0) { int value = ringBuffer.ReadByte(); if (value >= 0) { buffer[offset] = (byte)(value & 0xff); offset++; bytesRead++; count--; } else { break; } } return bytesRead; } /// /// Not supported, throws . /// /// A byte offset relative to the parameter. /// A value of type indicating the reference point used to obtain the new position. /// /// The stream does not support seeking, such as if the stream is constructed from a pipe or console output. public override long Seek(long offset, SeekOrigin origin) => throw new NotSupportedException(); /// /// Not supported, throws . /// /// The desired length of the current stream in bytes. /// The stream does not support both writing and seeking, such as if the stream is constructed from a pipe or console output. public override void SetLength(long value) => throw new NotSupportedException(); /// /// Writes a sequence of bytes to the current stream and advances the current position within this stream by the number of bytes written. /// /// An array of bytes. This method copies bytes from to the current stream. /// The zero-based byte offset in at which to begin copying bytes to the current stream. /// The number of bytes to be written to the current stream. /// The sum of and is greater than the buffer length. /// /// is null. /// /// or is negative. /// An I/O error occurs. /// The stream does not support writing. /// Methods were called after the stream was closed. public override void Write(byte[] buffer, int offset, int count) { for (int i = 0; i < count; ++i) { ringBuffer.WriteByte(buffer[offset + i]); } } /// /// Gets a value indicating whether this instance is closed. /// /// true if this instance is closed; otherwise, false. public bool IsClosed { get { return ringBuffer.IsClosed; } } /// Releases the unmanaged resources used by the and optionally releases the managed resources. /// true to release both managed and unmanaged resources; false to release only unmanaged resources. protected override void Dispose(bool disposing) { if (disposing && !ringBuffer.IsClosed) { ringBuffer.Close(); } base.Dispose(disposing); } /// /// Gets the bytes written. /// /// The bytes written. public long BytesWritten => ringBuffer.BytesWritten; /// /// Gets the bytes read. /// /// The bytes read. public long BytesRead => ringBuffer.BytesRead; #region Instance Fields private readonly ReadWriteRingBuffer ringBuffer; #endregion Instance Fields } internal class SingleByteReadingStream : MemoryStream { /// /// Initializes a new instance of the class. /// public SingleByteReadingStream() { } public override int Read(byte[] buffer, int offset, int count) { if (count > 0) count = 1; return base.Read(buffer, offset, count); } } /// /// A stream that closes itself when all of its data is read. /// /// /// Useful for testing issues such as https://github.com/icsharpcode/SharpZipLib/issues/379 /// internal class SelfClosingStream : MemoryStream { private bool isFullyRead = false; /// /// Initializes a new instance of the class. /// public SelfClosingStream() { } // public override int Read(byte[] buffer, int offset, int count) { var read = base.Read(buffer, offset, count); if (read == 0) { isFullyRead = true; Close(); } return read; } /// /// CanRead is false if we're closed, or base.CanRead otherwise. /// public override bool CanRead { get { if (isFullyRead) { return false; } return base.CanRead; } } } }