2 * Encoding.cs - Implementation of the "System.Text.Encoding" class.
4 * Copyright (c) 2001, 2002 Southern Storm Software, Pty Ltd
5 * Copyright (c) 2002, Ximian, Inc.
6 * Copyright (c) 2003, 2004 Novell, Inc.
8 * Permission is hereby granted, free of charge, to any person obtaining
9 * a copy of this software and associated documentation files (the "Software"),
10 * to deal in the Software without restriction, including without limitation
11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 * and/or sell copies of the Software, and to permit persons to whom the
13 * Software is furnished to do so, subject to the following conditions:
15 * The above copyright notice and this permission notice shall be included
16 * in all copies or substantial portions of the Software.
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
22 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
23 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
24 * OTHER DEALINGS IN THE SOFTWARE.
31 using System.Reflection;
32 using System.Globalization;
33 using System.Security;
34 using System.Runtime.CompilerServices;
35 using System.Runtime.InteropServices;
41 public abstract class Encoding
46 // Code page used by this encoding.
47 internal int codePage;
48 internal int windows_code_page;
49 bool is_readonly = true;
61 Encoding (int codePage)
63 this.codePage = windows_code_page = codePage;
68 // MS has "InternalBestFit{Decoder|Encoder}Fallback
69 // here, but we dunno what they are for.
70 decoder_fallback = DecoderFallback.ReplacementFallback;
71 encoder_fallback = EncoderFallback.ReplacementFallback;
74 case 54936: // GB18030
75 decoder_fallback = DecoderFallback.ReplacementFallback;
76 encoder_fallback = EncoderFallback.ReplacementFallback;
84 decoder_fallback = new DecoderReplacementFallback (String.Empty);
85 encoder_fallback = new EncoderReplacementFallback (String.Empty);
91 // until we change the callers:
92 internal static string _ (string arg) {
97 DecoderFallback decoder_fallback;
98 EncoderFallback encoder_fallback;
101 public bool IsReadOnly {
102 get { return is_readonly; }
106 public virtual bool IsSingleByte {
107 get { return false; }
110 [MonoTODO ("not used yet")]
112 public DecoderFallback DecoderFallback {
114 if (decoder_fallback == null)
115 decoder_fallback = new DecoderReplacementFallback (String.Empty);
116 return decoder_fallback;
120 throw new InvalidOperationException ("This Encoding is readonly.");
122 throw new ArgumentNullException ();
123 decoder_fallback = value;
127 [MonoTODO ("not used yet")]
129 public EncoderFallback EncoderFallback {
131 if (encoder_fallback == null)
132 encoder_fallback = new EncoderReplacementFallback (String.Empty);
133 return encoder_fallback;
137 throw new InvalidOperationException ("This Encoding is readonly.");
139 throw new ArgumentNullException ();
140 encoder_fallback = value;
144 internal void SetFallbackInternal (EncoderFallback e, DecoderFallback d)
147 encoder_fallback = e;
149 decoder_fallback = d;
153 // Convert between two encodings.
154 public static byte[] Convert (Encoding srcEncoding, Encoding dstEncoding,
157 if (srcEncoding == null) {
158 throw new ArgumentNullException ("srcEncoding");
160 if (dstEncoding == null) {
161 throw new ArgumentNullException ("dstEncoding");
164 throw new ArgumentNullException ("bytes");
166 return dstEncoding.GetBytes (srcEncoding.GetChars (bytes, 0, bytes.Length));
168 public static byte[] Convert (Encoding srcEncoding, Encoding dstEncoding,
169 byte[] bytes, int index, int count)
171 if (srcEncoding == null) {
172 throw new ArgumentNullException ("srcEncoding");
174 if (dstEncoding == null) {
175 throw new ArgumentNullException ("dstEncoding");
178 throw new ArgumentNullException ("bytes");
180 if (index < 0 || index > bytes.Length) {
181 throw new ArgumentOutOfRangeException
182 ("index", _("ArgRange_Array"));
184 if (count < 0 || (bytes.Length - index) < count) {
185 throw new ArgumentOutOfRangeException
186 ("count", _("ArgRange_Array"));
188 return dstEncoding.GetBytes (srcEncoding.GetChars (bytes, index, count));
191 // Determine if two Encoding objects are equal.
192 public override bool Equals (Object obj)
194 Encoding enc = (obj as Encoding);
197 return codePage == enc.codePage &&
198 DecoderFallback.Equals (enc.DecoderFallback) &&
199 EncoderFallback.Equals (enc.EncoderFallback);
201 return (codePage == enc.codePage);
208 // Get the number of characters needed to encode a character buffer.
209 public abstract int GetByteCount (char[] chars, int index, int count);
211 // Convenience wrappers for "GetByteCount".
212 public virtual int GetByteCount (String s)
215 throw new ArgumentNullException ("s");
221 fixed (char* cptr = s) {
222 return GetByteCount (cptr, s.Length);
226 char[] chars = s.ToCharArray ();
227 return GetByteCount (chars, 0, chars.Length);
230 public virtual int GetByteCount (char[] chars)
233 return GetByteCount (chars, 0, chars.Length);
235 throw new ArgumentNullException ("chars");
239 // Get the bytes that result from encoding a character buffer.
240 public abstract int GetBytes (char[] chars, int charIndex, int charCount,
241 byte[] bytes, int byteIndex);
243 // Convenience wrappers for "GetBytes".
244 public virtual int GetBytes (String s, int charIndex, int charCount,
245 byte[] bytes, int byteIndex)
248 throw new ArgumentNullException ("s");
250 if (charIndex < 0 || charIndex > s.Length)
251 throw new ArgumentOutOfRangeException ("charIndex", _("ArgRange_Array"));
252 if (charCount < 0 || charIndex + charCount > s.Length)
253 throw new ArgumentOutOfRangeException ("charCount", _("ArgRange_Array"));
254 if (byteIndex < 0 || byteIndex > bytes.Length)
255 throw new ArgumentOutOfRangeException ("byteIndex", _("ArgRange_Array"));
257 if (charCount == 0 || bytes.Length == byteIndex)
260 fixed (char* cptr = s) {
261 fixed (byte* bptr = bytes) {
262 return GetBytes (cptr + charIndex,
265 bytes.Length - byteIndex);
270 return GetBytes (s.ToCharArray(), charIndex, charCount, bytes, byteIndex);
273 public virtual byte[] GetBytes (String s)
276 throw new ArgumentNullException ("s");
281 int byteCount = GetByteCount (s);
285 fixed (char* cptr = s) {
286 byte [] bytes = new byte [byteCount];
287 fixed (byte* bptr = bytes) {
288 GetBytes (cptr, s.Length,
295 char[] chars = s.ToCharArray ();
296 int numBytes = GetByteCount (chars, 0, chars.Length);
297 byte[] bytes = new byte [numBytes];
298 GetBytes (chars, 0, chars.Length, bytes, 0);
302 public virtual byte[] GetBytes (char[] chars, int index, int count)
304 int numBytes = GetByteCount (chars, index, count);
305 byte[] bytes = new byte [numBytes];
306 GetBytes (chars, index, count, bytes, 0);
309 public virtual byte[] GetBytes (char[] chars)
311 int numBytes = GetByteCount (chars, 0, chars.Length);
312 byte[] bytes = new byte [numBytes];
313 GetBytes (chars, 0, chars.Length, bytes, 0);
317 // Get the number of characters needed to decode a byte buffer.
318 public abstract int GetCharCount (byte[] bytes, int index, int count);
320 // Convenience wrappers for "GetCharCount".
321 public virtual int GetCharCount (byte[] bytes)
324 throw new ArgumentNullException ("bytes");
326 return GetCharCount (bytes, 0, bytes.Length);
329 // Get the characters that result from decoding a byte buffer.
330 public abstract int GetChars (byte[] bytes, int byteIndex, int byteCount,
331 char[] chars, int charIndex);
333 // Convenience wrappers for "GetChars".
334 public virtual char[] GetChars (byte[] bytes, int index, int count)
336 int numChars = GetCharCount (bytes, index, count);
337 char[] chars = new char [numChars];
338 GetChars (bytes, index, count, chars, 0);
341 public virtual char[] GetChars (byte[] bytes)
344 throw new ArgumentNullException ("bytes");
346 int numChars = GetCharCount (bytes, 0, bytes.Length);
347 char[] chars = new char [numChars];
348 GetChars (bytes, 0, bytes.Length, chars, 0);
352 // Get a decoder that forwards requests to this object.
353 public virtual Decoder GetDecoder ()
355 return new ForwardingDecoder (this);
358 // Get an encoder that forwards requests to this object.
359 public virtual Encoder GetEncoder ()
361 return new ForwardingEncoder (this);
364 // Loaded copy of the "I18N" assembly. We need to move
365 // this into a class in "System.Private" eventually.
366 private static Assembly i18nAssembly;
367 private static bool i18nDisabled;
369 // Invoke a specific method on the "I18N" manager object.
370 // Returns NULL if the method failed.
371 private static Object InvokeI18N (String name, params Object[] args)
374 // Bail out if we previously detected that there
375 // is insufficent engine support for I18N handling.
380 // Find or load the "I18N" assembly.
381 if (i18nAssembly == null) {
384 i18nAssembly = Assembly.Load (Consts.AssemblyI18N);
385 } catch (NotImplementedException) {
386 // Assembly loading unsupported by the engine.
390 if (i18nAssembly == null) {
393 } catch (SystemException) {
398 // Find the "I18N.Common.Manager" class.
401 managerClass = i18nAssembly.GetType ("I18N.Common.Manager");
402 } catch (NotImplementedException) {
403 // "GetType" is not supported by the engine.
407 if (managerClass == null) {
411 // Get the value of the "PrimaryManager" property.
414 manager = managerClass.InvokeMember
416 BindingFlags.GetProperty |
417 BindingFlags.Static |
419 null, null, null, null, null, null);
420 if (manager == null) {
423 } catch (MissingMethodException) {
425 } catch (SecurityException) {
427 } catch (NotImplementedException) {
428 // "InvokeMember" is not supported by the engine.
433 // Invoke the requested method on the manager.
435 return managerClass.InvokeMember
437 BindingFlags.InvokeMethod |
438 BindingFlags.Instance |
440 null, manager, args, null, null, null);
441 } catch (MissingMethodException) {
443 } catch (SecurityException) {
449 // Get an encoder for a specific code page.
455 static Encoding GetEncoding (int codePage)
457 // Check for the builtin code pages first.
459 case 0: return Default;
461 case ASCIIEncoding.ASCII_CODE_PAGE:
464 case UTF7Encoding.UTF7_CODE_PAGE:
467 case UTF8Encoding.UTF8_CODE_PAGE:
471 case UTF32Encoding.UTF32_CODE_PAGE:
474 case UTF32Encoding.BIG_UTF32_CODE_PAGE:
475 return BigEndianUTF32;
478 case UnicodeEncoding.UNICODE_CODE_PAGE:
481 case UnicodeEncoding.BIG_UNICODE_CODE_PAGE:
482 return BigEndianUnicode;
484 case Latin1Encoding.ISOLATIN_CODE_PAGE:
490 // Try to obtain a code page handler from the I18N handler.
491 Encoding enc = (Encoding)(InvokeI18N ("GetEncoding", codePage));
493 enc.is_readonly = true;
497 // Build a code page class name.
498 String cpName = "System.Text.CP" + codePage.ToString ();
500 // Look for a code page converter in this assembly.
501 Assembly assembly = Assembly.GetExecutingAssembly ();
502 Type type = assembly.GetType (cpName);
504 enc = (Encoding)(Activator.CreateInstance (type));
505 enc.is_readonly = true;
509 // Look in any assembly, in case the application
510 // has provided its own code page handler.
511 type = Type.GetType (cpName);
513 enc = (Encoding)(Activator.CreateInstance (type));
514 enc.is_readonly = true;
518 // We have no idea how to handle this code page.
519 throw new NotSupportedException
520 (String.Format ("CodePage {0} not supported", codePage.ToString ()));
527 public virtual object Clone ()
529 Encoding e = (Encoding) MemberwiseClone ();
530 e.is_readonly = false;
534 public static Encoding GetEncoding (int codePage,
535 EncoderFallback encoderFallback, DecoderFallback decoderFallback)
537 if (encoderFallback == null)
538 throw new ArgumentNullException ("encoderFallback");
539 if (decoderFallback == null)
540 throw new ArgumentNullException ("decoderFallback");
542 Encoding e = GetEncoding (codePage).Clone () as Encoding;
543 e.is_readonly = false;
544 e.encoder_fallback = encoderFallback;
545 e.decoder_fallback = decoderFallback;
549 public static Encoding GetEncoding (string name,
550 EncoderFallback encoderFallback, DecoderFallback decoderFallback)
552 if (encoderFallback == null)
553 throw new ArgumentNullException ("encoderFallback");
554 if (decoderFallback == null)
555 throw new ArgumentNullException ("decoderFallback");
557 Encoding e = GetEncoding (name).Clone () as Encoding;
558 e.is_readonly = false;
559 e.encoder_fallback = encoderFallback;
560 e.decoder_fallback = decoderFallback;
564 static EncodingInfo [] encoding_infos;
566 // FIXME: As everyone would agree, this implementation is so *hacky*
567 // and could be very easily broken. But since there is a test for
568 // this method to make sure that this method always returns
569 // the same number and content of encoding infos, this won't
570 // matter practically.
571 public static EncodingInfo[] GetEncodings ()
573 if (encoding_infos == null) {
574 int [] codepages = new int [] {
576 850, 852, 855, 857, 858, 860, 861, 862, 863,
577 864, 865, 866, 869, 870, 874, 875,
579 1026, 1047, 1140, 1141, 1142, 1143, 1144,
580 1145, 1146, 1147, 1148, 1149,
581 1200, 1201, 1250, 1251, 1252, 1253, 1254,
582 1255, 1256, 1257, 1258,
583 10000, 10079, 12000, 12001,
584 20127, 20273, 20277, 20278, 20280, 20284,
585 20285, 20290, 20297, 20420, 20424, 20866,
586 20871, 21025, 21866, 28591, 28592, 28593,
587 28594, 28595, 28596, 28597, 28598, 28599,
589 50220, 50221, 50222, 51932, 51949, 54936,
590 57002, 57003, 57004, 57005, 57006, 57007,
591 57008, 57009, 57010, 57011,
594 encoding_infos = new EncodingInfo [codepages.Length];
595 for (int i = 0; i < codepages.Length; i++)
596 encoding_infos [i] = new EncodingInfo (codepages [i]);
598 return encoding_infos;
601 public bool IsAlwaysNormalized ()
603 return IsAlwaysNormalized (NormalizationForm.FormC);
606 public virtual bool IsAlwaysNormalized (NormalizationForm form)
608 // umm, ASCIIEncoding should have overriden this method, no?
609 return form == NormalizationForm.FormC && this is ASCIIEncoding;
613 // Table of builtin web encoding names and the corresponding code pages.
614 private static readonly object[] encodings =
616 ASCIIEncoding.ASCII_CODE_PAGE,
617 "ascii", "us_ascii", "us", "ansi_x3.4_1968",
618 "ansi_x3.4_1986", "cp367", "csascii", "ibm367",
619 "iso_ir_6", "iso646_us", "iso_646.irv:1991",
621 UTF7Encoding.UTF7_CODE_PAGE,
622 "utf_7", "csunicode11utf7", "unicode_1_1_utf_7",
623 "unicode_2_0_utf_7", "x_unicode_1_1_utf_7",
624 "x_unicode_2_0_utf_7",
626 UTF8Encoding.UTF8_CODE_PAGE,
627 "utf_8", "unicode_1_1_utf_8", "unicode_2_0_utf_8",
628 "x_unicode_1_1_utf_8", "x_unicode_2_0_utf_8",
630 UnicodeEncoding.UNICODE_CODE_PAGE,
631 "utf_16", "UTF_16LE", "ucs_2", "unicode",
634 UnicodeEncoding.BIG_UNICODE_CODE_PAGE,
635 "unicodefffe", "utf_16be",
638 UTF32Encoding.UTF32_CODE_PAGE,
639 "utf_32", "UTF_32LE", "ucs_4",
641 UTF32Encoding.BIG_UTF32_CODE_PAGE,
645 Latin1Encoding.ISOLATIN_CODE_PAGE,
646 "iso_8859_1", "latin1"
649 // Get an encoding object for a specific web encoding name.
650 public static Encoding GetEncoding (String name)
652 // Validate the parameters.
654 throw new ArgumentNullException ("name");
657 string converted = name.ToLowerInvariant ().Replace ('-', '_');
659 // Search the table for a name match.
661 for (int i = 0; i < encodings.Length; ++i) {
662 object o = encodings [i];
669 if (converted == ((string)encodings [i]))
670 return GetEncoding (code);
673 // Try to obtain a web encoding handler from the I18N handler.
674 Encoding enc = (Encoding)(InvokeI18N ("GetEncoding", name));
679 // Build a web encoding class name.
680 String encName = "System.Text.ENC" + converted;
683 // Look for a code page converter in this assembly.
684 Assembly assembly = Assembly.GetExecutingAssembly ();
685 Type type = assembly.GetType (encName);
687 return (Encoding)(Activator.CreateInstance (type));
690 // Look in any assembly, in case the application
691 // has provided its own code page handler.
692 type = Type.GetType (encName);
694 return (Encoding)(Activator.CreateInstance (type));
697 // We have no idea how to handle this encoding name.
698 throw new NotSupportedException (String.Format ("Encoding name `{0}' not supported", name));
701 #endif // !ECMA_COMPAT
703 // Get a hash code for this instance.
704 public override int GetHashCode ()
707 return DecoderFallback.GetHashCode () << 24 + EncoderFallback.GetHashCode () << 16 + codePage;
713 // Get the maximum number of bytes needed to encode a
714 // specified number of characters.
715 public abstract int GetMaxByteCount (int charCount);
717 // Get the maximum number of characters needed to decode a
718 // specified number of bytes.
719 public abstract int GetMaxCharCount (int byteCount);
721 // Get the identifying preamble for this encoding.
722 public virtual byte[] GetPreamble ()
727 // Decode a buffer of bytes into a string.
728 public virtual String GetString (byte[] bytes, int index, int count)
730 return new String (GetChars(bytes, index, count));
732 public virtual String GetString (byte[] bytes)
735 throw new ArgumentNullException ("bytes");
737 return GetString (bytes, 0, bytes.Length);
742 internal string body_name;
743 internal string encoding_name;
744 internal string header_name;
745 internal bool is_mail_news_display;
746 internal bool is_mail_news_save;
747 internal bool is_browser_save = false;
748 internal bool is_browser_display = false;
749 internal string web_name;
751 // Get the mail body name for this encoding.
752 public virtual String BodyName
759 // Get the code page represented by this object.
760 public virtual int CodePage
767 // Get the human-readable name for this encoding.
768 public virtual String EncodingName
771 return encoding_name;
775 // Get the mail agent header name for this encoding.
776 public virtual String HeaderName
783 // Determine if this encoding can be displayed in a Web browser.
784 public virtual bool IsBrowserDisplay
787 return is_browser_display;
791 // Determine if this encoding can be saved from a Web browser.
792 public virtual bool IsBrowserSave
795 return is_browser_save;
799 // Determine if this encoding can be displayed in a mail/news agent.
800 public virtual bool IsMailNewsDisplay
803 return is_mail_news_display;
807 // Determine if this encoding can be saved from a mail/news agent.
808 public virtual bool IsMailNewsSave
811 return is_mail_news_save;
815 // Get the IANA-preferred Web name for this encoding.
816 public virtual String WebName
823 // Get the Windows code page represented by this object.
824 public virtual int WindowsCodePage
827 // We make no distinction between normal and
828 // Windows code pages in this implementation.
829 return windows_code_page;
833 #endif // !ECMA_COMPAT
835 // Storage for standard encoding objects.
836 static volatile Encoding asciiEncoding;
837 static volatile Encoding bigEndianEncoding;
838 static volatile Encoding defaultEncoding;
839 static volatile Encoding utf7Encoding;
840 static volatile Encoding utf8EncodingWithMarkers;
841 static volatile Encoding utf8EncodingWithoutMarkers;
842 static volatile Encoding unicodeEncoding;
843 static volatile Encoding isoLatin1Encoding;
844 static volatile Encoding unixConsoleEncoding;
846 static volatile Encoding utf32Encoding;
847 static volatile Encoding bigEndianUTF32Encoding;
850 static readonly object lockobj = new object ();
852 // Get the standard ASCII encoding object.
853 public static Encoding ASCII
856 if (asciiEncoding == null) {
858 if (asciiEncoding == null) {
859 asciiEncoding = new ASCIIEncoding ();
860 asciiEncoding.is_readonly = true;
865 return asciiEncoding;
869 // Get the standard big-endian Unicode encoding object.
870 public static Encoding BigEndianUnicode
873 if (bigEndianEncoding == null) {
875 if (bigEndianEncoding == null) {
876 bigEndianEncoding = new UnicodeEncoding (true, true);
877 bigEndianEncoding.is_readonly = true;
882 return bigEndianEncoding;
886 [MethodImpl (MethodImplOptions.InternalCall)]
887 extern internal static string InternalCodePage (ref int code_page);
889 // Get the default encoding object.
890 public static Encoding Default
893 if (defaultEncoding == null) {
895 if (defaultEncoding == null) {
896 // See if the underlying system knows what
897 // code page handler we should be using.
900 string code_page_name = InternalCodePage (ref code_page);
903 defaultEncoding = GetEncoding (code_page_name);
905 // map the codepage from internal to our numbers
906 code_page = code_page & 0x0fffffff;
908 case 1: code_page = ASCIIEncoding.ASCII_CODE_PAGE; break;
909 case 2: code_page = UTF7Encoding.UTF7_CODE_PAGE; break;
910 case 3: code_page = UTF8Encoding.UTF8_CODE_PAGE; break;
911 case 4: code_page = UnicodeEncoding.UNICODE_CODE_PAGE; break;
912 case 5: code_page = UnicodeEncoding.BIG_UNICODE_CODE_PAGE; break;
913 case 6: code_page = Latin1Encoding.ISOLATIN_CODE_PAGE; break;
915 defaultEncoding = GetEncoding (code_page);
917 } catch (NotSupportedException) {
918 defaultEncoding = UTF8Unmarked;
920 defaultEncoding.is_readonly = true;
925 return defaultEncoding;
929 // Get the ISO Latin1 encoding object.
930 private static Encoding ISOLatin1
933 if (isoLatin1Encoding == null) {
935 if (isoLatin1Encoding == null) {
936 isoLatin1Encoding = new Latin1Encoding ();
937 isoLatin1Encoding.is_readonly = true;
942 return isoLatin1Encoding;
946 // Get the standard UTF-7 encoding object.
955 if (utf7Encoding == null) {
957 if (utf7Encoding == null) {
958 utf7Encoding = new UTF7Encoding ();
959 utf7Encoding.is_readonly = true;
968 // Get the standard UTF-8 encoding object.
969 public static Encoding UTF8
972 if (utf8EncodingWithMarkers == null) {
974 if (utf8EncodingWithMarkers == null) {
975 utf8EncodingWithMarkers = new UTF8Encoding (true);
976 utf8EncodingWithMarkers.is_readonly = true;
981 return utf8EncodingWithMarkers;
986 // Only internal, to be used by the class libraries: Unmarked and non-input-validating
988 internal static Encoding UTF8Unmarked {
990 if (utf8EncodingWithoutMarkers == null) {
992 if (utf8EncodingWithoutMarkers == null){
993 utf8EncodingWithoutMarkers = new UTF8Encoding (false, false);
994 utf8EncodingWithoutMarkers.is_readonly = true;
999 return utf8EncodingWithoutMarkers;
1003 // Get the standard little-endian Unicode encoding object.
1004 public static Encoding Unicode
1007 if (unicodeEncoding == null) {
1009 if (unicodeEncoding == null) {
1010 unicodeEncoding = new UnicodeEncoding (false, true);
1011 unicodeEncoding.is_readonly = true;
1016 return unicodeEncoding;
1021 // Get the standard little-endian UTF-32 encoding object.
1022 public static Encoding UTF32
1025 if (utf32Encoding == null) {
1027 if (utf32Encoding == null) {
1028 utf32Encoding = new UTF32Encoding (false, true);
1029 utf32Encoding.is_readonly = true;
1034 return utf32Encoding;
1038 // Get the standard big-endian UTF-32 encoding object.
1039 private static Encoding BigEndianUTF32
1042 if (bigEndianUTF32Encoding == null) {
1044 if (bigEndianUTF32Encoding == null) {
1045 bigEndianUTF32Encoding = new UTF32Encoding (true, true);
1046 bigEndianUTF32Encoding.is_readonly = true;
1051 return bigEndianUTF32Encoding;
1056 // Forwarding decoder implementation.
1057 private sealed class ForwardingDecoder : Decoder
1059 private Encoding encoding;
1062 public ForwardingDecoder (Encoding enc)
1066 Fallback = encoding.DecoderFallback;
1070 // Override inherited methods.
1071 public override int GetCharCount (byte[] bytes, int index, int count)
1073 return encoding.GetCharCount (bytes, index, count);
1075 public override int GetChars (byte[] bytes, int byteIndex,
1076 int byteCount, char[] chars,
1079 return encoding.GetChars (bytes, byteIndex, byteCount, chars, charIndex);
1082 } // class ForwardingDecoder
1084 // Forwarding encoder implementation.
1085 private sealed class ForwardingEncoder : Encoder
1087 private Encoding encoding;
1090 public ForwardingEncoder (Encoding enc)
1094 Fallback = encoding.EncoderFallback;
1098 // Override inherited methods.
1099 public override int GetByteCount (char[] chars, int index, int count, bool flush)
1101 return encoding.GetByteCount (chars, index, count);
1103 public override int GetBytes (char[] chars, int charIndex,
1104 int charCount, byte[] bytes,
1105 int byteCount, bool flush)
1107 return encoding.GetBytes (chars, charIndex, charCount, bytes, byteCount);
1110 } // class ForwardingEncoder
1113 [CLSCompliantAttribute(false)]
1114 [ComVisible (false)]
1115 public unsafe virtual int GetByteCount (char *chars, int count)
1118 throw new ArgumentNullException ("chars");
1120 throw new ArgumentOutOfRangeException ("count");
1121 char [] c = new char [count];
1123 for (int p = 0; p < count; p++)
1126 return GetByteCount (c);
1129 [CLSCompliantAttribute(false)]
1130 [ComVisible (false)]
1131 public unsafe virtual int GetCharCount (byte *bytes, int count)
1134 throw new ArgumentNullException ("bytes");
1136 throw new ArgumentOutOfRangeException ("count");
1138 byte [] ba = new byte [count];
1139 for (int i = 0; i < count; i++)
1141 return GetCharCount (ba, 0, count);
1144 [CLSCompliantAttribute(false)]
1145 [ComVisible (false)]
1146 public unsafe virtual int GetChars (byte *bytes, int byteCount, char *chars, int charCount)
1149 throw new ArgumentNullException ("bytes");
1151 throw new ArgumentNullException ("chars");
1153 throw new ArgumentOutOfRangeException ("charCount");
1155 throw new ArgumentOutOfRangeException ("byteCount");
1157 byte [] ba = new byte [byteCount];
1158 for (int i = 0; i < byteCount; i++)
1160 char [] ret = GetChars (ba, 0, byteCount);
1161 int top = ret.Length;
1163 if (top > charCount)
1164 throw new ArgumentException ("charCount is less than the number of characters produced", "charCount");
1166 for (int i = 0; i < top; i++)
1167 chars [i] = ret [i];
1171 [CLSCompliantAttribute(false)]
1172 [ComVisible (false)]
1173 public unsafe virtual int GetBytes (char *chars, int charCount, byte *bytes, int byteCount)
1176 throw new ArgumentNullException ("bytes");
1178 throw new ArgumentNullException ("chars");
1180 throw new ArgumentOutOfRangeException ("charCount");
1182 throw new ArgumentOutOfRangeException ("byteCount");
1184 char [] c = new char [charCount];
1186 for (int i = 0; i < charCount; i++)
1189 byte [] b = GetBytes (c, 0, charCount);
1191 if (top > byteCount)
1192 throw new ArgumentException ("byteCount is less that the number of bytes produced", "byteCount");
1194 for (int i = 0; i < top; i++)
1201 }; // class Encoding
1203 }; // namespace System.Text