move common Align() functionality into ReaderWriterBase

This commit is contained in:
Michael Becker 2019-11-15 22:13:10 -05:00
parent d3b6a1e6e6
commit f1b9f2a6b9
No known key found for this signature in database
GPG Key ID: 389DFF5D73781A12
3 changed files with 25 additions and 32 deletions

View File

@ -1506,21 +1506,6 @@ namespace UniversalEditor.IO
return buffer2;
}
/// <summary>
/// Aligns the <see cref="Reader" /> to the specified number of bytes. If the current
/// position of the <see cref="Reader" /> 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.
/// </summary>
/// <param name="alignTo">The number of bytes on which to align the <see cref="Reader"/>.</param>
/// <param name="extraPadding">Any additional padding bytes that should be included after aligning to the specified boundary.</param>
public void Align(int alignTo, int extraPadding = 0)
{
long paddingCount = ((alignTo - (base.Accessor.Position % alignTo)) % alignTo);
paddingCount += extraPadding;
base.Accessor.Position += paddingCount;
}
public string PeekFixedLengthString(int count)
{
return PeekFixedLengthString(count, base.Accessor.DefaultEncoding);

View File

@ -85,6 +85,29 @@ namespace UniversalEditor.IO
this.mvarAccessor = accessor;
this.mvarEndianness = Endianness.LittleEndian;
this.mvarReverse = false;
}
}
}
/// <summary>
/// Aligns the <see cref="Reader" /> to the specified number of bytes. If the current
/// position of the <see cref="Reader" /> 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.
/// </summary>
/// <param name="alignTo">The number of bytes on which to align the <see cref="Reader"/>.</param>
/// <param name="extraPadding">Any additional padding bytes that should be included after aligning to the specified boundary.</param>
public void Align(int alignTo, int extraPadding = 0)
{
long paddingCount = ((alignTo - (Accessor.Position % alignTo)) % alignTo);
paddingCount += extraPadding;
if (Accessor.Position == Accessor.Length)
{
Accessor.Writer.WriteBytes(new byte[paddingCount]);
}
else
{
Accessor.Position += paddingCount;
}
}
}
}

View File

@ -845,21 +845,6 @@ namespace UniversalEditor.IO
return num;
}
/// <summary>
/// Aligns the <see cref="Writer" /> to the specified number of bytes. If the current
/// position of the <see cref="Writer" /> 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.
/// </summary>
/// <param name="alignTo">The number of bytes on which to align the <see cref="Writer"/>.</param>
/// <param name="extraPadding">Any additional padding bytes that should be included after aligning to the specified boundary.</param>
public void Align(int alignTo, int extraPadding = 0)
{
long paddingCount = ((alignTo - (base.Accessor.Position % alignTo)) % alignTo);
paddingCount += extraPadding;
base.Accessor.Position += paddingCount;
}
public void WriteDoubleEndianInt16(short value)
{
WriteInt16(value);