1
0
Fork 0
mirror of https://github.com/beefytech/Beef.git synced 2025-06-16 23:34:10 +02:00

Add SeekUnderlying to FileStream

This commit is contained in:
disarray2077 2021-08-01 17:37:37 -03:00
parent a71962a5a8
commit 4bd3cc641d

View file

@ -253,6 +253,22 @@ namespace System.IO
}
}
public override bool CanRead
{
get
{
return mFileAccess.HasFlag(FileAccess.Read);
}
}
public override bool CanWrite
{
get
{
return mFileAccess.HasFlag(FileAccess.Write);
}
}
public this()
{
@ -274,22 +290,6 @@ namespace System.IO
mFileAccess = access;
}
public override bool CanRead
{
get
{
return mFileAccess.HasFlag(FileAccess.Read);
}
}
public override bool CanWrite
{
get
{
return mFileAccess.HasFlag(FileAccess.Write);
}
}
public Result<void, FileOpenError> Create(StringView path, FileAccess access = .ReadWrite, FileShare share = .None, int bufferSize = 4096, FileOptions options = .None, SecurityAttributes* secAttrs = null)
{
return Open(path, FileMode.Create, access, share, bufferSize, options, secAttrs);
@ -406,15 +406,20 @@ namespace System.IO
mUnderlyingLength = Platform.BfpFile_GetFileSize(mBfpFile);
}
protected Result<void> SeekUnderlying(int64 offset, Platform.BfpFileSeekKind seekKind = .Absolute)
{
int64 newPos = Platform.BfpFile_Seek(mBfpFile, offset, seekKind);
Result<void> result = ((seekKind == .Absolute) && (newPos != offset)) ? .Err : .Ok;
if (result case .Ok)
mBfpFilePos = newPos;
return result;
}
protected override Result<int> TryReadUnderlying(int64 pos, Span<uint8> data)
{
if (mBfpFilePos != pos)
{
int64 newPos = Platform.BfpFile_Seek(mBfpFile, pos, .Absolute);
if (newPos != pos)
return .Err;
mBfpFilePos = pos;
}
Try!(SeekUnderlying(pos));
Platform.BfpFileResult result = .Ok;
int numBytesRead = Platform.BfpFile_Read(mBfpFile, data.Ptr, data.Length, -1, &result);
if ((result != .Ok) && (result != .PartialData))
@ -426,12 +431,8 @@ namespace System.IO
protected override Result<int> TryWriteUnderlying(int64 pos, Span<uint8> data)
{
if (mBfpFilePos != pos)
{
int64 newPos = Platform.BfpFile_Seek(mBfpFile, pos, .Absolute);
if (newPos != pos)
return .Err;
mBfpFilePos = pos;
}
Try!(SeekUnderlying(pos));
Platform.BfpFileResult result = .Ok;
int numBytesRead = Platform.BfpFile_Write(mBfpFile, data.Ptr, data.Length, -1, &result);
if ((result != .Ok) && (result != .PartialData))
@ -443,12 +444,7 @@ namespace System.IO
public Result<int> TryRead(Span<uint8> data, int timeoutMS)
{
if (mBfpFilePos != mPos)
{
int64 newPos = Platform.BfpFile_Seek(mBfpFile, mPos, .Absolute);
if (newPos != mPos)
return .Err;
mBfpFilePos = mPos;
}
Try!(SeekUnderlying(mPos));
Platform.BfpFileResult result = .Ok;
int numBytesRead = Platform.BfpFile_Read(mBfpFile, data.Ptr, data.Length, timeoutMS, &result);