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;
39 public abstract class Encoding : ICloneable
41 // Code page used by this encoding.
42 internal int codePage;
43 internal int windows_code_page;
44 bool is_readonly = true;
46 protected static readonly byte[] empty = new byte[0];
58 Encoding (int codePage)
60 this.codePage = windows_code_page = codePage;
64 // MS has "InternalBestFit{Decoder|Encoder}Fallback
65 // here, but we dunno what they are for.
66 decoder_fallback = DecoderFallback.ReplacementFallback;
67 encoder_fallback = EncoderFallback.ReplacementFallback;
70 case 54936: // GB18030
71 decoder_fallback = DecoderFallback.ReplacementFallback;
72 encoder_fallback = EncoderFallback.ReplacementFallback;
80 decoder_fallback = DecoderFallback.StandardSafeFallback;
81 encoder_fallback = EncoderFallback.StandardSafeFallback;
86 // until we change the callers:
87 internal static string _ (string arg) {
91 DecoderFallback decoder_fallback;
92 EncoderFallback encoder_fallback;
95 public bool IsReadOnly {
96 get { return is_readonly; }
100 public virtual bool IsSingleByte {
101 get { return false; }
105 public DecoderFallback DecoderFallback {
106 get { return decoder_fallback; }
109 throw new InvalidOperationException ("This Encoding is readonly.");
111 throw new ArgumentNullException ();
112 decoder_fallback = value;
117 public EncoderFallback EncoderFallback {
118 get { return encoder_fallback; }
121 throw new InvalidOperationException ("This Encoding is readonly.");
123 throw new ArgumentNullException ();
124 encoder_fallback = value;
128 internal void SetFallbackInternal (EncoderFallback e, DecoderFallback d)
131 encoder_fallback = e;
133 decoder_fallback = d;
136 // Convert between two encodings.
137 public static byte[] Convert (Encoding srcEncoding, Encoding dstEncoding,
140 if (srcEncoding == null) {
141 throw new ArgumentNullException ("srcEncoding");
143 if (dstEncoding == null) {
144 throw new ArgumentNullException ("dstEncoding");
147 throw new ArgumentNullException ("bytes");
149 return dstEncoding.GetBytes (srcEncoding.GetChars (bytes, 0, bytes.Length));
151 public static byte[] Convert (Encoding srcEncoding, Encoding dstEncoding,
152 byte[] bytes, int index, int count)
154 if (srcEncoding == null) {
155 throw new ArgumentNullException ("srcEncoding");
157 if (dstEncoding == null) {
158 throw new ArgumentNullException ("dstEncoding");
161 throw new ArgumentNullException ("bytes");
163 if (index < 0 || index > bytes.Length) {
164 throw new ArgumentOutOfRangeException
165 ("index", _("ArgRange_Array"));
167 if (count < 0 || (bytes.Length - index) < count) {
168 throw new ArgumentOutOfRangeException
169 ("count", _("ArgRange_Array"));
171 return dstEncoding.GetBytes (srcEncoding.GetChars (bytes, index, count));
174 // Determine if two Encoding objects are equal.
175 public override bool Equals (Object value)
177 Encoding enc = (value as Encoding);
179 return codePage == enc.codePage &&
180 DecoderFallback.Equals (enc.DecoderFallback) &&
181 EncoderFallback.Equals (enc.EncoderFallback);
187 // Get the number of characters needed to encode a character buffer.
188 public abstract int GetByteCount (char[] chars, int index, int count);
190 // Convenience wrappers for "GetByteCount".
191 public virtual int GetByteCount (String s)
194 throw new ArgumentNullException ("s");
199 fixed (char* cptr = s) {
200 return GetByteCount (cptr, s.Length);
204 public virtual int GetByteCount (char[] chars)
207 return GetByteCount (chars, 0, chars.Length);
209 throw new ArgumentNullException ("chars");
213 // Get the bytes that result from encoding a character buffer.
214 public abstract int GetBytes (char[] chars, int charIndex, int charCount,
215 byte[] bytes, int byteIndex);
217 // Convenience wrappers for "GetBytes".
218 public virtual int GetBytes (String s, int charIndex, int charCount,
219 byte[] bytes, int byteIndex)
222 throw new ArgumentNullException ("s");
223 if (charIndex < 0 || charIndex > s.Length)
224 throw new ArgumentOutOfRangeException ("charIndex", _("ArgRange_Array"));
225 if (charCount < 0 || charIndex > (s.Length - charCount))
226 throw new ArgumentOutOfRangeException ("charCount", _("ArgRange_Array"));
227 if (byteIndex < 0 || byteIndex > bytes.Length)
228 throw new ArgumentOutOfRangeException ("byteIndex", _("ArgRange_Array"));
230 if (charCount == 0 || bytes.Length == byteIndex)
233 fixed (char* cptr = s) {
234 fixed (byte* bptr = bytes) {
235 return GetBytes (cptr + charIndex,
238 bytes.Length - byteIndex);
243 public virtual byte[] GetBytes (String s)
246 throw new ArgumentNullException ("s");
250 int byteCount = GetByteCount (s);
254 fixed (char* cptr = s) {
255 byte [] bytes = new byte [byteCount];
256 fixed (byte* bptr = bytes) {
257 GetBytes (cptr, s.Length,
265 public virtual byte[] GetBytes (char[] chars, int index, int count)
267 int numBytes = GetByteCount (chars, index, count);
268 byte[] bytes = new byte [numBytes];
269 GetBytes (chars, index, count, bytes, 0);
272 public virtual byte[] GetBytes (char[] chars)
274 int numBytes = GetByteCount (chars, 0, chars.Length);
275 byte[] bytes = new byte [numBytes];
276 GetBytes (chars, 0, chars.Length, bytes, 0);
280 // Get the number of characters needed to decode a byte buffer.
281 public abstract int GetCharCount (byte[] bytes, int index, int count);
283 // Convenience wrappers for "GetCharCount".
284 public virtual int GetCharCount (byte[] bytes)
287 throw new ArgumentNullException ("bytes");
289 return GetCharCount (bytes, 0, bytes.Length);
292 // Get the characters that result from decoding a byte buffer.
293 public abstract int GetChars (byte[] bytes, int byteIndex, int byteCount,
294 char[] chars, int charIndex);
296 // Convenience wrappers for "GetChars".
297 public virtual char[] GetChars (byte[] bytes, int index, int count)
299 int numChars = GetCharCount (bytes, index, count);
300 char[] chars = new char [numChars];
301 GetChars (bytes, index, count, chars, 0);
304 public virtual char[] GetChars (byte[] bytes)
307 throw new ArgumentNullException ("bytes");
309 int numChars = GetCharCount (bytes, 0, bytes.Length);
310 char[] chars = new char [numChars];
311 GetChars (bytes, 0, bytes.Length, chars, 0);
315 // Get a decoder that forwards requests to this object.
316 public virtual Decoder GetDecoder ()
318 return new ForwardingDecoder (this);
321 // Get an encoder that forwards requests to this object.
322 public virtual Encoder GetEncoder ()
324 return new ForwardingEncoder (this);
327 // Loaded copy of the "I18N" assembly. We need to move
328 // this into a class in "System.Private" eventually.
329 private static Assembly i18nAssembly;
330 private static bool i18nDisabled;
332 // Invoke a specific method on the "I18N" manager object.
333 // Returns NULL if the method failed.
334 private static Object InvokeI18N (String name, params Object[] args)
337 // Bail out if we previously detected that there
338 // is insufficent engine support for I18N handling.
343 // Find or load the "I18N" assembly.
344 if (i18nAssembly == null) {
347 i18nAssembly = Assembly.Load (Consts.AssemblyI18N);
348 } catch (NotImplementedException) {
349 // Assembly loading unsupported by the engine.
353 if (i18nAssembly == null) {
356 } catch (SystemException) {
361 // Find the "I18N.Common.Manager" class.
364 managerClass = i18nAssembly.GetType ("I18N.Common.Manager");
365 } catch (NotImplementedException) {
366 // "GetType" is not supported by the engine.
370 if (managerClass == null) {
374 // Get the value of the "PrimaryManager" property.
377 manager = managerClass.InvokeMember
379 BindingFlags.GetProperty |
380 BindingFlags.Static |
382 null, null, null, null, null, null);
383 if (manager == null) {
386 } catch (MissingMethodException) {
388 } catch (SecurityException) {
390 } catch (NotImplementedException) {
391 // "InvokeMember" is not supported by the engine.
396 // Invoke the requested method on the manager.
398 return managerClass.InvokeMember
400 BindingFlags.InvokeMethod |
401 BindingFlags.Instance |
403 null, manager, args, null, null, null);
404 } catch (MissingMethodException) {
406 } catch (SecurityException) {
412 // Get an encoder for a specific code page.
418 static Encoding GetEncoding (int codepage)
420 if (codepage < 0 || codepage > 0xffff)
421 throw new ArgumentOutOfRangeException ("codepage",
422 "Valid values are between 0 and 65535, inclusive.");
424 // Check for the builtin code pages first.
426 case 0: return Default;
428 case ASCIIEncoding.ASCII_CODE_PAGE:
431 case UTF7Encoding.UTF7_CODE_PAGE:
434 case UTF8Encoding.UTF8_CODE_PAGE:
437 case UTF32Encoding.UTF32_CODE_PAGE:
440 case UTF32Encoding.BIG_UTF32_CODE_PAGE:
441 return BigEndianUTF32;
443 case UnicodeEncoding.UNICODE_CODE_PAGE:
446 case UnicodeEncoding.BIG_UNICODE_CODE_PAGE:
447 return BigEndianUnicode;
450 case Latin1Encoding.ISOLATIN_CODE_PAGE:
456 // Try to obtain a code page handler from the I18N handler.
457 Encoding enc = (Encoding)(InvokeI18N ("GetEncoding", codepage));
459 enc.is_readonly = true;
463 // Build a code page class name.
464 String cpName = "System.Text.CP" + codepage.ToString ();
466 // Look for a code page converter in this assembly.
467 Assembly assembly = Assembly.GetExecutingAssembly ();
468 Type type = assembly.GetType (cpName);
470 enc = (Encoding)(Activator.CreateInstance (type));
471 enc.is_readonly = true;
475 // Look in any assembly, in case the application
476 // has provided its own code page handler.
477 type = Type.GetType (cpName);
479 enc = (Encoding)(Activator.CreateInstance (type));
480 enc.is_readonly = true;
484 // We have no idea how to handle this code page.
485 throw new NotSupportedException
486 (String.Format ("CodePage {0} not supported", codepage.ToString ()));
492 public virtual object Clone ()
494 Encoding e = (Encoding) MemberwiseClone ();
495 e.is_readonly = false;
501 public static Encoding GetEncoding (int codepage,
502 EncoderFallback encoderFallback, DecoderFallback decoderFallback)
504 if (encoderFallback == null)
505 throw new ArgumentNullException ("encoderFallback");
506 if (decoderFallback == null)
507 throw new ArgumentNullException ("decoderFallback");
509 Encoding e = GetEncoding (codepage).Clone () as Encoding;
510 e.is_readonly = false;
511 e.encoder_fallback = encoderFallback;
512 e.decoder_fallback = decoderFallback;
516 public static Encoding GetEncoding (string name,
517 EncoderFallback encoderFallback, DecoderFallback decoderFallback)
519 if (encoderFallback == null)
520 throw new ArgumentNullException ("encoderFallback");
521 if (decoderFallback == null)
522 throw new ArgumentNullException ("decoderFallback");
524 Encoding e = GetEncoding (name).Clone () as Encoding;
525 e.is_readonly = false;
526 e.encoder_fallback = encoderFallback;
527 e.decoder_fallback = decoderFallback;
533 static EncodingInfo [] encoding_infos;
535 // FIXME: As everyone would agree, this implementation is so *hacky*
536 // and could be very easily broken. But since there is a test for
537 // this method to make sure that this method always returns
538 // the same number and content of encoding infos, this won't
539 // matter practically.
540 public static EncodingInfo[] GetEncodings ()
542 if (encoding_infos == null) {
543 int [] codepages = new int [] {
545 850, 852, 855, 857, 858, 860, 861, 862, 863,
546 864, 865, 866, 869, 870, 874, 875,
548 1026, 1047, 1140, 1141, 1142, 1143, 1144,
549 1145, 1146, 1147, 1148, 1149,
550 1200, 1201, 1250, 1251, 1252, 1253, 1254,
551 1255, 1256, 1257, 1258,
552 10000, 10079, 12000, 12001,
553 20127, 20273, 20277, 20278, 20280, 20284,
554 20285, 20290, 20297, 20420, 20424, 20866,
555 20871, 21025, 21866, 28591, 28592, 28593,
556 28594, 28595, 28596, 28597, 28598, 28599,
558 50220, 50221, 50222, 51932, 51949, 54936,
559 57002, 57003, 57004, 57005, 57006, 57007,
560 57008, 57009, 57010, 57011,
563 encoding_infos = new EncodingInfo [codepages.Length];
564 for (int i = 0; i < codepages.Length; i++)
565 encoding_infos [i] = new EncodingInfo (codepages [i]);
567 return encoding_infos;
572 public bool IsAlwaysNormalized ()
574 return IsAlwaysNormalized (NormalizationForm.FormC);
578 public virtual bool IsAlwaysNormalized (NormalizationForm form)
580 // umm, ASCIIEncoding should have overriden this method, no?
581 return form == NormalizationForm.FormC && this is ASCIIEncoding;
585 // Get an encoding object for a specific web encoding name.
586 public static Encoding GetEncoding (string name)
588 // Validate the parameters.
590 throw new ArgumentNullException ("name");
593 string converted = name.ToLowerInvariant ().Replace ('-', '_');
595 // Builtin web encoding names and the corresponding code pages.
600 case "ansi_x3.4_1968":
601 case "ansi_x3.4_1986":
607 case "iso_646.irv:1991":
608 return GetEncoding (ASCIIEncoding.ASCII_CODE_PAGE);
611 case "csunicode11utf7":
612 case "unicode_1_1_utf_7":
613 case "unicode_2_0_utf_7":
614 case "x_unicode_1_1_utf_7":
615 case "x_unicode_2_0_utf_7":
616 return GetEncoding (UTF7Encoding.UTF7_CODE_PAGE);
619 case "unicode_1_1_utf_8":
620 case "unicode_2_0_utf_8":
621 case "x_unicode_1_1_utf_8":
622 case "x_unicode_2_0_utf_8":
623 return GetEncoding (UTF8Encoding.UTF8_CODE_PAGE);
629 case "iso_10646_ucs2":
630 return GetEncoding (UnicodeEncoding.UNICODE_CODE_PAGE);
634 return GetEncoding (UnicodeEncoding.BIG_UNICODE_CODE_PAGE);
639 return GetEncoding (UTF32Encoding.UTF32_CODE_PAGE);
642 return GetEncoding (UTF32Encoding.BIG_UTF32_CODE_PAGE);
647 return GetEncoding (Latin1Encoding.ISOLATIN_CODE_PAGE);
652 // Try to obtain a web encoding handler from the I18N handler.
653 Encoding enc = (Encoding)(InvokeI18N ("GetEncoding", name));
658 // Build a web encoding class name.
659 String encName = "System.Text.ENC" + converted;
662 // Look for a code page converter in this assembly.
663 Assembly assembly = Assembly.GetExecutingAssembly ();
664 Type type = assembly.GetType (encName);
666 return (Encoding)(Activator.CreateInstance (type));
669 // Look in any assembly, in case the application
670 // has provided its own code page handler.
671 type = Type.GetType (encName);
673 return (Encoding)(Activator.CreateInstance (type));
676 // We have no idea how to handle this encoding name.
677 throw new ArgumentException (String.Format ("Encoding name '{0}' not "
678 + "supported", name), "name");
681 #endif // !ECMA_COMPAT
683 // Get a hash code for this instance.
684 public override int GetHashCode ()
686 return DecoderFallback.GetHashCode () << 24 + EncoderFallback.GetHashCode () << 16 + codePage;
689 // Get the maximum number of bytes needed to encode a
690 // specified number of characters.
691 public abstract int GetMaxByteCount (int charCount);
693 // Get the maximum number of characters needed to decode a
694 // specified number of bytes.
695 public abstract int GetMaxCharCount (int byteCount);
697 // Get the identifying preamble for this encoding.
698 public virtual byte[] GetPreamble ()
703 // Decode a buffer of bytes into a string.
704 public virtual String GetString (byte[] bytes, int index, int count)
706 return new String (GetChars(bytes, index, count));
708 public virtual String GetString (byte[] bytes)
711 throw new ArgumentNullException ("bytes");
713 return GetString (bytes, 0, bytes.Length);
718 internal bool is_mail_news_display;
719 internal bool is_mail_news_save;
720 internal bool is_browser_save = false;
721 internal bool is_browser_display = false;
722 internal string body_name;
723 internal string encoding_name;
724 internal string header_name;
725 internal string web_name;
727 // Get the mail body name for this encoding.
728 public virtual String BodyName
735 // Get the code page represented by this object.
736 public virtual int CodePage
743 // Get the human-readable name for this encoding.
744 public virtual String EncodingName
747 return encoding_name;
751 // Get the mail agent header name for this encoding.
752 public virtual String HeaderName
759 // Determine if this encoding can be displayed in a Web browser.
760 public virtual bool IsBrowserDisplay
763 return is_browser_display;
767 // Determine if this encoding can be saved from a Web browser.
768 public virtual bool IsBrowserSave
771 return is_browser_save;
775 // Determine if this encoding can be displayed in a mail/news agent.
776 public virtual bool IsMailNewsDisplay
779 return is_mail_news_display;
783 // Determine if this encoding can be saved from a mail/news agent.
784 public virtual bool IsMailNewsSave
787 return is_mail_news_save;
791 // Get the IANA-preferred Web name for this encoding.
792 public virtual String WebName
799 // Get the Windows code page represented by this object.
800 public virtual int WindowsCodePage
803 // We make no distinction between normal and
804 // Windows code pages in this implementation.
805 return windows_code_page;
809 #endif // !ECMA_COMPAT
811 // Storage for standard encoding objects.
812 static volatile Encoding asciiEncoding;
813 static volatile Encoding bigEndianEncoding;
814 static volatile Encoding defaultEncoding;
815 static volatile Encoding utf7Encoding;
816 static volatile Encoding utf8EncodingWithMarkers;
817 static volatile Encoding utf8EncodingWithoutMarkers;
818 static volatile Encoding unicodeEncoding;
819 static volatile Encoding isoLatin1Encoding;
820 static volatile Encoding utf8EncodingUnsafe;
821 static volatile Encoding utf32Encoding;
822 static volatile Encoding bigEndianUTF32Encoding;
824 static readonly object lockobj = new object ();
826 // Get the standard ASCII encoding object.
827 public static Encoding ASCII
830 if (asciiEncoding == null) {
832 if (asciiEncoding == null) {
833 asciiEncoding = new ASCIIEncoding ();
834 // asciiEncoding.is_readonly = true;
839 return asciiEncoding;
843 // Get the standard big-endian Unicode encoding object.
844 public static Encoding BigEndianUnicode
847 if (bigEndianEncoding == null) {
849 if (bigEndianEncoding == null) {
850 bigEndianEncoding = new UnicodeEncoding (true, true);
851 // bigEndianEncoding.is_readonly = true;
856 return bigEndianEncoding;
860 [MethodImpl (MethodImplOptions.InternalCall)]
861 extern internal static string InternalCodePage (ref int code_page);
863 // Get the default encoding object.
864 public static Encoding Default
867 if (defaultEncoding == null) {
869 if (defaultEncoding == null) {
870 // See if the underlying system knows what
871 // code page handler we should be using.
874 string code_page_name = InternalCodePage (ref code_page);
877 defaultEncoding = GetEncoding (code_page_name);
879 // map the codepage from internal to our numbers
880 code_page = code_page & 0x0fffffff;
882 case 1: code_page = ASCIIEncoding.ASCII_CODE_PAGE; break;
883 case 2: code_page = UTF7Encoding.UTF7_CODE_PAGE; break;
884 case 3: code_page = UTF8Encoding.UTF8_CODE_PAGE; break;
885 case 4: code_page = UnicodeEncoding.UNICODE_CODE_PAGE; break;
886 case 5: code_page = UnicodeEncoding.BIG_UNICODE_CODE_PAGE; break;
888 case 6: code_page = Latin1Encoding.ISOLATIN_CODE_PAGE; break;
891 defaultEncoding = GetEncoding (code_page);
893 } catch (NotSupportedException) {
895 defaultEncoding = UTF8;
897 // code_page is not supported on underlying platform
898 defaultEncoding = UTF8Unmarked;
900 } catch (ArgumentException) {
901 // code_page_name is not a valid code page, or is
902 // not supported by underlying OS
904 defaultEncoding = UTF8;
906 defaultEncoding = UTF8Unmarked;
909 defaultEncoding.is_readonly = true;
914 return defaultEncoding;
920 // Get the ISO Latin1 encoding object.
921 private static Encoding ISOLatin1
924 if (isoLatin1Encoding == null) {
926 if (isoLatin1Encoding == null) {
927 isoLatin1Encoding = new Latin1Encoding ();
928 // isoLatin1Encoding.is_readonly = true;
933 return isoLatin1Encoding;
939 // Get the standard UTF-7 encoding object.
948 if (utf7Encoding == null) {
950 if (utf7Encoding == null) {
951 utf7Encoding = new UTF7Encoding ();
952 // utf7Encoding.is_readonly = true;
961 // Get the standard UTF-8 encoding object.
962 public static Encoding UTF8
965 if (utf8EncodingWithMarkers == null) {
967 if (utf8EncodingWithMarkers == null) {
968 utf8EncodingWithMarkers = new UTF8Encoding (true);
969 // utf8EncodingWithMarkers.is_readonly = true;
974 return utf8EncodingWithMarkers;
979 // Only internal, to be used by the class libraries: Unmarked and non-input-validating
981 internal static Encoding UTF8Unmarked {
983 if (utf8EncodingWithoutMarkers == null) {
985 if (utf8EncodingWithoutMarkers == null){
986 utf8EncodingWithoutMarkers = new UTF8Encoding (false, false);
987 // utf8EncodingWithoutMarkers.is_readonly = true;
992 return utf8EncodingWithoutMarkers;
997 // Only internal, to be used by the class libraries: Unmarked and non-input-validating
999 internal static Encoding UTF8UnmarkedUnsafe {
1001 if (utf8EncodingUnsafe == null) {
1003 if (utf8EncodingUnsafe == null){
1004 utf8EncodingUnsafe = new UTF8Encoding (false, false);
1005 utf8EncodingUnsafe.is_readonly = false;
1006 utf8EncodingUnsafe.DecoderFallback = new DecoderReplacementFallback (String.Empty);
1007 utf8EncodingUnsafe.is_readonly = true;
1012 return utf8EncodingUnsafe;
1016 // Get the standard little-endian Unicode encoding object.
1017 public static Encoding Unicode
1020 if (unicodeEncoding == null) {
1022 if (unicodeEncoding == null) {
1023 unicodeEncoding = new UnicodeEncoding (false, true);
1024 // unicodeEncoding.is_readonly = true;
1029 return unicodeEncoding;
1033 // Get the standard little-endian UTF-32 encoding object.
1034 public static Encoding UTF32
1037 if (utf32Encoding == null) {
1039 if (utf32Encoding == null) {
1040 utf32Encoding = new UTF32Encoding (false, true);
1041 // utf32Encoding.is_readonly = true;
1046 return utf32Encoding;
1050 // Get the standard big-endian UTF-32 encoding object.
1051 internal static Encoding BigEndianUTF32
1054 if (bigEndianUTF32Encoding == null) {
1056 if (bigEndianUTF32Encoding == null) {
1057 bigEndianUTF32Encoding = new UTF32Encoding (true, true);
1058 // bigEndianUTF32Encoding.is_readonly = true;
1063 return bigEndianUTF32Encoding;
1067 // Forwarding decoder implementation.
1068 private sealed class ForwardingDecoder : Decoder
1070 private Encoding encoding;
1073 public ForwardingDecoder (Encoding enc)
1076 DecoderFallback fallback = encoding.DecoderFallback;
1077 if (fallback != null)
1078 Fallback = fallback;
1081 // Override inherited methods.
1082 public override int GetCharCount (byte[] bytes, int index, int count)
1084 return encoding.GetCharCount (bytes, index, count);
1086 public override int GetChars (byte[] bytes, int byteIndex,
1087 int byteCount, char[] chars,
1090 return encoding.GetChars (bytes, byteIndex, byteCount, chars, charIndex);
1093 } // class ForwardingDecoder
1095 // Forwarding encoder implementation.
1096 private sealed class ForwardingEncoder : Encoder
1098 private Encoding encoding;
1101 public ForwardingEncoder (Encoding enc)
1104 EncoderFallback fallback = encoding.EncoderFallback;
1105 if (fallback != null)
1106 Fallback = fallback;
1109 // Override inherited methods.
1110 public override int GetByteCount (char[] chars, int index, int count, bool flush)
1112 return encoding.GetByteCount (chars, index, count);
1114 public override int GetBytes (char[] chars, int charIndex,
1115 int charCount, byte[] bytes,
1116 int byteCount, bool flush)
1118 return encoding.GetBytes (chars, charIndex, charCount, bytes, byteCount);
1121 } // class ForwardingEncoder
1123 [CLSCompliantAttribute(false)]
1124 [ComVisible (false)]
1125 public unsafe virtual int GetByteCount (char *chars, int count)
1128 throw new ArgumentNullException ("chars");
1130 throw new ArgumentOutOfRangeException ("count");
1131 char [] c = new char [count];
1133 for (int p = 0; p < count; p++)
1136 return GetByteCount (c);
1139 [CLSCompliantAttribute(false)]
1140 [ComVisible (false)]
1141 public unsafe virtual int GetCharCount (byte *bytes, int count)
1144 throw new ArgumentNullException ("bytes");
1146 throw new ArgumentOutOfRangeException ("count");
1148 byte [] ba = new byte [count];
1149 for (int i = 0; i < count; i++)
1151 return GetCharCount (ba, 0, count);
1154 [CLSCompliantAttribute(false)]
1155 [ComVisible (false)]
1156 public unsafe virtual int GetChars (byte *bytes, int byteCount, char *chars, int charCount)
1159 throw new ArgumentNullException ("bytes");
1161 throw new ArgumentNullException ("chars");
1163 throw new ArgumentOutOfRangeException ("charCount");
1165 throw new ArgumentOutOfRangeException ("byteCount");
1167 byte [] ba = new byte [byteCount];
1168 for (int i = 0; i < byteCount; i++)
1170 char [] ret = GetChars (ba, 0, byteCount);
1171 int top = ret.Length;
1173 if (top > charCount)
1174 throw new ArgumentException ("charCount is less than the number of characters produced", "charCount");
1176 for (int i = 0; i < top; i++)
1177 chars [i] = ret [i];
1181 [CLSCompliantAttribute(false)]
1182 [ComVisible (false)]
1183 public unsafe virtual int GetBytes (char *chars, int charCount, byte *bytes, int byteCount)
1186 throw new ArgumentNullException ("bytes");
1188 throw new ArgumentNullException ("chars");
1190 throw new ArgumentOutOfRangeException ("charCount");
1192 throw new ArgumentOutOfRangeException ("byteCount");
1194 char [] c = new char [charCount];
1196 for (int i = 0; i < charCount; i++)
1199 byte [] b = GetBytes (c, 0, charCount);
1201 if (top > byteCount)
1202 throw new ArgumentException ("byteCount is less that the number of bytes produced", "byteCount");
1204 for (int i = 0; i < top; i++)
1209 }; // class Encoding
1211 }; // namespace System.Text