summary refs log tree commit diff
path: root/crypto/src/util/io/Streams.cs
diff options
context:
space:
mode:
authorPeter Dettman <peter.dettman@bouncycastle.org>2022-05-19 20:20:04 +0700
committerPeter Dettman <peter.dettman@bouncycastle.org>2022-05-19 20:20:04 +0700
commitf89b319a11832ccf4db3d11faccba09bf5239ba8 (patch)
treeb035b12eacaa3b42c2118232dea0faee572c6941 /crypto/src/util/io/Streams.cs
parentAdd previewing of pending output records (diff)
downloadBouncyCastle.NET-ed25519-f89b319a11832ccf4db3d11faccba09bf5239ba8.tar.xz
Add ValidateBufferArguments
Diffstat (limited to '')
-rw-r--r--crypto/src/util/io/Streams.cs80
1 files changed, 46 insertions, 34 deletions
diff --git a/crypto/src/util/io/Streams.cs b/crypto/src/util/io/Streams.cs
index a86367e56..3623dfe39 100644
--- a/crypto/src/util/io/Streams.cs
+++ b/crypto/src/util/io/Streams.cs
@@ -19,38 +19,6 @@ namespace Org.BouncyCastle.Utilities.IO
 			}
 		}
 
-		public static byte[] ReadAll(Stream inStr)
-		{
-			MemoryStream buf = new MemoryStream();
-			PipeAll(inStr, buf);
-			return buf.ToArray();
-		}
-
-		public static byte[] ReadAllLimited(Stream inStr, int limit)
-		{
-			MemoryStream buf = new MemoryStream();
-			PipeAllLimited(inStr, limit, buf);
-			return buf.ToArray();
-		}
-
-		public static int ReadFully(Stream inStr, byte[] buf)
-		{
-			return ReadFully(inStr, buf, 0, buf.Length);
-		}
-
-		public static int ReadFully(Stream inStr, byte[] buf, int off, int len)
-		{
-			int totalRead = 0;
-			while (totalRead < len)
-			{
-				int numRead = inStr.Read(buf, off + totalRead, len - totalRead);
-				if (numRead < 1)
-					break;
-				totalRead += numRead;
-			}
-			return totalRead;
-		}
-
         /// <summary>Write the full contents of inStr to the destination stream outStr.</summary>
         /// <param name="inStr">Source stream.</param>
         /// <param name="outStr">Destination stream.</param>
@@ -105,8 +73,52 @@ namespace Org.BouncyCastle.Utilities.IO
 			return total;
 		}
 
-        /// <exception cref="IOException"></exception>
-        public static void WriteBufTo(MemoryStream buf, Stream output)
+		public static byte[] ReadAll(Stream inStr)
+		{
+			MemoryStream buf = new MemoryStream();
+			PipeAll(inStr, buf);
+			return buf.ToArray();
+		}
+
+		public static byte[] ReadAllLimited(Stream inStr, int limit)
+		{
+			MemoryStream buf = new MemoryStream();
+			PipeAllLimited(inStr, limit, buf);
+			return buf.ToArray();
+		}
+
+		public static int ReadFully(Stream inStr, byte[] buf)
+		{
+			return ReadFully(inStr, buf, 0, buf.Length);
+		}
+
+		public static int ReadFully(Stream inStr, byte[] buf, int off, int len)
+		{
+			int totalRead = 0;
+			while (totalRead < len)
+			{
+				int numRead = inStr.Read(buf, off + totalRead, len - totalRead);
+				if (numRead < 1)
+					break;
+				totalRead += numRead;
+			}
+			return totalRead;
+		}
+
+		public static void ValidateBufferArguments(byte[] buffer, int offset, int count)
+        {
+			if (buffer == null)
+				throw new ArgumentNullException("buffer");
+			int available = buffer.Length - offset;
+			if ((offset | available) < 0)
+				throw new ArgumentOutOfRangeException("offset");
+			int remaining = available - count;
+			if ((count | remaining) < 0)
+				throw new ArgumentOutOfRangeException("count");
+		}
+
+		/// <exception cref="IOException"></exception>
+		public static void WriteBufTo(MemoryStream buf, Stream output)
         {
             buf.WriteTo(output);
         }