ASCIIEncoding
Implementation of ASCII Encoding
using System;
using System.Text;
namespace Renci.SshNet.Common
{
public class ASCIIEncoding : Encoding
{
private readonly char _fallbackChar;
private static readonly char[] ByteToChar;
static ASCIIEncoding()
{
if (ByteToChar == null) {
ByteToChar = new char[128];
char c = ' ';
for (byte b = 0; b < 128; b = (byte)(b + 1)) {
char[] byteToChar = ByteToChar;
byte num = b;
char num2 = c;
c = (char)(num2 + 1);
byteToChar[num] = num2;
}
}
}
public ASCIIEncoding()
{
_fallbackChar = '?';
}
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 i = 0; i < charCount && i < chars.Length; i++) {
byte b = (byte)chars[i + charIndex];
if (b > 127)
b = (byte)_fallbackChar;
bytes[i + byteIndex] = b;
}
return charCount;
}
public override int GetCharCount(byte[] bytes, int index, int count)
{
return count;
}
public override int GetChars(byte[] bytes, int byteIndex, int byteCount, char[] chars, int charIndex)
{
for (int i = 0; i < byteCount; i++) {
byte b = bytes[i + byteIndex];
char c = chars[i + charIndex] = ((b <= 127) ? ByteToChar[b] : _fallbackChar);
}
return byteCount;
}
public override int GetMaxByteCount(int charCount)
{
if (charCount < 0)
throw new ArgumentOutOfRangeException("charCount", "Non-negative number required.");
return charCount + 1;
}
public override int GetMaxCharCount(int byteCount)
{
if (byteCount < 0)
throw new ArgumentOutOfRangeException("byteCount", "Non-negative number required.");
return byteCount;
}
}
}