From 7288d9b4da49008a71dfc5ef7a7d6d7efc738184 Mon Sep 17 00:00:00 2001 From: alcexhim Date: Thu, 9 Jul 2015 15:34:46 -0400 Subject: [PATCH] Enhanced alignment functions, Writer now uses same algorithm as Reader --- .../UniversalEditor.Core/IO/Reader.cs | 4 +- .../UniversalEditor.Core/IO/Writer.cs | 43 ++++++------------- 2 files changed, 15 insertions(+), 32 deletions(-) diff --git a/CSharp/Libraries/UniversalEditor.Core/IO/Reader.cs b/CSharp/Libraries/UniversalEditor.Core/IO/Reader.cs index a95a1100..7a5bf67e 100644 --- a/CSharp/Libraries/UniversalEditor.Core/IO/Reader.cs +++ b/CSharp/Libraries/UniversalEditor.Core/IO/Reader.cs @@ -1237,9 +1237,11 @@ namespace UniversalEditor.IO /// aligned position. /// /// The number of bytes on which to align the . - public void Align(int alignTo) + /// Any additional padding bytes that should be included after aligning to the specified boundary. + public void Align(int alignTo, int extraPadding = 0) { long paddingCount = ((alignTo - (base.Accessor.Position % alignTo)) % alignTo); + paddingCount += extraPadding; base.Accessor.Position += paddingCount; } diff --git a/CSharp/Libraries/UniversalEditor.Core/IO/Writer.cs b/CSharp/Libraries/UniversalEditor.Core/IO/Writer.cs index 50741b5e..7789367d 100644 --- a/CSharp/Libraries/UniversalEditor.Core/IO/Writer.cs +++ b/CSharp/Libraries/UniversalEditor.Core/IO/Writer.cs @@ -535,38 +535,19 @@ namespace UniversalEditor.IO return num; } - - public void Align(int paddingCount) + /// + /// Aligns the to the specified number of bytes. If the current + /// position of the is not a multiple of the specified number of bytes, + /// the position will be increased by the amount of bytes necessary to bring it to the + /// aligned position. + /// + /// The number of bytes on which to align the . + /// Any additional padding bytes that should be included after aligning to the specified boundary. + public void Align(int alignTo, int extraPadding = 0) { - switch (paddingCount) - { - case 2: - { - long position = base.Accessor.Position; - int num = (int)(position % 2L); - byte[] array = new byte[num]; - array.Initialize(); - WriteBytes(array); - break; - } - case 4: - { - long num = base.Accessor.Position; - num = (num + 3L & -4L); - long num2 = num - base.Accessor.Position; - byte[] array = new byte[num2]; - array.Initialize(); - WriteBytes(array); - break; - } - default: - { - long count = (base.Accessor.Position % paddingCount); - byte[] array = new byte[count]; - WriteBytes(array); - break; - } - } + long paddingCount = ((alignTo - (base.Accessor.Position % alignTo)) % alignTo); + paddingCount += extraPadding; + base.Accessor.Position += paddingCount; }