From 9b476fa8cee91288945f6872400071456bb000c7 Mon Sep 17 00:00:00 2001 From: shadowninja108 Date: Mon, 10 Dec 2018 20:55:08 -0700 Subject: [PATCH] Nothing to see here officer --- src/LibHac/IO/StorageStream.cs | 73 ++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 src/LibHac/IO/StorageStream.cs diff --git a/src/LibHac/IO/StorageStream.cs b/src/LibHac/IO/StorageStream.cs new file mode 100644 index 00000000..810d9c15 --- /dev/null +++ b/src/LibHac/IO/StorageStream.cs @@ -0,0 +1,73 @@ +using System; +using System.IO; + +namespace LibHac.IO +{ + public class StorageStream : Stream + { + private IStorage BaseStorage { get; } + private bool LeaveOpen { get; } + + public StorageStream(IStorage baseStorage, bool leaveOpen) + { + BaseStorage = baseStorage; + LeaveOpen = leaveOpen; + Length = baseStorage.Length; + } + + public override int Read(byte[] buffer, int offset, int count) + { + int toRead = (int)Math.Min(count, Length - Position); + BaseStorage.Read(buffer, Position, toRead, offset); + + Position += toRead; + return toRead; + } + + public override void Write(byte[] buffer, int offset, int count) + { + BaseStorage.Write(buffer, Position, count, offset); + Position += count; + } + + public override void Flush() + { + BaseStorage.Flush(); + } + + public override long Seek(long offset, SeekOrigin origin) + { + switch (origin) + { + case SeekOrigin.Begin: + Position = offset; + break; + case SeekOrigin.Current: + Position += offset; + break; + case SeekOrigin.End: + Position = Length - offset; + break; + } + + return Position; + } + + public override void SetLength(long value) + { + throw new NotImplementedException(); + } + + public override bool CanRead => (BaseStorage as Storage)?.CanRead ?? true; + public override bool CanSeek => true; + public override bool CanWrite => (BaseStorage as Storage)?.CanWrite ?? true; + public override long Length { get; } + public override long Position { get; set; } + + protected override void Dispose(bool disposing) + { + if (!LeaveOpen) BaseStorage?.Dispose(); + base.Dispose(disposing); + } + } +} \ No newline at end of file