File: BinaryEncoding.cs

package info (click to toggle)
mono 2.6.7-5.1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 327,344 kB
  • ctags: 413,649
  • sloc: cs: 2,471,883; xml: 1,768,594; ansic: 350,665; sh: 13,644; makefile: 8,640; perl: 1,784; asm: 717; cpp: 209; python: 146; sql: 81; sed: 16
file content (161 lines) | stat: -rw-r--r-- 3,459 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/*
 *  BinaryEncoding handler for .Net.  This class implements
 *	a symmetric encoding that will convert string to byte[]
 *  and byte[] to string without any character set
 *  transliteration.
 *
 *  The contents of this file were written by jimb
 *  at connectedsw.com on Dec 9, 2004.  It is placed in
 *  the Public Domain and may be used as you see fit.
 */

using System;
using System.Text;

namespace FirebirdSql.Data.Common
{
	internal class BinaryEncoding : Encoding
	{
		#region Static Methods

		public static string BytesToString(byte[] byteArray)
		{
			// This code isn't great because it requires a double copy,
			// but it requires unsafe code to solve the problem efficiently.
			char[] charArray = new char[byteArray.GetLength(0)];
			Array.Copy(byteArray, charArray, byteArray.Length);

			return new string(charArray);
		}

		static void Validate(object data, int dataLength, int index, int count)
		{
			if (data == null)
			{
				throw new ArgumentNullException();
			}

			if (index < 0 || count < 0 || dataLength - index < count)
			{
				throw new ArgumentOutOfRangeException();
			}
		}

		#endregion

		#region Methods

		public override int GetByteCount(char[] chars, int index, int count)
		{
			Validate(chars, chars.Length, index, count);

			return count;
		}

		public override int GetByteCount(string chars)
		{
			return chars.Length;
		}

		public override int GetBytes(char[] chars, int charIndex, int charCount, byte[] bytes, int index)
		{
			Validate(chars, chars.Length, charIndex, charCount);

			if (index < 0 || index > bytes.Length)
			{
				throw new ArgumentOutOfRangeException();
			}
			if (bytes.Length - index < charCount)
			{
				throw new ArgumentException();
			}

			int charEnd = charIndex + charCount;
			while (charIndex < charEnd)
			{
				bytes[index++] = (byte)chars[charIndex++];
			}

			return charCount;
		}

		public override int GetBytes(string chars, int charIndex, int charCount, byte[] bytes, int index)
		{
			Validate(chars, chars.Length, charIndex, charCount);

			if (index < 0 || index > bytes.Length)
			{
				throw new ArgumentOutOfRangeException();
			}
			if (bytes.Length - index < charCount)
			{
				throw new ArgumentException();
			}

			int charEnd = charIndex + charCount;
			while (charIndex < charEnd)
			{
				bytes[index++] = (byte)chars[charIndex++];
			}

			return charCount;
		}

		public override int GetCharCount(byte[] bytes, int index, int count)
		{
			Validate(bytes, bytes.Length, index, count);

			return (count);
		}

		public override int GetChars(byte[] bytes, int index, int count, char[] chars, int charIndex)
		{
			Validate(bytes, bytes.Length, index, count);

			if (charIndex < 0 || charIndex > chars.Length)
			{
				throw new ArgumentOutOfRangeException();
			}
			if (chars.Length - charIndex < count)
			{
				throw new ArgumentException();
			}

			int byteEnd = index + count;
			while (index < byteEnd)
			{
				chars[charIndex++] = (char)bytes[index++];
			}

			return count;
		}

#if (!NETCF)
	
        public override string GetString(byte[] bytes)
		{
			return BytesToString(bytes);
		}

#endif

		public override string GetString(byte[] bytes, int index, int count)
		{
			Validate(bytes, bytes.Length, index, count);

			return BytesToString(bytes);
		}

		public override int GetMaxByteCount(int charCount)
		{
			return charCount;
		}

		public override int GetMaxCharCount(int count)
		{
			return count;
		}

		#endregion
	}
}