Pad data to a WORD.
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.IO;
namespace Vestris.ResourceLib
{
/// <summary>
/// Resource utilities.
/// </summary>
public abstract class ResourceUtil
{
/// <summary>
/// Pad data to a WORD.
/// </summary>
/// <param name="w">Binary stream.</param>
/// <returns>New position within the binary stream.</returns>
internal static long PadToWORD(BinaryWriter w)
{
long pos = w.BaseStream.Position;
if (pos % 2 != 0)
{
long count = 2 - pos % 2;
Pad(w, (UInt16)count);
pos += count;
}
return pos;
}
/// <summary>
/// Pad bytes.
/// </summary>
/// <param name="w">Binary stream.</param>
/// <param name="len">Number of bytes to write.</param>
/// <returns>New position within the stream.</returns>
internal static long Pad(BinaryWriter w, UInt16 len)
{
while (len-- > 0)
w.Write((byte)0);
return w.BaseStream.Position;
}
/// <summary>
/// Pad data to a DWORD.
/// </summary>
/// <param name="w">Binary stream.</param>
/// <returns>New position within the binary stream.</returns>
internal static long PadToDWORD(BinaryWriter w)
{
long pos = w.BaseStream.Position;
if (pos % 4 != 0)
{
long count = 4 - pos % 4;
Pad(w, (UInt16)count);
pos += count;
}
return pos;
}
}
}
Related examples in the same category