Add FileBase

This commit is contained in:
Alex Barney 2018-12-28 21:10:50 -07:00
parent b606724fbc
commit 23088aab02
4 changed files with 36 additions and 10 deletions

23
src/LibHac/IO/FileBase.cs Normal file
View file

@ -0,0 +1,23 @@
using System;
namespace LibHac.IO
{
public abstract class FileBase : IFile
{
public abstract int Read(Span<byte> destination, long offset);
public abstract void Write(ReadOnlySpan<byte> source, long offset);
public abstract void Flush();
public abstract long GetSize();
public abstract long SetSize();
protected int GetAvailableSizeAndValidate(ReadOnlySpan<byte> span, long offset)
{
long fileLength = GetSize();
if (span == null) throw new ArgumentNullException(nameof(span));
if (offset < 0) throw new ArgumentOutOfRangeException(nameof(offset), "Argument must be non-negative.");
return (int)Math.Min(fileLength - offset, span.Length);
}
}
}

View file

@ -4,7 +4,7 @@ namespace LibHac.IO
{
public interface IFile
{
void Read(Span<byte> destination, long offset);
int Read(Span<byte> destination, long offset);
void Write(ReadOnlySpan<byte> source, long offset);
void Flush();
long GetSize();

View file

@ -2,7 +2,7 @@
namespace LibHac.IO
{
public class RomFsFile : IFile
public class RomFsFile : FileBase
{
private IStorage BaseStorage { get; }
private long Offset { get; }
@ -15,29 +15,32 @@ namespace LibHac.IO
Size = size;
}
public void Read(Span<byte> destination, long offset)
public override int Read(Span<byte> destination, long offset)
{
long storageOffset = Offset + offset;
int toRead = GetAvailableSizeAndValidate(destination, offset);
BaseStorage.Read(destination, storageOffset);
BaseStorage.Read(destination.Slice(0, toRead), storageOffset);
return toRead;
}
public void Write(ReadOnlySpan<byte> source, long offset)
public override void Write(ReadOnlySpan<byte> source, long offset)
{
throw new NotImplementedException();
}
public void Flush()
public override void Flush()
{
throw new NotImplementedException();
}
public long GetSize()
public override long GetSize()
{
return Size;
}
public long SetSize()
public override long SetSize()
{
throw new NotSupportedException();
}

View file

@ -95,7 +95,7 @@ namespace LibHac.IO
if (Length != -1)
{
if (offset + count > Length) throw new ArgumentException();
if (offset + count > Length) throw new ArgumentException("The given offset and count exceed the length of the Storage");
}
}
@ -107,7 +107,7 @@ namespace LibHac.IO
if (Length != -1)
{
if (offset + destination.Length > Length) throw new ArgumentException("Storage");
if (offset + destination.Length > Length) throw new ArgumentException("The given offset and count exceed the length of the Storage");
}
}
}