From 81b98575c6d606371df360215dffe9ff688ce307 Mon Sep 17 00:00:00 2001 From: Brian Fiete Date: Sun, 24 Dec 2023 07:04:04 -0500 Subject: [PATCH] Made 'Align' work for both reading as well as writing --- BeefLibs/corlib/src/IO/Stream.bf | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/BeefLibs/corlib/src/IO/Stream.bf b/BeefLibs/corlib/src/IO/Stream.bf index fc863dc2..51f355df 100644 --- a/BeefLibs/corlib/src/IO/Stream.bf +++ b/BeefLibs/corlib/src/IO/Stream.bf @@ -225,11 +225,25 @@ namespace System.IO public void Align(int alignSize) { - int64 pos = Length; - int64 alignAdd = alignSize - (pos % alignSize); - if (alignAdd == alignSize) + int64 pos = Position; + int64 length = Length; + + int64 wantPos = Math.Align(pos, alignSize); + if (pos == wantPos) return; + if (wantPos < length) + { + Position = wantPos; + return; + } + + if (length > pos) + { + Position = length; + pos = length; + } + int64 alignAdd = wantPos - pos; int64 emptyData = 0; while (alignAdd > 0) {