summary refs log tree commit diff
path: root/crypto/src/util/io/BaseInputStream.cs
diff options
context:
space:
mode:
authorPeter Dettman <peter.dettman@bouncycastle.org>2022-05-23 22:44:57 +0700
committerPeter Dettman <peter.dettman@bouncycastle.org>2022-05-23 22:44:57 +0700
commit98b3ce6512812cd1d91c60ca0e69d69c32838d8e (patch)
tree8af69c097b389fe494e97401f7808590314a5b6b /crypto/src/util/io/BaseInputStream.cs
parentAdd ValidateBufferArguments (diff)
downloadBouncyCastle.NET-ed25519-98b3ce6512812cd1d91c60ca0e69d69c32838d8e.tar.xz
Work on Stream classes
- consistent naming, ordering
- CanRead/Seek/Write fixes
- argument validation
- some cases of blocking zero-byte Read calls
Diffstat (limited to 'crypto/src/util/io/BaseInputStream.cs')
-rw-r--r--crypto/src/util/io/BaseInputStream.cs43
1 files changed, 14 insertions, 29 deletions
diff --git a/crypto/src/util/io/BaseInputStream.cs b/crypto/src/util/io/BaseInputStream.cs
index a5613d801..ebe256632 100644
--- a/crypto/src/util/io/BaseInputStream.cs
+++ b/crypto/src/util/io/BaseInputStream.cs
@@ -1,34 +1,15 @@
 using System;
-using System.Diagnostics;
 using System.IO;
 
 namespace Org.BouncyCastle.Utilities.IO
 {
-    public abstract class BaseInputStream : Stream
+    public abstract class BaseInputStream
+        : Stream
     {
-		private bool closed;
-
-		public sealed override bool CanRead { get { return !closed; } }
+        public sealed override bool CanRead { get { return true; } }
         public sealed override bool CanSeek { get { return false; } }
         public sealed override bool CanWrite { get { return false; } }
 
-#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 void Flush() {}
         public sealed override long Length { get { throw new NotSupportedException(); } }
         public sealed override long Position
@@ -39,22 +20,26 @@ namespace Org.BouncyCastle.Utilities.IO
 
         public override int Read(byte[] buffer, int offset, int count)
         {
-            int pos = offset;
+            Streams.ValidateBufferArguments(buffer, offset, count);
+
+            int pos = 0;
             try
             {
-                int end = offset + count;
-                while (pos < end)
+                while (pos < count)
                 {
                     int b = ReadByte();
-                    if (b == -1) break;
-                    buffer[pos++] = (byte) b;
+                    if (b < 0)
+                        break;
+
+                    buffer[offset + pos++] = (byte)b;
                 }
             }
             catch (IOException)
             {
-                if (pos == offset) throw;
+                if (pos == 0)
+                    throw;
             }
-            return pos - offset;
+            return pos;
         }
 
         public sealed override long Seek(long offset, SeekOrigin origin) { throw new NotSupportedException(); }