2 * ASCIIEncoding.cs - Implementation of the "System.Text.ASCIIEncoding" class.
4 * Copyright (c) 2001 Southern Storm Software, Pty Ltd
5 * Copyright (C) 2003 Novell, Inc.
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:
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
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.
32 public class ASCIIEncoding : Encoding
34 // Magic number used by Windows for "ASCII".
35 internal const int ASCII_CODE_PAGE = 20127;
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;
45 // Get the number of bytes needed to encode a character buffer.
46 public override int GetByteCount (char[] chars, int index, int count)
49 throw new ArgumentNullException ("chars");
51 if (index < 0 || index > chars.Length) {
52 throw new ArgumentOutOfRangeException ("index", _("ArgRange_Array"));
54 if (count < 0 || count > (chars.Length - index)) {
55 throw new ArgumentOutOfRangeException ("count", _("ArgRange_Array"));
60 // Convenience wrappers for "GetByteCount".
61 public override int GetByteCount (String s)
64 throw new ArgumentNullException ("s");
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)
74 throw new ArgumentNullException ("chars");
77 throw new ArgumentNullException ("bytes");
79 if (charIndex < 0 || charIndex > chars.Length) {
80 throw new ArgumentOutOfRangeException ("charIndex", _("ArgRange_Array"));
82 if (charCount < 0 || charCount > (chars.Length - charIndex)) {
83 throw new ArgumentOutOfRangeException ("charCount", _("ArgRange_Array"));
85 if (byteIndex < 0 || byteIndex > bytes.Length) {
86 throw new ArgumentOutOfRangeException ("byteIndex", _("ArgRange_Array"));
88 if ((bytes.Length - byteIndex) < charCount) {
89 throw new ArgumentException (_("Arg_InsufficientSpace"));
91 int count = charCount;
94 ch = chars [charIndex++];
95 if (ch < (char)0x80) {
96 bytes [byteIndex++] = (byte)ch;
98 bytes [byteIndex++] = (byte)'?';
104 // Convenience wrappers for "GetBytes".
105 public override int GetBytes (String s, int charIndex, int charCount,
106 byte[] bytes, int byteIndex)
109 throw new ArgumentNullException ("s");
112 throw new ArgumentNullException ("bytes");
114 if (charIndex < 0 || charIndex > s.Length) {
115 throw new ArgumentOutOfRangeException ("charIndex", _("ArgRange_StringIndex"));
117 if (charCount < 0 || charCount > (s.Length - charIndex)) {
118 throw new ArgumentOutOfRangeException ("charCount", _("ArgRange_StringRange"));
120 if (byteIndex < 0 || byteIndex > bytes.Length) {
121 throw new ArgumentOutOfRangeException ("byteIndex", _("ArgRange_Array"));
123 if ((bytes.Length - byteIndex) < charCount) {
124 throw new ArgumentException (_("Arg_InsufficientSpace"));
126 int count = charCount;
128 while (count-- > 0) {
129 ch = s [charIndex++];
130 if (ch < (char)0x80) {
131 bytes [byteIndex++] = (byte)ch;
133 bytes [byteIndex++] = (byte)'?';
139 // Get the number of characters needed to decode a byte buffer.
140 public override int GetCharCount (byte[] bytes, int index, int count)
143 throw new ArgumentNullException ("bytes");
145 if (index < 0 || index > bytes.Length) {
146 throw new ArgumentOutOfRangeException ("index", _("ArgRange_Array"));
148 if (count < 0 || count > (bytes.Length - index)) {
149 throw new ArgumentOutOfRangeException ("count", _("ArgRange_Array"));
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)
159 throw new ArgumentNullException ("bytes");
162 throw new ArgumentNullException ("chars");
164 if (byteIndex < 0 || byteIndex > bytes.Length) {
165 throw new ArgumentOutOfRangeException ("byteIndex", _("ArgRange_Array"));
167 if (byteCount < 0 || byteCount > (bytes.Length - byteIndex)) {
168 throw new ArgumentOutOfRangeException ("byteCount", _("ArgRange_Array"));
170 if (charIndex < 0 || charIndex > chars.Length) {
171 throw new ArgumentOutOfRangeException ("charIndex", _("ArgRange_Array"));
173 if ((chars.Length - charIndex) < byteCount) {
174 throw new ArgumentException (_("Arg_InsufficientSpace"));
176 int count = byteCount;
177 while (count-- > 0) {
178 char c = (char)(bytes [byteIndex++]);
181 chars [charIndex++] = c;
186 // Get the maximum number of bytes needed to encode a
187 // specified number of characters.
188 public override int GetMaxByteCount (int charCount)
191 throw new ArgumentOutOfRangeException ("charCount", _("ArgRange_NonNegative"));
196 // Get the maximum number of characters needed to decode a
197 // specified number of bytes.
198 public override int GetMaxCharCount (int byteCount)
201 throw new ArgumentOutOfRangeException ("byteCount", _("ArgRange_NonNegative"));
206 // Decode a buffer of bytes into a string.
207 public override String GetString (byte[] bytes, int index, int count)
210 throw new ArgumentNullException ("bytes");
212 if (index < 0 || index > bytes.Length) {
213 throw new ArgumentOutOfRangeException ("index", _("ArgRange_Array"));
215 if (count < 0 || count > (bytes.Length - index)) {
216 throw new ArgumentOutOfRangeException ("count", _("ArgRange_Array"));
221 fixed (byte *ss = &bytes [0]) {
222 return new String ((sbyte*)ss, index, count);
226 public override String GetString (byte[] bytes)
229 throw new ArgumentNullException ("bytes");
231 int count = bytes.Length;
235 fixed (byte *ss = &bytes [0]) {
236 return new String ((sbyte*)ss, 0, count);
241 }; // class ASCIIEncoding
243 }; // namespace System.Text