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