Use GetByteCount instead of GetMaxByteCount (which doesn't seem to be implemented...
[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         [Serializable]
15         public class ASCIIEncoding : Encoding
16         {
17                 public ASCIIEncoding () : base ()
18                 {
19                         encoding_name = "US-ASCII";
20                         body_name = "us-ascii";
21                         header_name = "us-ascii";
22                         web_name = "us-ascii";
23                         is_browser_display = false;
24                         is_browser_save = false;
25                         is_mail_news_display = true;
26                         is_mail_news_save = true;
27                 }
28
29                 public override int GetByteCount (string chars)
30                 {
31                         if (chars == null) 
32                                 throw new ArgumentNullException ();
33
34                         return chars.Length;
35                 }
36
37                 public override int GetByteCount (char[] chars)
38                 {
39                         if (chars == null) 
40                                 throw new ArgumentNullException ();
41
42                         return chars.Length;
43                 }
44
45                 public override int GetByteCount (char[] chars, int index, int count)
46                 {
47                         if (chars == null) 
48                                 throw new ArgumentNullException ();
49
50                         if ((index < 0) || (count <= 0) || ((index + count) > chars.Length))
51                                 throw new ArgumentOutOfRangeException ();
52
53                         return count;
54                 }
55
56                 public override int GetBytes (char[] chars, int charIndex, int charCount,
57                                               byte[] bytes, int byteIndex)
58                 {
59                         if ((bytes == null) || (chars == null))
60                                 throw new ArgumentNullException ();
61
62                         if ((byteIndex < 0) || (charIndex < 0) || (charCount < 0) ||
63                             ((charIndex + charCount) > chars.Length) ||
64                             (byteIndex >= bytes.Length))
65                                 throw new ArgumentOutOfRangeException ();
66
67                         if ((bytes.Length - byteIndex) < charCount)
68                                 throw new ArgumentException ();
69
70                         for (int i = 0; i < charCount; i++)
71                                 if (chars[charIndex+i] > 0x7f)
72                                         bytes[byteIndex+i] = (byte) '?';
73                                 else
74                                         bytes[byteIndex+i] = (byte) chars[charIndex+i];
75
76                         return charCount;
77                 }
78
79                 public override int GetBytes (string chars, int charIndex, int charCount,
80                                               byte[] bytes, int byteIndex)
81                 {
82                         return GetBytes (chars.ToCharArray (), charIndex, charCount,
83                                          bytes, byteIndex);
84                 }
85
86                 public override int GetCharCount (byte[] bytes)
87                 {
88                         if (bytes == null) 
89                                 throw new ArgumentNullException ();
90
91                         return bytes.Length;
92                 }
93
94                 public override int GetCharCount (byte[] bytes, int index, int count)
95                 {
96                         if (bytes == null) 
97                                 throw new ArgumentNullException ();
98
99                         if ((index < 0) || (count <= 0) || ((index + count) > bytes.Length))
100                                 throw new ArgumentOutOfRangeException ();
101
102                         return count;
103                 }
104
105                 public override int GetChars (byte[] bytes, int byteIndex, int byteCount,
106                                               char[] chars, int charIndex)
107                 {
108                         if ((bytes == null) || (chars == null))
109                                 throw new ArgumentNullException ();
110
111                         if ((byteIndex < 0) || (charIndex < 0) || (byteCount < 0) ||
112                             ((byteIndex + byteCount) > bytes.Length) ||
113                             (charIndex + byteCount > chars.Length))
114                                 throw new ArgumentOutOfRangeException ();
115
116                         if ((chars.Length - charIndex) < byteCount)
117                                 throw new ArgumentException ();
118
119                         for (int i = 0; i < byteCount; i++)
120                                 if (bytes[byteIndex+i] > 0x7f)
121                                         chars[charIndex+i] = '?';
122                                 else
123                                         chars[charIndex+i] = (char) bytes[byteIndex+i];
124
125                         return byteCount;
126                 }
127
128                 public override int GetMaxByteCount (int charCount)
129                 {
130                         if (charCount < 0) 
131                                 throw new ArgumentOutOfRangeException ();
132
133                         return charCount;
134                 }
135
136                 public override int GetMaxCharCount (int byteCount)
137                 {
138                         if (byteCount < 0) 
139                                 throw new ArgumentOutOfRangeException ();
140
141                         return byteCount;
142                 }
143
144                 public override string GetString (byte[] bytes)
145                 {
146                         if (bytes == null) 
147                                 throw new ArgumentNullException ();
148
149                         return new String (GetChars (bytes, 0, bytes.Length));
150                 }
151
152                 public override string GetString (byte[] bytes, int byteIndex, int byteCount)
153                 {
154                         if (bytes == null) 
155                                 throw new ArgumentNullException ();
156
157                         if ((byteIndex < 0) || (byteCount < 0) || 
158                             ((byteIndex + byteCount) > bytes.Length))
159                                 throw new ArgumentOutOfRangeException ();
160
161                         return new String (GetChars (bytes, byteIndex, byteCount));
162                 }
163
164         }
165 }
166