From 98b3ce6512812cd1d91c60ca0e69d69c32838d8e Mon Sep 17 00:00:00 2001 From: Peter Dettman Date: Mon, 23 May 2022 22:44:57 +0700 Subject: Work on Stream classes - consistent naming, ordering - CanRead/Seek/Write fixes - argument validation - some cases of blocking zero-byte Read calls --- crypto/src/util/io/BaseOutputStream.cs | 53 +++++++++------------------------- 1 file changed, 14 insertions(+), 39 deletions(-) (limited to 'crypto/src/util/io/BaseOutputStream.cs') diff --git a/crypto/src/util/io/BaseOutputStream.cs b/crypto/src/util/io/BaseOutputStream.cs index 0dbe821de..d9a5b92d6 100644 --- a/crypto/src/util/io/BaseOutputStream.cs +++ b/crypto/src/util/io/BaseOutputStream.cs @@ -1,35 +1,16 @@ using System; -using System.Diagnostics; using System.IO; namespace Org.BouncyCastle.Utilities.IO { - public abstract class BaseOutputStream : Stream + public abstract class BaseOutputStream + : Stream { - private bool closed; - - public sealed override bool CanRead { get { return false; } } + public sealed override bool CanRead { get { return false; } } public sealed override bool CanSeek { get { return false; } } - public sealed override bool CanWrite { get { return !closed; } } - -#if PORTABLE - protected override void Dispose(bool disposing) - { - if (disposing) - { - closed = true; - } - base.Dispose(disposing); - } -#else - public override void Close() - { - closed = true; - base.Close(); - } -#endif + public sealed override bool CanWrite { get { return true; } } - public override void Flush() { } + public override void Flush() {} public sealed override long Length { get { throw new NotSupportedException(); } } public sealed override long Position { @@ -42,28 +23,22 @@ namespace Org.BouncyCastle.Utilities.IO public override void Write(byte[] buffer, int offset, int count) { - Debug.Assert(buffer != null); - Debug.Assert(0 <= offset && offset <= buffer.Length); - Debug.Assert(count >= 0); - - int end = offset + count; + Streams.ValidateBufferArguments(buffer, offset, count); - Debug.Assert(0 <= end && end <= buffer.Length); - - for (int i = offset; i < end; ++i) + for (int i = 0; i < count; ++i) { - this.WriteByte(buffer[i]); + WriteByte(buffer[offset + i]); } } - public virtual void Write(params byte[] buffer) - { - Write(buffer, 0, buffer.Length); - } + public virtual void Write(params byte[] buffer) + { + Write(buffer, 0, buffer.Length); + } - public override void WriteByte(byte b) + public override void WriteByte(byte value) { - Write(new byte[]{ b }, 0, 1); + Write(new byte[]{ value }, 0, 1); } } } -- cgit 1.4.1