using System;
using System.Text;
namespace NAudio.Utils
{
///
/// An encoding for use with file types that have one byte per character
///
public class ByteEncoding : Encoding
{
private ByteEncoding()
{
}
///
/// The one and only instance of this class
///
public static readonly ByteEncoding Instance = new ByteEncoding();
///
///
///
public override int GetByteCount(char[] chars, int index, int count)
{
return count;
}
///
///
///
public override int GetBytes(char[] chars, int charIndex, int charCount, byte[] bytes, int byteIndex)
{
for (int n = 0; n < charCount; n++)
{
bytes[byteIndex + n] = (byte)chars[charIndex + n];
}
return charCount;
}
///
///
///
public override int GetCharCount(byte[] bytes, int index, int count)
{
for (int n = 0; n < count; n++)
{
if (bytes[index + n] == 0)
return n;
}
return count;
}
///
///
///
public override int GetChars(byte[] bytes, int byteIndex, int byteCount, char[] chars, int charIndex)
{
for (int n = 0; n < byteCount; n++)
{
var b = bytes[byteIndex + n];
if (b == 0)
{
return n;
}
chars[charIndex + n] = (char)b;
}
return byteCount;
}
///
///
///
public override int GetMaxCharCount(int byteCount)
{
return byteCount;
}
///
///
///
public override int GetMaxByteCount(int charCount)
{
return charCount;
}
}
}