imported everything from my branch (which is slightly harmless).
[mono.git] / mcs / class / FirebirdSql.Data.Firebird / FirebirdSql.Data.Common / BinaryEncoding.cs
1 /*\r
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
5  *  transliteration.\r
6  *\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
10  */\r
11 \r
12 using System;\r
13 using System.Text;\r
14 \r
15 namespace FirebirdSql.Data.Common\r
16 {\r
17         internal class BinaryEncoding : Encoding\r
18         {\r
19                 #region Static Methods\r
20 \r
21                 public static string BytesToString(byte[] byteArray)\r
22                 {\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
27 \r
28                         return new string(charArray);\r
29                 }\r
30 \r
31                 static void Validate(object data, int dataLength, int index, int count)\r
32                 {\r
33                         if (data == null)\r
34                         {\r
35                                 throw new ArgumentNullException();\r
36                         }\r
37 \r
38                         if (index < 0 || count < 0 || dataLength - index < count)\r
39                         {\r
40                                 throw new ArgumentOutOfRangeException();\r
41                         }\r
42                 }\r
43 \r
44                 #endregion\r
45 \r
46                 #region Methods\r
47 \r
48                 public override int GetByteCount(char[] chars, int index, int count)\r
49                 {\r
50                         Validate(chars, chars.Length, index, count);\r
51 \r
52                         return count;\r
53                 }\r
54 \r
55                 public override int GetByteCount(string chars)\r
56                 {\r
57                         return chars.Length;\r
58                 }\r
59 \r
60                 public override int GetBytes(char[] chars, int charIndex, int charCount, byte[] bytes, int index)\r
61                 {\r
62                         Validate(chars, chars.Length, charIndex, charCount);\r
63 \r
64                         if (index < 0 || index > bytes.Length)\r
65                         {\r
66                                 throw new ArgumentOutOfRangeException();\r
67                         }\r
68                         if (bytes.Length - index < charCount)\r
69                         {\r
70                                 throw new ArgumentException();\r
71                         }\r
72 \r
73                         int charEnd = charIndex + charCount;\r
74                         while (charIndex < charEnd)\r
75                         {\r
76                                 bytes[index++] = (byte)chars[charIndex++];\r
77                         }\r
78 \r
79                         return charCount;\r
80                 }\r
81 \r
82                 public override int GetBytes(string chars, int charIndex, int charCount, byte[] bytes, int index)\r
83                 {\r
84                         Validate(chars, chars.Length, charIndex, charCount);\r
85 \r
86                         if (index < 0 || index > bytes.Length)\r
87                         {\r
88                                 throw new ArgumentOutOfRangeException();\r
89                         }\r
90                         if (bytes.Length - index < charCount)\r
91                         {\r
92                                 throw new ArgumentException();\r
93                         }\r
94 \r
95                         int charEnd = charIndex + charCount;\r
96                         while (charIndex < charEnd)\r
97                         {\r
98                                 bytes[index++] = (byte)chars[charIndex++];\r
99                         }\r
100 \r
101                         return charCount;\r
102                 }\r
103 \r
104                 public override int GetCharCount(byte[] bytes, int index, int count)\r
105                 {\r
106                         Validate(bytes, bytes.Length, index, count);\r
107 \r
108                         return (count);\r
109                 }\r
110 \r
111                 public override int GetChars(byte[] bytes, int index, int count, char[] chars, int charIndex)\r
112                 {\r
113                         Validate(bytes, bytes.Length, index, count);\r
114 \r
115                         if (charIndex < 0 || charIndex > chars.Length)\r
116                         {\r
117                                 throw new ArgumentOutOfRangeException();\r
118                         }\r
119                         if (chars.Length - charIndex < count)\r
120                         {\r
121                                 throw new ArgumentException();\r
122                         }\r
123 \r
124                         int byteEnd = index + count;\r
125                         while (index < byteEnd)\r
126                         {\r
127                                 chars[charIndex++] = (char)bytes[index++];\r
128                         }\r
129 \r
130                         return count;\r
131                 }\r
132 \r
133 #if (!NETCF)\r
134         \r
135         public override string GetString(byte[] bytes)\r
136                 {\r
137                         return BytesToString(bytes);\r
138                 }\r
139 \r
140 #endif\r
141 \r
142                 public override string GetString(byte[] bytes, int index, int count)\r
143                 {\r
144                         Validate(bytes, bytes.Length, index, count);\r
145 \r
146                         return BytesToString(bytes);\r
147                 }\r
148 \r
149                 public override int GetMaxByteCount(int charCount)\r
150                 {\r
151                         return charCount;\r
152                 }\r
153 \r
154                 public override int GetMaxCharCount(int count)\r
155                 {\r
156                         return count;\r
157                 }\r
158 \r
159                 #endregion\r
160         }\r
161 }\r