From 6e2fa012c2cebb9aea22d9d3aeb75701ac573020 Mon Sep 17 00:00:00 2001
From: Alex Barney <thealexbarney@gmail.com>
Date: Tue, 22 Jan 2019 20:57:19 -0600
Subject: [PATCH] Make IStorage array read/write extension methods

---
 src/LibHac/IO/StorageExtensions.cs | 21 ++++++++++++++++++++-
 1 file changed, 20 insertions(+), 1 deletion(-)

diff --git a/src/LibHac/IO/StorageExtensions.cs b/src/LibHac/IO/StorageExtensions.cs
index 07ac6fbe..4e68377e 100644
--- a/src/LibHac/IO/StorageExtensions.cs
+++ b/src/LibHac/IO/StorageExtensions.cs
@@ -6,6 +6,25 @@ namespace LibHac.IO
 {
     public static class StorageExtensions
     {
+        public static void Read(this IStorage storage, byte[] buffer, long offset, int count, int bufferOffset)
+        {
+            ValidateStorageParameters(buffer, offset, count, bufferOffset);
+            storage.Read(buffer.AsSpan(bufferOffset, count), offset);
+        }
+        public static void Write(this IStorage storage, byte[] buffer, long offset, int count, int bufferOffset)
+        {
+            ValidateStorageParameters(buffer, offset, count, bufferOffset);
+            storage.Write(buffer.AsSpan(bufferOffset, count), offset);
+        }
+
+        private static void ValidateStorageParameters(byte[] buffer, long offset, int count, int bufferOffset)
+        {
+            if (buffer == null) throw new ArgumentNullException(nameof(buffer));
+            if (offset < 0) throw new ArgumentOutOfRangeException(nameof(offset), "Argument must be non-negative.");
+            if (count < 0) throw new ArgumentOutOfRangeException(nameof(count), "Argument must be non-negative.");
+            if (bufferOffset < 0) throw new ArgumentOutOfRangeException(nameof(bufferOffset), "Argument must be non-negative.");
+        }
+
         public static IStorage Slice(this IStorage storage, long start)
         {
             if (storage.Length == -1)
@@ -93,7 +112,7 @@ namespace LibHac.IO
 
             while (remaining > 0)
             {
-                int toWrite = (int) Math.Min(buffer.Length, remaining);
+                int toWrite = (int)Math.Min(buffer.Length, remaining);
                 input.Read(buffer.AsSpan(0, toWrite), inOffset);
 
                 output.Write(buffer, 0, toWrite);