diff --git a/BeefLibs/corlib/src/IO/FileStream.bf b/BeefLibs/corlib/src/IO/FileStream.bf index 3359f561..287de4fa 100644 --- a/BeefLibs/corlib/src/IO/FileStream.bf +++ b/BeefLibs/corlib/src/IO/FileStream.bf @@ -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 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 SeekUnderlying(int64 offset, Platform.BfpFileSeekKind seekKind = .Absolute) + { + int64 newPos = Platform.BfpFile_Seek(mBfpFile, offset, seekKind); + Result result = ((seekKind == .Absolute) && (newPos != offset)) ? .Err : .Ok; + if (result case .Ok) + mBfpFilePos = newPos; + return result; + } + protected override Result TryReadUnderlying(int64 pos, Span 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 TryWriteUnderlying(int64 pos, Span 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 TryRead(Span 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);