mirror of
https://github.com/Thealexbarney/LibHac.git
synced 2024-11-14 10:49:41 +01:00
Nothing to see here officer
This commit is contained in:
parent
c1e60f8c75
commit
9b476fa8ce
1 changed files with 73 additions and 0 deletions
73
src/LibHac/IO/StorageStream.cs
Normal file
73
src/LibHac/IO/StorageStream.cs
Normal file
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue