2002-03-17 Mike Kestner <mkestner@speakeasy.net>
[mono.git] / mcs / class / corlib / System.Text / ASCIIEncoding.cs
1 //
2 // System.Text.ASCIIEncoding.cs
3 //
4 // Author:
5 //   Sean MacIsaac (macisaac@ximian.com)
6 //   Dietmar Maurer (dietmar@ximian.com)
7 //
8 // (C) Ximian, Inc.  http://www.ximian.com
9 //
10
11
12 namespace System.Text {
13         
14         public class ASCIIEncoding : Encoding
15         {
16                 public ASCIIEncoding () : base ()
17                 {
18                         encoding_name = "US-ASCII";
19                         body_name = "us-ascii";
20                         header_name = "us-ascii";
21                         web_name = "us-ascii";
22                         is_browser_display = false;
23                         is_browser_save = false;
24                         is_mail_news_display = true;
25                         is_mail_news_save = true;
26                 }
27
28                 public override int GetByteCount (string chars)
29                 {
30                         if (chars == null) 
31                                 throw new ArgumentNullException ();
32
33                         return chars.Length;
34                 }
35
36                 public override int GetByteCount (char[] chars)
37                 {
38                         if (chars == null) 
39                                 throw new ArgumentNullException ();
40
41                         return chars.Length;
42                 }
43
44                 public override int GetByteCount (char[] chars, int index, int count)
45                 {
46                         if (chars == null) 
47                                 throw new ArgumentNullException ();
48
49                         if ((index < 0) || (count <= 0) || ((index + count) >= chars.Length))
50                                 throw new ArgumentOutOfRangeException ();
51
52                         return count;
53                 }
54
55                 public override int GetBytes (char[] chars, int charIndex, int charCount,
56                                               byte[] bytes, int byteIndex)
57                 {
58                         if ((bytes == null) || (chars == null))
59                                 throw new ArgumentNullException ();
60
61                         if ((byteIndex < 0) || (charIndex < 0) || (charCount < 0) ||
62                             ((charIndex + charCount) > chars.Length) ||
63                             (byteIndex >= bytes.Length))
64                                 throw new ArgumentOutOfRangeException ();
65
66                         if ((bytes.Length - byteIndex) < charCount)
67                                 throw new ArgumentException ();
68
69                         for (int i = 0; i < charCount; i++)
70                                 if (chars[charIndex+i] > 0x7f)
71                                         bytes[byteIndex+i] = (byte) '?';
72                                 else
73                                         bytes[byteIndex+i] = (byte) chars[charIndex+i];
74
75                         return charCount;
76                 }
77
78                 public override int GetBytes (string chars, int charIndex, int charCount,
79                                               byte[] bytes, int byteIndex)
80                 {
81                         return GetBytes (chars.ToCharArray (), charIndex, charCount,
82                                          bytes, byteIndex);
83                 }
84
85                 public override int GetCharCount (byte[] bytes)
86                 {
87                         if (bytes == null) 
88                                 throw new ArgumentNullException ();
89
90                         return bytes.Length;
91                 }
92
93                 public override int GetCharCount (byte[] bytes, int index, int count)
94                 {
95                         if (bytes == null) 
96                                 throw new ArgumentNullException ();
97
98                         if ((index < 0) || (count <= 0) || ((index + count) >= bytes.Length))
99                                 throw new ArgumentOutOfRangeException ();
100
101                         return count;
102                 }
103
104                 public override int GetChars (byte[] bytes, int byteIndex, int byteCount,
105                                               char[] chars, int charIndex)
106                 {
107                         if ((bytes == null) || (chars == null))
108                                 throw new ArgumentNullException ();
109
110                         if ((byteIndex < 0) || (charIndex < 0) || (byteCount < 0) ||
111                             ((byteIndex + byteCount) > bytes.Length) ||
112                             (charIndex >= chars.Length))
113                                 throw new ArgumentOutOfRangeException ();
114
115                         if ((chars.Length - charIndex) < byteCount)
116                                 throw new ArgumentException ();
117
118                         for (int i = 0; i < byteCount; i++)
119                                 if (bytes[byteIndex+i] > 0x7f)
120                                         chars[charIndex+i] = '?';
121                                 else
122                                         chars[charIndex+i] = (char) bytes[byteIndex+i];
123
124                         return byteCount;
125                 }
126
127                 public override int GetMaxByteCount (int charCount)
128                 {
129                         if (charCount < 0) 
130                                 throw new ArgumentOutOfRangeException ();
131
132                         return charCount;
133                 }
134
135                 public override int GetMaxCharCount (int byteCount)
136                 {
137                         if (byteCount < 0) 
138                                 throw new ArgumentOutOfRangeException ();
139
140                         return byteCount;
141                 }
142
143                 public override string GetString (byte[] bytes)
144                 {
145                         if (bytes == null) 
146                                 throw new ArgumentNullException ();
147
148                         return new String (GetChars (bytes, 0, bytes.Length));
149                 }
150
151                 public override string GetString (byte[] bytes, int byteIndex, int byteCount)
152                 {
153                         if (bytes == null) 
154                                 throw new ArgumentNullException ();
155
156                         if ((byteIndex < 0) || (byteCount <= 0) || 
157                             ((byteIndex + byteCount) >= bytes.Length))
158                                 throw new ArgumentOutOfRangeException ();
159
160                         return new String (GetChars (bytes, byteIndex, byteCount));
161                 }
162
163         }
164 }
165