2 * ByteEncoding.cs - Implementation of the "I18N.Common.ByteEncoding" class.
4 * Copyright (c) 2002 Southern Storm Software, Pty Ltd
6 * Permission is hereby granted, free of charge, to any person obtaining
7 * a copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 * OTHER DEALINGS IN THE SOFTWARE.
31 // This class provides an abstract base for encodings that use a single
32 // byte per character. The bulk of the work is done in this class, with
33 // subclasses providing implementations of the "ToBytes" methods to perform
34 // the char->byte conversion.
36 public abstract class ByteEncoding
: Encoding
39 protected char[] toChars
;
40 protected String encodingName
;
41 protected String bodyName
;
42 protected String headerName
;
43 protected String webName
;
44 protected bool isBrowserDisplay
;
45 protected bool isBrowserSave
;
46 protected bool isMailNewsDisplay
;
47 protected bool isMailNewsSave
;
48 protected int windowsCodePage
;
51 protected ByteEncoding(int codePage
, char[] toChars
,
52 String encodingName
, String bodyName
,
53 String headerName
, String webName
,
54 bool isBrowserDisplay
, bool isBrowserSave
,
55 bool isMailNewsDisplay
, bool isMailNewsSave
,
59 this.toChars
= toChars
;
60 this.encodingName
= encodingName
;
61 this.bodyName
= bodyName
;
62 this.headerName
= headerName
;
63 this.webName
= webName
;
64 this.isBrowserDisplay
= isBrowserDisplay
;
65 this.isBrowserSave
= isBrowserSave
;
66 this.isMailNewsDisplay
= isMailNewsDisplay
;
67 this.isMailNewsSave
= isMailNewsSave
;
68 this.windowsCodePage
= windowsCodePage
;
71 // Get the number of bytes needed to encode a character buffer.
72 public override int GetByteCount(char[] chars
, int index
, int count
)
76 throw new ArgumentNullException("chars");
78 if(index
< 0 || index
> chars
.Length
)
80 throw new ArgumentOutOfRangeException
81 ("index", Strings
.GetString("ArgRange_Array"));
83 if(count
< 0 || count
> (chars
.Length
- index
))
85 throw new ArgumentOutOfRangeException
86 ("count", Strings
.GetString("ArgRange_Array"));
91 // Convenience wrappers for "GetByteCount".
92 public override int GetByteCount(String s
)
96 throw new ArgumentNullException("s");
101 // Convert an array of characters into a byte buffer,
102 // once the parameters have been validated.
103 protected abstract void ToBytes(char[] chars
, int charIndex
, int charCount
,
104 byte[] bytes
, int byteIndex
);
106 // Convert a string into a byte buffer, once the parameters
107 // have been validated.
108 protected abstract void ToBytes(String s
, int charIndex
, int charCount
,
109 byte[] bytes
, int byteIndex
);
111 // Get the bytes that result from encoding a character buffer.
112 public override int GetBytes(char[] chars
, int charIndex
, int charCount
,
113 byte[] bytes
, int byteIndex
)
117 throw new ArgumentNullException("chars");
121 throw new ArgumentNullException("bytes");
123 if(charIndex
< 0 || charIndex
> chars
.Length
)
125 throw new ArgumentOutOfRangeException
126 ("charIndex", Strings
.GetString("ArgRange_Array"));
128 if(charCount
< 0 || charCount
> (chars
.Length
- charIndex
))
130 throw new ArgumentOutOfRangeException
131 ("charCount", Strings
.GetString("ArgRange_Array"));
133 if(byteIndex
< 0 || byteIndex
> bytes
.Length
)
135 throw new ArgumentOutOfRangeException
136 ("byteIndex", Strings
.GetString("ArgRange_Array"));
138 if((bytes
.Length
- byteIndex
) < charCount
)
140 throw new ArgumentException
141 (Strings
.GetString("Arg_InsufficientSpace"));
143 ToBytes(chars
, charIndex
, charCount
, bytes
, byteIndex
);
147 // Convenience wrappers for "GetBytes".
148 public override int GetBytes(String s
, int charIndex
, int charCount
,
149 byte[] bytes
, int byteIndex
)
153 throw new ArgumentNullException("s");
157 throw new ArgumentNullException("bytes");
159 if(charIndex
< 0 || charIndex
> s
.Length
)
161 throw new ArgumentOutOfRangeException
163 Strings
.GetString("ArgRange_StringIndex"));
165 if(charCount
< 0 || charCount
> (s
.Length
- charIndex
))
167 throw new ArgumentOutOfRangeException
169 Strings
.GetString("ArgRange_StringRange"));
171 if(byteIndex
< 0 || byteIndex
> bytes
.Length
)
173 throw new ArgumentOutOfRangeException
174 ("byteIndex", Strings
.GetString("ArgRange_Array"));
176 if((bytes
.Length
- byteIndex
) < charCount
)
178 throw new ArgumentException
179 (Strings
.GetString("Arg_InsufficientSpace"));
181 ToBytes(s
, charIndex
, charCount
, bytes
, byteIndex
);
185 // Get the number of characters needed to decode a byte buffer.
186 public override int GetCharCount(byte[] bytes
, int index
, int count
)
190 throw new ArgumentNullException("bytes");
192 if(index
< 0 || index
> bytes
.Length
)
194 throw new ArgumentOutOfRangeException
195 ("index", Strings
.GetString("ArgRange_Array"));
197 if(count
< 0 || count
> (bytes
.Length
- index
))
199 throw new ArgumentOutOfRangeException
200 ("count", Strings
.GetString("ArgRange_Array"));
205 // Get the characters that result from decoding a byte buffer.
206 public override int GetChars(byte[] bytes
, int byteIndex
, int byteCount
,
207 char[] chars
, int charIndex
)
211 throw new ArgumentNullException("bytes");
215 throw new ArgumentNullException("chars");
217 if(byteIndex
< 0 || byteIndex
> bytes
.Length
)
219 throw new ArgumentOutOfRangeException
220 ("byteIndex", Strings
.GetString("ArgRange_Array"));
222 if(byteCount
< 0 || byteCount
> (bytes
.Length
- byteIndex
))
224 throw new ArgumentOutOfRangeException
225 ("byteCount", Strings
.GetString("ArgRange_Array"));
227 if(charIndex
< 0 || charIndex
> chars
.Length
)
229 throw new ArgumentOutOfRangeException
230 ("charIndex", Strings
.GetString("ArgRange_Array"));
232 if((chars
.Length
- charIndex
) < byteCount
)
234 throw new ArgumentException
235 (Strings
.GetString("Arg_InsufficientSpace"));
237 int count
= byteCount
;
238 char[] cvt
= toChars
;
241 chars
[charIndex
++] = cvt
[(int)(bytes
[byteIndex
++])];
246 // Get the maximum number of bytes needed to encode a
247 // specified number of characters.
248 public override int GetMaxByteCount(int charCount
)
252 throw new ArgumentOutOfRangeException
254 Strings
.GetString("ArgRange_NonNegative"));
259 // Get the maximum number of characters needed to decode a
260 // specified number of bytes.
261 public override int GetMaxCharCount(int byteCount
)
265 throw new ArgumentOutOfRangeException
267 Strings
.GetString("ArgRange_NonNegative"));
272 // Decode a buffer of bytes into a string.
273 public override String
GetString(byte[] bytes
, int index
, int count
)
277 throw new ArgumentNullException("bytes");
279 if(index
< 0 || index
> bytes
.Length
)
281 throw new ArgumentOutOfRangeException
282 ("index", Strings
.GetString("ArgRange_Array"));
284 if(count
< 0 || count
> (bytes
.Length
- index
))
286 throw new ArgumentOutOfRangeException
287 ("count", Strings
.GetString("ArgRange_Array"));
289 StringBuilder s
= new StringBuilder();
290 char[] cvt
= toChars
;
293 s
.Append(cvt
[(int)(bytes
[index
++])]);
297 public override String
GetString(byte[] bytes
)
301 throw new ArgumentNullException("bytes");
303 int count
= bytes
.Length
;
305 StringBuilder s
= new StringBuilder();
306 char[] cvt
= toChars
;
309 s
.Append(cvt
[(int)(bytes
[posn
++])]);
316 // Get the mail body name for this encoding.
317 public override String BodyName
325 // Get the human-readable name for this encoding.
326 public override String EncodingName
334 // Get the mail agent header name for this encoding.
335 public override String HeaderName
343 // Determine if this encoding can be displayed in a Web browser.
344 public override bool IsBrowserDisplay
348 return isBrowserDisplay
;
352 // Determine if this encoding can be saved from a Web browser.
353 public override bool IsBrowserSave
357 return isBrowserSave
;
361 // Determine if this encoding can be displayed in a mail/news agent.
362 public override bool IsMailNewsDisplay
366 return isMailNewsDisplay
;
370 // Determine if this encoding can be saved from a mail/news agent.
371 public override bool IsMailNewsSave
375 return isMailNewsSave
;
379 // Get the IANA-preferred Web name for this encoding.
380 public override String WebName
388 // Get the Windows code page represented by this object.
389 public override int WindowsCodePage
393 return windowsCodePage
;
397 #endif // !ECMA_COMPAT
399 }; // class ByteEncoding
401 }; // namespace I18N.Encoding