2 * BinaryEncoding handler for .Net. This class implements
3 * a symmetric encoding that will convert string to byte[]
4 * and byte[] to string without any character set
7 * The contents of this file were written by jimb
8 * at connectedsw.com on Dec 9, 2004. It is placed in
9 * the Public Domain and may be used as you see fit.
15 namespace FirebirdSql.Data.Common
17 internal class BinaryEncoding : Encoding
19 #region Static Methods
21 public static string BytesToString(byte[] byteArray)
23 // This code isn't great because it requires a double copy,
24 // but it requires unsafe code to solve the problem efficiently.
25 char[] charArray = new char[byteArray.GetLength(0)];
26 Array.Copy(byteArray, charArray, byteArray.Length);
28 return new string(charArray);
31 static void Validate(object data, int dataLength, int index, int count)
35 throw new ArgumentNullException();
38 if (index < 0 || count < 0 || dataLength - index < count)
40 throw new ArgumentOutOfRangeException();
48 public override int GetByteCount(char[] chars, int index, int count)
50 Validate(chars, chars.Length, index, count);
55 public override int GetByteCount(string chars)
60 public override int GetBytes(char[] chars, int charIndex, int charCount, byte[] bytes, int index)
62 Validate(chars, chars.Length, charIndex, charCount);
64 if (index < 0 || index > bytes.Length)
66 throw new ArgumentOutOfRangeException();
68 if (bytes.Length - index < charCount)
70 throw new ArgumentException();
73 int charEnd = charIndex + charCount;
74 while (charIndex < charEnd)
76 bytes[index++] = (byte)chars[charIndex++];
82 public override int GetBytes(string chars, int charIndex, int charCount, byte[] bytes, int index)
84 Validate(chars, chars.Length, charIndex, charCount);
86 if (index < 0 || index > bytes.Length)
88 throw new ArgumentOutOfRangeException();
90 if (bytes.Length - index < charCount)
92 throw new ArgumentException();
95 int charEnd = charIndex + charCount;
96 while (charIndex < charEnd)
98 bytes[index++] = (byte)chars[charIndex++];
104 public override int GetCharCount(byte[] bytes, int index, int count)
106 Validate(bytes, bytes.Length, index, count);
111 public override int GetChars(byte[] bytes, int index, int count, char[] chars, int charIndex)
113 Validate(bytes, bytes.Length, index, count);
115 if (charIndex < 0 || charIndex > chars.Length)
117 throw new ArgumentOutOfRangeException();
119 if (chars.Length - charIndex < count)
121 throw new ArgumentException();
124 int byteEnd = index + count;
125 while (index < byteEnd)
127 chars[charIndex++] = (char)bytes[index++];
135 public override string GetString(byte[] bytes)
137 return BytesToString(bytes);
142 public override string GetString(byte[] bytes, int index, int count)
144 Validate(bytes, bytes.Length, index, count);
146 return BytesToString(bytes);
149 public override int GetMaxByteCount(int charCount)
154 public override int GetMaxCharCount(int count)