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