2004-06-03 Gonzalo Paniagua Javier <gonzalo@ximian.com>
[mono.git] / mcs / class / corlib / System.Text / ASCIIEncoding.cs
1 /*
2  * ASCIIEncoding.cs - Implementation of the "System.Text.ASCIIEncoding" class.
3  *
4  * Copyright (c) 2001  Southern Storm Software, Pty Ltd
5  * Copyright (C) 2003 Novell, Inc.
6  *
7  * Permission is hereby granted, free of charge, to any person obtaining
8  * a copy of this software and associated documentation files (the "Software"),
9  * to deal in the Software without restriction, including without limitation
10  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11  * and/or sell copies of the Software, and to permit persons to whom the
12  * Software is furnished to do so, subject to the following conditions:
13  *
14  * The above copyright notice and this permission notice shall be included
15  * in all copies or substantial portions of the Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
21  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
22  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23  * OTHER DEALINGS IN THE SOFTWARE.
24  */
25
26 namespace System.Text
27 {
28
29 using System;
30
31 [Serializable]
32 public class ASCIIEncoding : Encoding
33 {
34         // Magic number used by Windows for "ASCII".
35         internal const int ASCII_CODE_PAGE = 20127;
36
37         // Constructor.
38         public ASCIIEncoding () : base(ASCII_CODE_PAGE) {
39                 body_name = header_name = web_name= "us-ascii";
40                 encoding_name = "US-ASCII";
41                 is_mail_news_display = true;
42                 is_mail_news_save = true;
43         }
44
45         // Get the number of bytes needed to encode a character buffer.
46         public override int GetByteCount (char[] chars, int index, int count)
47         {
48                 if (chars == null) {
49                         throw new ArgumentNullException ("chars");
50                 }
51                 if (index < 0 || index > chars.Length) {
52                         throw new ArgumentOutOfRangeException ("index", _("ArgRange_Array"));
53                 }
54                 if (count < 0 || count > (chars.Length - index)) {
55                         throw new ArgumentOutOfRangeException ("count", _("ArgRange_Array"));
56                 }
57                 return count;
58         }
59
60         // Convenience wrappers for "GetByteCount".
61         public override int GetByteCount (String s)
62         {
63                 if (s == null) {
64                         throw new ArgumentNullException ("s");
65                 }
66                 return s.Length;
67         }
68
69         // Get the bytes that result from encoding a character buffer.
70         public override int GetBytes (char[] chars, int charIndex, int charCount,
71                                                                  byte[] bytes, int byteIndex)
72         {
73                 if (chars == null) {
74                         throw new ArgumentNullException ("chars");
75                 }
76                 if (bytes == null) {
77                         throw new ArgumentNullException ("bytes");
78                 }
79                 if (charIndex < 0 || charIndex > chars.Length) {
80                         throw new ArgumentOutOfRangeException ("charIndex", _("ArgRange_Array"));
81                 }
82                 if (charCount < 0 || charCount > (chars.Length - charIndex)) {
83                         throw new ArgumentOutOfRangeException ("charCount", _("ArgRange_Array"));
84                 }
85                 if (byteIndex < 0 || byteIndex > bytes.Length) {
86                         throw new ArgumentOutOfRangeException ("byteIndex", _("ArgRange_Array"));
87                 }
88                 if ((bytes.Length - byteIndex) < charCount) {
89                         throw new ArgumentException (_("Arg_InsufficientSpace"));
90                 }
91                 int count = charCount;
92                 char ch;
93                 while (count-- > 0) {
94                         ch = chars [charIndex++];
95                         if (ch < (char)0x80) {
96                                 bytes [byteIndex++] = (byte)ch;
97                         } else {
98                                 bytes [byteIndex++] = (byte)'?';
99                         }
100                 }
101                 return charCount;
102         }
103
104         // Convenience wrappers for "GetBytes".
105         public override int GetBytes (String s, int charIndex, int charCount,
106                                                                  byte[] bytes, int byteIndex)
107         {
108                 if (s == null) {
109                         throw new ArgumentNullException ("s");
110                 }
111                 if (bytes == null) {
112                         throw new ArgumentNullException ("bytes");
113                 }
114                 if (charIndex < 0 || charIndex > s.Length) {
115                         throw new ArgumentOutOfRangeException ("charIndex", _("ArgRange_StringIndex"));
116                 }
117                 if (charCount < 0 || charCount > (s.Length - charIndex)) {
118                         throw new ArgumentOutOfRangeException ("charCount", _("ArgRange_StringRange"));
119                 }
120                 if (byteIndex < 0 || byteIndex > bytes.Length) {
121                         throw new ArgumentOutOfRangeException ("byteIndex", _("ArgRange_Array"));
122                 }
123                 if ((bytes.Length - byteIndex) < charCount) {
124                         throw new ArgumentException (_("Arg_InsufficientSpace"));
125                 }
126                 int count = charCount;
127                 char ch;
128                 while (count-- > 0) {
129                         ch = s [charIndex++];
130                         if (ch < (char)0x80) {
131                                 bytes [byteIndex++] = (byte)ch;
132                         } else {
133                                 bytes [byteIndex++] = (byte)'?';
134                         }
135                 }
136                 return charCount;
137         }
138
139         // Get the number of characters needed to decode a byte buffer.
140         public override int GetCharCount (byte[] bytes, int index, int count)
141         {
142                 if (bytes == null) {
143                         throw new ArgumentNullException ("bytes");
144                 }
145                 if (index < 0 || index > bytes.Length) {
146                         throw new ArgumentOutOfRangeException ("index", _("ArgRange_Array"));
147                 }
148                 if (count < 0 || count > (bytes.Length - index)) {
149                         throw new ArgumentOutOfRangeException ("count", _("ArgRange_Array"));
150                 }
151                 return count;
152         }
153
154         // Get the characters that result from decoding a byte buffer.
155         public override int GetChars (byte[] bytes, int byteIndex, int byteCount,
156                                                                  char[] chars, int charIndex)
157         {
158                 if (bytes == null) {
159                         throw new ArgumentNullException ("bytes");
160                 }
161                 if (chars == null) {
162                         throw new ArgumentNullException ("chars");
163                 }
164                 if (byteIndex < 0 || byteIndex > bytes.Length) {
165                         throw new ArgumentOutOfRangeException ("byteIndex", _("ArgRange_Array"));
166                 }
167                 if (byteCount < 0 || byteCount > (bytes.Length - byteIndex)) {
168                         throw new ArgumentOutOfRangeException ("byteCount", _("ArgRange_Array"));
169                 }
170                 if (charIndex < 0 || charIndex > chars.Length) {
171                         throw new ArgumentOutOfRangeException ("charIndex", _("ArgRange_Array"));
172                 }
173                 if ((chars.Length - charIndex) < byteCount) {
174                         throw new ArgumentException (_("Arg_InsufficientSpace"));
175                 }
176                 int count = byteCount;
177                 while (count-- > 0) {
178                         char c = (char)(bytes [byteIndex++]);
179                         if (c > 127)
180                                 c = '?';
181                         chars [charIndex++] = c;
182                 }
183                 return byteCount;
184         }
185
186         // Get the maximum number of bytes needed to encode a
187         // specified number of characters.
188         public override int GetMaxByteCount (int charCount)
189         {
190                 if (charCount < 0) {
191                         throw new ArgumentOutOfRangeException ("charCount", _("ArgRange_NonNegative"));
192                 }
193                 return charCount;
194         }
195
196         // Get the maximum number of characters needed to decode a
197         // specified number of bytes.
198         public override int GetMaxCharCount (int byteCount)
199         {
200                 if (byteCount < 0) {
201                         throw new ArgumentOutOfRangeException ("byteCount", _("ArgRange_NonNegative"));
202                 }
203                 return byteCount;
204         }
205
206         // Decode a buffer of bytes into a string.
207         public override String GetString (byte[] bytes, int index, int count)
208         {
209                 if (bytes == null) {
210                         throw new ArgumentNullException ("bytes");
211                 }
212                 if (index < 0 || index > bytes.Length) {
213                         throw new ArgumentOutOfRangeException ("index", _("ArgRange_Array"));
214                 }
215                 if (count < 0 || count > (bytes.Length - index)) {
216                         throw new ArgumentOutOfRangeException ("count", _("ArgRange_Array"));
217                 }
218                 if (count == 0)
219                     return String.Empty;
220                 unsafe {
221                         fixed (byte *ss = &bytes [0]) {
222                                 return new String ((sbyte*)ss, index, count);
223                         }
224                 }
225         }
226         public override String GetString (byte[] bytes)
227         {
228                 if (bytes == null) {
229                         throw new ArgumentNullException ("bytes");
230                 }
231                 int count = bytes.Length;
232                 if (count == 0)
233                     return String.Empty;
234                 unsafe {
235                         fixed (byte *ss = &bytes [0]) {
236                                 return new String ((sbyte*)ss, 0, count);
237                         }
238                 }
239         }
240
241 }; // class ASCIIEncoding
242
243 }; // namespace System.Text