mscorlib(4.0.0.0) API with additions
Encoding.cs
1 using Microsoft.Win32;
2 using System.Collections;
6 using System.Security;
7 using System.Threading;
8 
9 namespace System.Text
10 {
12  [Serializable]
13  [ComVisible(true)]
14  [__DynamicallyInvokable]
15  public abstract class Encoding : ICloneable
16  {
17  [Serializable]
18  internal class DefaultEncoder : Encoder, ISerializable, IObjectReference
19  {
20  private Encoding m_encoding;
21 
22  [NonSerialized]
23  private bool m_hasInitializedEncoding;
24 
25  [NonSerialized]
26  internal char charLeftOver;
27 
28  public DefaultEncoder(Encoding encoding)
29  {
30  m_encoding = encoding;
31  m_hasInitializedEncoding = true;
32  }
33 
34  internal DefaultEncoder(SerializationInfo info, StreamingContext context)
35  {
36  if (info == null)
37  {
38  throw new ArgumentNullException("info");
39  }
40  m_encoding = (Encoding)info.GetValue("encoding", typeof(Encoding));
41  try
42  {
43  m_fallback = (EncoderFallback)info.GetValue("m_fallback", typeof(EncoderFallback));
44  charLeftOver = (char)info.GetValue("charLeftOver", typeof(char));
45  }
47  {
48  }
49  }
50 
51  [SecurityCritical]
52  public object GetRealObject(StreamingContext context)
53  {
54  if (m_hasInitializedEncoding)
55  {
56  return this;
57  }
58  Encoder encoder = m_encoding.GetEncoder();
59  if (m_fallback != null)
60  {
61  encoder.m_fallback = m_fallback;
62  }
63  if (charLeftOver != 0)
64  {
65  EncoderNLS encoderNLS = encoder as EncoderNLS;
66  if (encoderNLS != null)
67  {
68  encoderNLS.charLeftOver = charLeftOver;
69  }
70  }
71  return encoder;
72  }
73 
74  [SecurityCritical]
76  {
77  if (info == null)
78  {
79  throw new ArgumentNullException("info");
80  }
81  info.AddValue("encoding", m_encoding);
82  }
83 
84  public override int GetByteCount(char[] chars, int index, int count, bool flush)
85  {
86  return m_encoding.GetByteCount(chars, index, count);
87  }
88 
89  [SecurityCritical]
90  public unsafe override int GetByteCount(char* chars, int count, bool flush)
91  {
92  return m_encoding.GetByteCount(chars, count);
93  }
94 
95  public override int GetBytes(char[] chars, int charIndex, int charCount, byte[] bytes, int byteIndex, bool flush)
96  {
97  return m_encoding.GetBytes(chars, charIndex, charCount, bytes, byteIndex);
98  }
99 
100  [SecurityCritical]
101  public unsafe override int GetBytes(char* chars, int charCount, byte* bytes, int byteCount, bool flush)
102  {
103  return m_encoding.GetBytes(chars, charCount, bytes, byteCount);
104  }
105  }
106 
107  [Serializable]
108  internal class DefaultDecoder : Decoder, ISerializable, IObjectReference
109  {
110  private Encoding m_encoding;
111 
112  [NonSerialized]
113  private bool m_hasInitializedEncoding;
114 
115  public DefaultDecoder(Encoding encoding)
116  {
117  m_encoding = encoding;
118  m_hasInitializedEncoding = true;
119  }
120 
121  internal DefaultDecoder(SerializationInfo info, StreamingContext context)
122  {
123  if (info == null)
124  {
125  throw new ArgumentNullException("info");
126  }
127  m_encoding = (Encoding)info.GetValue("encoding", typeof(Encoding));
128  try
129  {
130  m_fallback = (DecoderFallback)info.GetValue("m_fallback", typeof(DecoderFallback));
131  }
132  catch (SerializationException)
133  {
134  m_fallback = null;
135  }
136  }
137 
138  [SecurityCritical]
139  public object GetRealObject(StreamingContext context)
140  {
141  if (m_hasInitializedEncoding)
142  {
143  return this;
144  }
145  Decoder decoder = m_encoding.GetDecoder();
146  if (m_fallback != null)
147  {
148  decoder.m_fallback = m_fallback;
149  }
150  return decoder;
151  }
152 
153  [SecurityCritical]
155  {
156  if (info == null)
157  {
158  throw new ArgumentNullException("info");
159  }
160  info.AddValue("encoding", m_encoding);
161  }
162 
163  public override int GetCharCount(byte[] bytes, int index, int count)
164  {
165  return GetCharCount(bytes, index, count, flush: false);
166  }
167 
168  public override int GetCharCount(byte[] bytes, int index, int count, bool flush)
169  {
170  return m_encoding.GetCharCount(bytes, index, count);
171  }
172 
173  [SecurityCritical]
174  public unsafe override int GetCharCount(byte* bytes, int count, bool flush)
175  {
176  return m_encoding.GetCharCount(bytes, count);
177  }
178 
179  public override int GetChars(byte[] bytes, int byteIndex, int byteCount, char[] chars, int charIndex)
180  {
181  return GetChars(bytes, byteIndex, byteCount, chars, charIndex, flush: false);
182  }
183 
184  public override int GetChars(byte[] bytes, int byteIndex, int byteCount, char[] chars, int charIndex, bool flush)
185  {
186  return m_encoding.GetChars(bytes, byteIndex, byteCount, chars, charIndex);
187  }
188 
189  [SecurityCritical]
190  public unsafe override int GetChars(byte* bytes, int byteCount, char* chars, int charCount, bool flush)
191  {
192  return m_encoding.GetChars(bytes, byteCount, chars, charCount);
193  }
194  }
195 
196  internal class EncodingCharBuffer
197  {
198  [SecurityCritical]
199  private unsafe char* chars;
200 
201  [SecurityCritical]
202  private unsafe char* charStart;
203 
204  [SecurityCritical]
205  private unsafe char* charEnd;
206 
207  private int charCountResult;
208 
209  private Encoding enc;
210 
211  private DecoderNLS decoder;
212 
213  [SecurityCritical]
214  private unsafe byte* byteStart;
215 
216  [SecurityCritical]
217  private unsafe byte* byteEnd;
218 
219  [SecurityCritical]
220  private unsafe byte* bytes;
221 
222  private DecoderFallbackBuffer fallbackBuffer;
223 
224  internal unsafe bool MoreData
225  {
226  [SecurityCritical]
227  get
228  {
229  return bytes < byteEnd;
230  }
231  }
232 
233  internal unsafe int BytesUsed
234  {
235  [SecurityCritical]
236  get
237  {
238  return (int)(bytes - byteStart);
239  }
240  }
241 
242  internal int Count => charCountResult;
243 
244  [SecurityCritical]
245  internal unsafe EncodingCharBuffer(Encoding enc, DecoderNLS decoder, char* charStart, int charCount, byte* byteStart, int byteCount)
246  {
247  this.enc = enc;
248  this.decoder = decoder;
249  chars = charStart;
250  this.charStart = charStart;
251  charEnd = charStart + charCount;
252  this.byteStart = byteStart;
253  bytes = byteStart;
254  byteEnd = byteStart + byteCount;
255  if (this.decoder == null)
256  {
257  fallbackBuffer = enc.DecoderFallback.CreateFallbackBuffer();
258  }
259  else
260  {
261  fallbackBuffer = this.decoder.FallbackBuffer;
262  }
263  fallbackBuffer.InternalInitialize(bytes, charEnd);
264  }
265 
266  [SecurityCritical]
267  internal unsafe bool AddChar(char ch, int numBytes)
268  {
269  if (chars != null)
270  {
271  if (chars >= charEnd)
272  {
273  bytes -= numBytes;
274  enc.ThrowCharsOverflow(decoder, bytes <= byteStart);
275  return false;
276  }
277  char* ptr = chars;
278  chars = ptr + 1;
279  *ptr = ch;
280  }
281  charCountResult++;
282  return true;
283  }
284 
285  [SecurityCritical]
286  internal bool AddChar(char ch)
287  {
288  return AddChar(ch, 1);
289  }
290 
291  [SecurityCritical]
292  internal unsafe bool AddChar(char ch1, char ch2, int numBytes)
293  {
294  if (chars >= charEnd - 1)
295  {
296  bytes -= numBytes;
297  enc.ThrowCharsOverflow(decoder, bytes <= byteStart);
298  return false;
299  }
300  if (AddChar(ch1, numBytes))
301  {
302  return AddChar(ch2, numBytes);
303  }
304  return false;
305  }
306 
307  [SecurityCritical]
308  internal unsafe void AdjustBytes(int count)
309  {
310  bytes += count;
311  }
312 
313  [SecurityCritical]
314  internal unsafe bool EvenMoreData(int count)
315  {
316  return bytes <= byteEnd - count;
317  }
318 
319  [SecurityCritical]
320  internal unsafe byte GetNextByte()
321  {
322  if (bytes >= byteEnd)
323  {
324  return 0;
325  }
326  return *(bytes++);
327  }
328 
329  [SecurityCritical]
330  internal bool Fallback(byte fallbackByte)
331  {
332  byte[] byteBuffer = new byte[1]
333  {
334  fallbackByte
335  };
336  return Fallback(byteBuffer);
337  }
338 
339  [SecurityCritical]
340  internal bool Fallback(byte byte1, byte byte2)
341  {
342  byte[] byteBuffer = new byte[2]
343  {
344  byte1,
345  byte2
346  };
347  return Fallback(byteBuffer);
348  }
349 
350  [SecurityCritical]
351  internal bool Fallback(byte byte1, byte byte2, byte byte3, byte byte4)
352  {
353  byte[] byteBuffer = new byte[4]
354  {
355  byte1,
356  byte2,
357  byte3,
358  byte4
359  };
360  return Fallback(byteBuffer);
361  }
362 
363  [SecurityCritical]
364  internal unsafe bool Fallback(byte[] byteBuffer)
365  {
366  if (chars != null)
367  {
368  char* ptr = chars;
369  if (!fallbackBuffer.InternalFallback(byteBuffer, bytes, ref chars))
370  {
371  bytes -= byteBuffer.Length;
372  fallbackBuffer.InternalReset();
373  enc.ThrowCharsOverflow(decoder, chars == charStart);
374  return false;
375  }
376  charCountResult += (int)(chars - ptr);
377  }
378  else
379  {
380  charCountResult += fallbackBuffer.InternalFallback(byteBuffer, bytes);
381  }
382  return true;
383  }
384  }
385 
386  internal class EncodingByteBuffer
387  {
388  [SecurityCritical]
389  private unsafe byte* bytes;
390 
391  [SecurityCritical]
392  private unsafe byte* byteStart;
393 
394  [SecurityCritical]
395  private unsafe byte* byteEnd;
396 
397  [SecurityCritical]
398  private unsafe char* chars;
399 
400  [SecurityCritical]
401  private unsafe char* charStart;
402 
403  [SecurityCritical]
404  private unsafe char* charEnd;
405 
406  private int byteCountResult;
407 
408  private Encoding enc;
409 
410  private EncoderNLS encoder;
411 
412  internal EncoderFallbackBuffer fallbackBuffer;
413 
414  internal unsafe bool MoreData
415  {
416  [SecurityCritical]
417  get
418  {
419  if (fallbackBuffer.Remaining <= 0)
420  {
421  return chars < charEnd;
422  }
423  return true;
424  }
425  }
426 
427  internal unsafe int CharsUsed
428  {
429  [SecurityCritical]
430  get
431  {
432  return (int)(chars - charStart);
433  }
434  }
435 
436  internal int Count => byteCountResult;
437 
438  [SecurityCritical]
439  internal unsafe EncodingByteBuffer(Encoding inEncoding, EncoderNLS inEncoder, byte* inByteStart, int inByteCount, char* inCharStart, int inCharCount)
440  {
441  enc = inEncoding;
442  encoder = inEncoder;
443  charStart = inCharStart;
444  chars = inCharStart;
445  charEnd = inCharStart + inCharCount;
446  bytes = inByteStart;
447  byteStart = inByteStart;
448  byteEnd = inByteStart + inByteCount;
449  if (encoder == null)
450  {
451  fallbackBuffer = enc.EncoderFallback.CreateFallbackBuffer();
452  }
453  else
454  {
455  fallbackBuffer = encoder.FallbackBuffer;
456  if (encoder.m_throwOnOverflow && encoder.InternalHasFallbackBuffer && fallbackBuffer.Remaining > 0)
457  {
458  throw new ArgumentException(Environment.GetResourceString("Argument_EncoderFallbackNotEmpty", encoder.Encoding.EncodingName, encoder.Fallback.GetType()));
459  }
460  }
461  fallbackBuffer.InternalInitialize(chars, charEnd, encoder, bytes != null);
462  }
463 
464  [SecurityCritical]
465  internal unsafe bool AddByte(byte b, int moreBytesExpected)
466  {
467  if (bytes != null)
468  {
469  if (bytes >= byteEnd - moreBytesExpected)
470  {
471  MovePrevious(bThrow: true);
472  return false;
473  }
474  *(bytes++) = b;
475  }
476  byteCountResult++;
477  return true;
478  }
479 
480  [SecurityCritical]
481  internal bool AddByte(byte b1)
482  {
483  return AddByte(b1, 0);
484  }
485 
486  [SecurityCritical]
487  internal bool AddByte(byte b1, byte b2)
488  {
489  return AddByte(b1, b2, 0);
490  }
491 
492  [SecurityCritical]
493  internal bool AddByte(byte b1, byte b2, int moreBytesExpected)
494  {
495  if (AddByte(b1, 1 + moreBytesExpected))
496  {
497  return AddByte(b2, moreBytesExpected);
498  }
499  return false;
500  }
501 
502  [SecurityCritical]
503  internal bool AddByte(byte b1, byte b2, byte b3)
504  {
505  return AddByte(b1, b2, b3, 0);
506  }
507 
508  [SecurityCritical]
509  internal bool AddByte(byte b1, byte b2, byte b3, int moreBytesExpected)
510  {
511  if (AddByte(b1, 2 + moreBytesExpected) && AddByte(b2, 1 + moreBytesExpected))
512  {
513  return AddByte(b3, moreBytesExpected);
514  }
515  return false;
516  }
517 
518  [SecurityCritical]
519  internal bool AddByte(byte b1, byte b2, byte b3, byte b4)
520  {
521  if (AddByte(b1, 3) && AddByte(b2, 2) && AddByte(b3, 1))
522  {
523  return AddByte(b4, 0);
524  }
525  return false;
526  }
527 
528  [SecurityCritical]
529  internal unsafe void MovePrevious(bool bThrow)
530  {
531  if (fallbackBuffer.bFallingBack)
532  {
533  fallbackBuffer.MovePrevious();
534  }
535  else if (chars > charStart)
536  {
537  chars--;
538  }
539  if (bThrow)
540  {
541  enc.ThrowBytesOverflow(encoder, bytes == byteStart);
542  }
543  }
544 
545  [SecurityCritical]
546  internal unsafe bool Fallback(char charFallback)
547  {
548  return fallbackBuffer.InternalFallback(charFallback, ref chars);
549  }
550 
551  [SecurityCritical]
552  internal unsafe char GetNextChar()
553  {
554  char c = fallbackBuffer.InternalGetNextChar();
555  if (c == '\0' && chars < charEnd)
556  {
557  char* ptr = chars;
558  chars = ptr + 1;
559  c = *ptr;
560  }
561  return c;
562  }
563  }
564 
565  private static volatile Encoding defaultEncoding;
566 
567  private static volatile Encoding unicodeEncoding;
568 
569  private static volatile Encoding bigEndianUnicode;
570 
571  private static volatile Encoding utf7Encoding;
572 
573  private static volatile Encoding utf8Encoding;
574 
575  private static volatile Encoding utf32Encoding;
576 
577  private static volatile Encoding asciiEncoding;
578 
579  private static volatile Encoding latin1Encoding;
580 
581  private static volatile Hashtable encodings;
582 
583  private const int MIMECONTF_MAILNEWS = 1;
584 
585  private const int MIMECONTF_BROWSER = 2;
586 
587  private const int MIMECONTF_SAVABLE_MAILNEWS = 256;
588 
589  private const int MIMECONTF_SAVABLE_BROWSER = 512;
590 
591  private const int CodePageDefault = 0;
592 
593  private const int CodePageNoOEM = 1;
594 
595  private const int CodePageNoMac = 2;
596 
597  private const int CodePageNoThread = 3;
598 
599  private const int CodePageNoSymbol = 42;
600 
601  private const int CodePageUnicode = 1200;
602 
603  private const int CodePageBigEndian = 1201;
604 
605  private const int CodePageWindows1252 = 1252;
606 
607  private const int CodePageMacGB2312 = 10008;
608 
609  private const int CodePageGB2312 = 20936;
610 
611  private const int CodePageMacKorean = 10003;
612 
613  private const int CodePageDLLKorean = 20949;
614 
615  private const int ISO2022JP = 50220;
616 
617  private const int ISO2022JPESC = 50221;
618 
619  private const int ISO2022JPSISO = 50222;
620 
621  private const int ISOKorean = 50225;
622 
623  private const int ISOSimplifiedCN = 50227;
624 
625  private const int EUCJP = 51932;
626 
627  private const int ChineseHZ = 52936;
628 
629  private const int DuplicateEUCCN = 51936;
630 
631  private const int EUCCN = 936;
632 
633  private const int EUCKR = 51949;
634 
635  internal const int CodePageASCII = 20127;
636 
637  internal const int ISO_8859_1 = 28591;
638 
639  private const int ISCIIAssemese = 57006;
640 
641  private const int ISCIIBengali = 57003;
642 
643  private const int ISCIIDevanagari = 57002;
644 
645  private const int ISCIIGujarathi = 57010;
646 
647  private const int ISCIIKannada = 57008;
648 
649  private const int ISCIIMalayalam = 57009;
650 
651  private const int ISCIIOriya = 57007;
652 
653  private const int ISCIIPanjabi = 57011;
654 
655  private const int ISCIITamil = 57004;
656 
657  private const int ISCIITelugu = 57005;
658 
659  private const int GB18030 = 54936;
660 
661  private const int ISO_8859_8I = 38598;
662 
663  private const int ISO_8859_8_Visual = 28598;
664 
665  private const int ENC50229 = 50229;
666 
667  private const int CodePageUTF7 = 65000;
668 
669  private const int CodePageUTF8 = 65001;
670 
671  private const int CodePageUTF32 = 12000;
672 
673  private const int CodePageUTF32BE = 12001;
674 
675  internal int m_codePage;
676 
677  internal CodePageDataItem dataItem;
678 
679  [NonSerialized]
680  internal bool m_deserializedFromEverett;
681 
682  [OptionalField(VersionAdded = 2)]
683  private bool m_isReadOnly = true;
684 
685  [OptionalField(VersionAdded = 2)]
686  internal EncoderFallback encoderFallback;
687 
688  [OptionalField(VersionAdded = 2)]
689  internal DecoderFallback decoderFallback;
690 
691  private static object s_InternalSyncObject;
692 
693  private static object InternalSyncObject
694  {
695  get
696  {
697  if (s_InternalSyncObject == null)
698  {
699  object value = new object();
700  Interlocked.CompareExchange<object>(ref s_InternalSyncObject, value, (object)null);
701  }
702  return s_InternalSyncObject;
703  }
704  }
705 
708  public virtual string BodyName
709  {
710  get
711  {
712  if (dataItem == null)
713  {
714  GetDataItem();
715  }
716  return dataItem.BodyName;
717  }
718  }
719 
722  [__DynamicallyInvokable]
723  public virtual string EncodingName
724  {
725  [__DynamicallyInvokable]
726  get
727  {
728  return Environment.GetResourceString("Globalization.cp_" + m_codePage);
729  }
730  }
731 
734  public virtual string HeaderName
735  {
736  get
737  {
738  if (dataItem == null)
739  {
740  GetDataItem();
741  }
742  return dataItem.HeaderName;
743  }
744  }
745 
748  [__DynamicallyInvokable]
749  public virtual string WebName
750  {
751  [__DynamicallyInvokable]
752  get
753  {
754  if (dataItem == null)
755  {
756  GetDataItem();
757  }
758  return dataItem.WebName;
759  }
760  }
761 
764  public virtual int WindowsCodePage
765  {
766  get
767  {
768  if (dataItem == null)
769  {
770  GetDataItem();
771  }
772  return dataItem.UIFamilyCodePage;
773  }
774  }
775 
779  public virtual bool IsBrowserDisplay
780  {
781  get
782  {
783  if (dataItem == null)
784  {
785  GetDataItem();
786  }
787  return (dataItem.Flags & 2) != 0;
788  }
789  }
790 
794  public virtual bool IsBrowserSave
795  {
796  get
797  {
798  if (dataItem == null)
799  {
800  GetDataItem();
801  }
802  return (dataItem.Flags & 0x200) != 0;
803  }
804  }
805 
809  public virtual bool IsMailNewsDisplay
810  {
811  get
812  {
813  if (dataItem == null)
814  {
815  GetDataItem();
816  }
817  return (dataItem.Flags & 1) != 0;
818  }
819  }
820 
824  public virtual bool IsMailNewsSave
825  {
826  get
827  {
828  if (dataItem == null)
829  {
830  GetDataItem();
831  }
832  return (dataItem.Flags & 0x100) != 0;
833  }
834  }
835 
839  [ComVisible(false)]
840  [__DynamicallyInvokable]
841  public virtual bool IsSingleByte
842  {
843  [__DynamicallyInvokable]
844  get
845  {
846  return false;
847  }
848  }
849 
854  [ComVisible(false)]
855  [__DynamicallyInvokable]
857  {
858  [__DynamicallyInvokable]
859  get
860  {
861  return encoderFallback;
862  }
863  set
864  {
865  if (IsReadOnly)
866  {
867  throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_ReadOnly"));
868  }
869  if (value == null)
870  {
871  throw new ArgumentNullException("value");
872  }
873  encoderFallback = value;
874  }
875  }
876 
881  [ComVisible(false)]
882  [__DynamicallyInvokable]
884  {
885  [__DynamicallyInvokable]
886  get
887  {
888  return decoderFallback;
889  }
890  set
891  {
892  if (IsReadOnly)
893  {
894  throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_ReadOnly"));
895  }
896  if (value == null)
897  {
898  throw new ArgumentNullException("value");
899  }
900  decoderFallback = value;
901  }
902  }
903 
907  [ComVisible(false)]
908  public bool IsReadOnly
909  {
910  get
911  {
912  return m_isReadOnly;
913  }
914  }
915 
918  [__DynamicallyInvokable]
919  public static Encoding ASCII
920  {
921  [__DynamicallyInvokable]
922  get
923  {
924  if (asciiEncoding == null)
925  {
926  asciiEncoding = new ASCIIEncoding();
927  }
928  return asciiEncoding;
929  }
930  }
931 
932  private static Encoding Latin1
933  {
934  get
935  {
936  if (latin1Encoding == null)
937  {
938  latin1Encoding = new Latin1Encoding();
939  }
940  return latin1Encoding;
941  }
942  }
943 
946  [__DynamicallyInvokable]
947  public virtual int CodePage
948  {
949  [__DynamicallyInvokable]
950  get
951  {
952  return m_codePage;
953  }
954  }
955 
958  public static Encoding Default
959  {
960  [SecuritySafeCritical]
961  get
962  {
963  if (defaultEncoding == null)
964  {
965  defaultEncoding = CreateDefaultEncoding();
966  }
967  return defaultEncoding;
968  }
969  }
970 
973  [__DynamicallyInvokable]
974  public static Encoding Unicode
975  {
976  [__DynamicallyInvokable]
977  get
978  {
979  if (unicodeEncoding == null)
980  {
981  unicodeEncoding = new UnicodeEncoding(bigEndian: false, byteOrderMark: true);
982  }
983  return unicodeEncoding;
984  }
985  }
986 
989  [__DynamicallyInvokable]
990  public static Encoding BigEndianUnicode
991  {
992  [__DynamicallyInvokable]
993  get
994  {
995  if (bigEndianUnicode == null)
996  {
997  bigEndianUnicode = new UnicodeEncoding(bigEndian: true, byteOrderMark: true);
998  }
999  return bigEndianUnicode;
1000  }
1001  }
1002 
1005  [__DynamicallyInvokable]
1006  public static Encoding UTF7
1007  {
1008  [__DynamicallyInvokable]
1009  get
1010  {
1011  if (utf7Encoding == null)
1012  {
1013  utf7Encoding = new UTF7Encoding();
1014  }
1015  return utf7Encoding;
1016  }
1017  }
1018 
1021  [__DynamicallyInvokable]
1022  public static Encoding UTF8
1023  {
1024  [__DynamicallyInvokable]
1025  get
1026  {
1027  if (utf8Encoding == null)
1028  {
1029  utf8Encoding = new UTF8Encoding(encoderShouldEmitUTF8Identifier: true);
1030  }
1031  return utf8Encoding;
1032  }
1033  }
1034 
1037  [__DynamicallyInvokable]
1038  public static Encoding UTF32
1039  {
1040  [__DynamicallyInvokable]
1041  get
1042  {
1043  if (utf32Encoding == null)
1044  {
1045  utf32Encoding = new UTF32Encoding(bigEndian: false, byteOrderMark: true);
1046  }
1047  return utf32Encoding;
1048  }
1049  }
1050 
1052  [__DynamicallyInvokable]
1053  protected Encoding()
1054  : this(0)
1055  {
1056  }
1057 
1062  [__DynamicallyInvokable]
1063  protected Encoding(int codePage)
1064  {
1065  if (codePage < 0)
1066  {
1067  throw new ArgumentOutOfRangeException("codePage");
1068  }
1069  m_codePage = codePage;
1070  SetDefaultFallbacks();
1071  }
1072 
1079  [__DynamicallyInvokable]
1080  protected Encoding(int codePage, EncoderFallback encoderFallback, DecoderFallback decoderFallback)
1081  {
1082  if (codePage < 0)
1083  {
1084  throw new ArgumentOutOfRangeException("codePage");
1085  }
1086  m_codePage = codePage;
1087  this.encoderFallback = (encoderFallback ?? new InternalEncoderBestFitFallback(this));
1088  this.decoderFallback = (decoderFallback ?? new InternalDecoderBestFitFallback(this));
1089  }
1090 
1091  internal virtual void SetDefaultFallbacks()
1092  {
1093  encoderFallback = new InternalEncoderBestFitFallback(this);
1094  decoderFallback = new InternalDecoderBestFitFallback(this);
1095  }
1096 
1097  internal void OnDeserializing()
1098  {
1099  encoderFallback = null;
1100  decoderFallback = null;
1101  m_isReadOnly = true;
1102  }
1103 
1104  internal void OnDeserialized()
1105  {
1106  if (encoderFallback == null || decoderFallback == null)
1107  {
1108  m_deserializedFromEverett = true;
1109  SetDefaultFallbacks();
1110  }
1111  dataItem = null;
1112  }
1113 
1114  [OnDeserializing]
1115  private void OnDeserializing(StreamingContext ctx)
1116  {
1117  OnDeserializing();
1118  }
1119 
1120  [OnDeserialized]
1121  private void OnDeserialized(StreamingContext ctx)
1122  {
1123  OnDeserialized();
1124  }
1125 
1126  [OnSerializing]
1127  private void OnSerializing(StreamingContext ctx)
1128  {
1129  dataItem = null;
1130  }
1131 
1132  internal void DeserializeEncoding(SerializationInfo info, StreamingContext context)
1133  {
1134  if (info == null)
1135  {
1136  throw new ArgumentNullException("info");
1137  }
1138  m_codePage = (int)info.GetValue("m_codePage", typeof(int));
1139  dataItem = null;
1140  try
1141  {
1142  m_isReadOnly = (bool)info.GetValue("m_isReadOnly", typeof(bool));
1143  encoderFallback = (EncoderFallback)info.GetValue("encoderFallback", typeof(EncoderFallback));
1144  decoderFallback = (DecoderFallback)info.GetValue("decoderFallback", typeof(DecoderFallback));
1145  }
1146  catch (SerializationException)
1147  {
1148  m_deserializedFromEverett = true;
1149  m_isReadOnly = true;
1150  SetDefaultFallbacks();
1151  }
1152  }
1153 
1154  internal void SerializeEncoding(SerializationInfo info, StreamingContext context)
1155  {
1156  if (info == null)
1157  {
1158  throw new ArgumentNullException("info");
1159  }
1160  info.AddValue("m_isReadOnly", m_isReadOnly);
1161  info.AddValue("encoderFallback", EncoderFallback);
1162  info.AddValue("decoderFallback", DecoderFallback);
1163  info.AddValue("m_codePage", m_codePage);
1164  info.AddValue("dataItem", null);
1165  info.AddValue("Encoding+m_codePage", m_codePage);
1166  info.AddValue("Encoding+dataItem", null);
1167  }
1168 
1184  [__DynamicallyInvokable]
1185  public static byte[] Convert(Encoding srcEncoding, Encoding dstEncoding, byte[] bytes)
1186  {
1187  if (bytes == null)
1188  {
1189  throw new ArgumentNullException("bytes");
1190  }
1191  return Convert(srcEncoding, dstEncoding, bytes, 0, bytes.Length);
1192  }
1193 
1213  [__DynamicallyInvokable]
1214  public static byte[] Convert(Encoding srcEncoding, Encoding dstEncoding, byte[] bytes, int index, int count)
1215  {
1216  if (srcEncoding == null || dstEncoding == null)
1217  {
1218  throw new ArgumentNullException((srcEncoding == null) ? "srcEncoding" : "dstEncoding", Environment.GetResourceString("ArgumentNull_Array"));
1219  }
1220  if (bytes == null)
1221  {
1222  throw new ArgumentNullException("bytes", Environment.GetResourceString("ArgumentNull_Array"));
1223  }
1224  return dstEncoding.GetBytes(srcEncoding.GetChars(bytes, index, count));
1225  }
1226 
1231  [SecurityCritical]
1232  [__DynamicallyInvokable]
1233  public static void RegisterProvider(EncodingProvider provider)
1234  {
1235  EncodingProvider.AddProvider(provider);
1236  }
1237 
1247  [SecuritySafeCritical]
1248  [__DynamicallyInvokable]
1249  public static Encoding GetEncoding(int codepage)
1250  {
1251  Encoding encoding = EncodingProvider.GetEncodingFromProvider(codepage);
1252  if (encoding != null)
1253  {
1254  return encoding;
1255  }
1256  if (codepage < 0 || codepage > 65535)
1257  {
1258  throw new ArgumentOutOfRangeException("codepage", Environment.GetResourceString("ArgumentOutOfRange_Range", 0, 65535));
1259  }
1260  if (encodings != null)
1261  {
1262  encoding = (Encoding)encodings[codepage];
1263  }
1264  if (encoding == null)
1265  {
1266  lock (InternalSyncObject)
1267  {
1268  if (encodings == null)
1269  {
1270  encodings = new Hashtable();
1271  }
1272  if ((encoding = (Encoding)encodings[codepage]) != null)
1273  {
1274  return encoding;
1275  }
1276  switch (codepage)
1277  {
1278  case 0:
1279  encoding = Default;
1280  break;
1281  case 1200:
1282  encoding = Unicode;
1283  break;
1284  case 1201:
1285  encoding = BigEndianUnicode;
1286  break;
1287  case 1252:
1288  encoding = new SBCSCodePageEncoding(codepage);
1289  break;
1290  case 65001:
1291  encoding = UTF8;
1292  break;
1293  case 1:
1294  case 2:
1295  case 3:
1296  case 42:
1297  throw new ArgumentException(Environment.GetResourceString("Argument_CodepageNotSupported", codepage), "codepage");
1298  case 20127:
1299  encoding = ASCII;
1300  break;
1301  case 28591:
1302  encoding = Latin1;
1303  break;
1304  default:
1305  encoding = GetEncodingCodePage(codepage);
1306  if (encoding == null)
1307  {
1308  encoding = GetEncodingRare(codepage);
1309  }
1310  break;
1311  }
1312  encodings.Add(codepage, encoding);
1313  return encoding;
1314  }
1315  }
1316  return encoding;
1317  }
1318 
1330  [__DynamicallyInvokable]
1331  public static Encoding GetEncoding(int codepage, EncoderFallback encoderFallback, DecoderFallback decoderFallback)
1332  {
1333  Encoding encodingFromProvider = EncodingProvider.GetEncodingFromProvider(codepage, encoderFallback, decoderFallback);
1334  if (encodingFromProvider != null)
1335  {
1336  return encodingFromProvider;
1337  }
1338  encodingFromProvider = GetEncoding(codepage);
1339  Encoding encoding = (Encoding)encodingFromProvider.Clone();
1340  encoding.EncoderFallback = encoderFallback;
1341  encoding.DecoderFallback = decoderFallback;
1342  return encoding;
1343  }
1344 
1345  [SecurityCritical]
1346  private static Encoding GetEncodingRare(int codepage)
1347  {
1348  switch (codepage)
1349  {
1350  case 65000:
1351  return UTF7;
1352  case 12000:
1353  return UTF32;
1354  case 12001:
1355  return new UTF32Encoding(bigEndian: true, byteOrderMark: true);
1356  case 57002:
1357  case 57003:
1358  case 57004:
1359  case 57005:
1360  case 57006:
1361  case 57007:
1362  case 57008:
1363  case 57009:
1364  case 57010:
1365  case 57011:
1366  return new ISCIIEncoding(codepage);
1367  case 10008:
1368  return new DBCSCodePageEncoding(10008, 20936);
1369  case 10003:
1370  return new DBCSCodePageEncoding(10003, 20949);
1371  case 54936:
1372  return new GB18030Encoding();
1373  case 50220:
1374  case 50221:
1375  case 50222:
1376  case 50225:
1377  case 52936:
1378  return new ISO2022Encoding(codepage);
1379  case 50227:
1380  case 51936:
1381  return new DBCSCodePageEncoding(codepage, 936);
1382  case 51932:
1383  return new EUCJPEncoding();
1384  case 51949:
1385  return new DBCSCodePageEncoding(codepage, 20949);
1386  case 50229:
1387  throw new NotSupportedException(Environment.GetResourceString("NotSupported_CodePage50229"));
1388  case 38598:
1389  return new SBCSCodePageEncoding(codepage, 28598);
1390  default:
1391  throw new NotSupportedException(Environment.GetResourceString("NotSupported_NoCodepageData", codepage));
1392  }
1393  }
1394 
1395  [SecurityCritical]
1396  private static Encoding GetEncodingCodePage(int CodePage)
1397  {
1398  switch (BaseCodePageEncoding.GetCodePageByteSize(CodePage))
1399  {
1400  case 1:
1401  return new SBCSCodePageEncoding(CodePage);
1402  case 2:
1403  return new DBCSCodePageEncoding(CodePage);
1404  default:
1405  return null;
1406  }
1407  }
1408 
1414  [__DynamicallyInvokable]
1415  public static Encoding GetEncoding(string name)
1416  {
1417  Encoding encodingFromProvider = EncodingProvider.GetEncodingFromProvider(name);
1418  if (encodingFromProvider != null)
1419  {
1420  return encodingFromProvider;
1421  }
1422  return GetEncoding(EncodingTable.GetCodePageFromName(name));
1423  }
1424 
1432  [__DynamicallyInvokable]
1433  public static Encoding GetEncoding(string name, EncoderFallback encoderFallback, DecoderFallback decoderFallback)
1434  {
1435  Encoding encodingFromProvider = EncodingProvider.GetEncodingFromProvider(name, encoderFallback, decoderFallback);
1436  if (encodingFromProvider != null)
1437  {
1438  return encodingFromProvider;
1439  }
1440  return GetEncoding(EncodingTable.GetCodePageFromName(name), encoderFallback, decoderFallback);
1441  }
1442 
1445  public static EncodingInfo[] GetEncodings()
1446  {
1447  return EncodingTable.GetEncodings();
1448  }
1449 
1452  [__DynamicallyInvokable]
1453  public virtual byte[] GetPreamble()
1454  {
1455  return EmptyArray<byte>.Value;
1456  }
1457 
1458  private void GetDataItem()
1459  {
1460  if (dataItem == null)
1461  {
1462  dataItem = EncodingTable.GetCodePageDataItem(m_codePage);
1463  if (dataItem == null)
1464  {
1465  throw new NotSupportedException(Environment.GetResourceString("NotSupported_NoCodepageData", m_codePage));
1466  }
1467  }
1468  }
1469 
1472  [ComVisible(false)]
1473  [__DynamicallyInvokable]
1474  public virtual object Clone()
1475  {
1476  Encoding encoding = (Encoding)MemberwiseClone();
1477  encoding.m_isReadOnly = false;
1478  return encoding;
1479  }
1480 
1488  [__DynamicallyInvokable]
1489  public virtual int GetByteCount(char[] chars)
1490  {
1491  if (chars == null)
1492  {
1493  throw new ArgumentNullException("chars", Environment.GetResourceString("ArgumentNull_Array"));
1494  }
1495  return GetByteCount(chars, 0, chars.Length);
1496  }
1497 
1505  [__DynamicallyInvokable]
1506  public virtual int GetByteCount(string s)
1507  {
1508  if (s == null)
1509  {
1510  throw new ArgumentNullException("s");
1511  }
1512  char[] array = s.ToCharArray();
1513  return GetByteCount(array, 0, array.Length);
1514  }
1515 
1528  [__DynamicallyInvokable]
1529  public abstract int GetByteCount(char[] chars, int index, int count);
1530 
1541  [SecurityCritical]
1542  [CLSCompliant(false)]
1543  [ComVisible(false)]
1544  public unsafe virtual int GetByteCount(char* chars, int count)
1545  {
1546  if (chars == null)
1547  {
1548  throw new ArgumentNullException("chars", Environment.GetResourceString("ArgumentNull_Array"));
1549  }
1550  if (count < 0)
1551  {
1552  throw new ArgumentOutOfRangeException("count", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
1553  }
1554  char[] array = new char[count];
1555  for (int i = 0; i < count; i++)
1556  {
1557  array[i] = chars[i];
1558  }
1559  return GetByteCount(array, 0, count);
1560  }
1561 
1562  [SecurityCritical]
1563  internal unsafe virtual int GetByteCount(char* chars, int count, EncoderNLS encoder)
1564  {
1565  return GetByteCount(chars, count);
1566  }
1567 
1575  [__DynamicallyInvokable]
1576  public virtual byte[] GetBytes(char[] chars)
1577  {
1578  if (chars == null)
1579  {
1580  throw new ArgumentNullException("chars", Environment.GetResourceString("ArgumentNull_Array"));
1581  }
1582  return GetBytes(chars, 0, chars.Length);
1583  }
1584 
1597  [__DynamicallyInvokable]
1598  public virtual byte[] GetBytes(char[] chars, int index, int count)
1599  {
1600  byte[] array = new byte[GetByteCount(chars, index, count)];
1601  GetBytes(chars, index, count, array, 0);
1602  return array;
1603  }
1604 
1623  [__DynamicallyInvokable]
1624  public abstract int GetBytes(char[] chars, int charIndex, int charCount, byte[] bytes, int byteIndex);
1625 
1633  [__DynamicallyInvokable]
1634  public virtual byte[] GetBytes(string s)
1635  {
1636  if (s == null)
1637  {
1638  throw new ArgumentNullException("s", Environment.GetResourceString("ArgumentNull_String"));
1639  }
1640  int byteCount = GetByteCount(s);
1641  byte[] array = new byte[byteCount];
1642  int bytes = GetBytes(s, 0, s.Length, array, 0);
1643  return array;
1644  }
1645 
1664  [__DynamicallyInvokable]
1665  public virtual int GetBytes(string s, int charIndex, int charCount, byte[] bytes, int byteIndex)
1666  {
1667  if (s == null)
1668  {
1669  throw new ArgumentNullException("s");
1670  }
1671  return GetBytes(s.ToCharArray(), charIndex, charCount, bytes, byteIndex);
1672  }
1673 
1674  [SecurityCritical]
1675  internal unsafe virtual int GetBytes(char* chars, int charCount, byte* bytes, int byteCount, EncoderNLS encoder)
1676  {
1677  return GetBytes(chars, charCount, bytes, byteCount);
1678  }
1679 
1695  [SecurityCritical]
1696  [CLSCompliant(false)]
1697  [ComVisible(false)]
1698  public unsafe virtual int GetBytes(char* chars, int charCount, byte* bytes, int byteCount)
1699  {
1700  if (bytes == null || chars == null)
1701  {
1702  throw new ArgumentNullException((bytes == null) ? "bytes" : "chars", Environment.GetResourceString("ArgumentNull_Array"));
1703  }
1704  if (charCount < 0 || byteCount < 0)
1705  {
1706  throw new ArgumentOutOfRangeException((charCount < 0) ? "charCount" : "byteCount", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
1707  }
1708  char[] array = new char[charCount];
1709  for (int i = 0; i < charCount; i++)
1710  {
1711  array[i] = chars[i];
1712  }
1713  byte[] array2 = new byte[byteCount];
1714  int bytes2 = GetBytes(array, 0, charCount, array2, 0);
1715  if (bytes2 < byteCount)
1716  {
1717  byteCount = bytes2;
1718  }
1719  for (int i = 0; i < byteCount; i++)
1720  {
1721  bytes[i] = array2[i];
1722  }
1723  return byteCount;
1724  }
1725 
1733  [__DynamicallyInvokable]
1734  public virtual int GetCharCount(byte[] bytes)
1735  {
1736  if (bytes == null)
1737  {
1738  throw new ArgumentNullException("bytes", Environment.GetResourceString("ArgumentNull_Array"));
1739  }
1740  return GetCharCount(bytes, 0, bytes.Length);
1741  }
1742 
1755  [__DynamicallyInvokable]
1756  public abstract int GetCharCount(byte[] bytes, int index, int count);
1757 
1768  [SecurityCritical]
1769  [CLSCompliant(false)]
1770  [ComVisible(false)]
1771  public unsafe virtual int GetCharCount(byte* bytes, int count)
1772  {
1773  if (bytes == null)
1774  {
1775  throw new ArgumentNullException("bytes", Environment.GetResourceString("ArgumentNull_Array"));
1776  }
1777  if (count < 0)
1778  {
1779  throw new ArgumentOutOfRangeException("count", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
1780  }
1781  byte[] array = new byte[count];
1782  for (int i = 0; i < count; i++)
1783  {
1784  array[i] = bytes[i];
1785  }
1786  return GetCharCount(array, 0, count);
1787  }
1788 
1789  [SecurityCritical]
1790  internal unsafe virtual int GetCharCount(byte* bytes, int count, DecoderNLS decoder)
1791  {
1792  return GetCharCount(bytes, count);
1793  }
1794 
1802  [__DynamicallyInvokable]
1803  public virtual char[] GetChars(byte[] bytes)
1804  {
1805  if (bytes == null)
1806  {
1807  throw new ArgumentNullException("bytes", Environment.GetResourceString("ArgumentNull_Array"));
1808  }
1809  return GetChars(bytes, 0, bytes.Length);
1810  }
1811 
1824  [__DynamicallyInvokable]
1825  public virtual char[] GetChars(byte[] bytes, int index, int count)
1826  {
1827  char[] array = new char[GetCharCount(bytes, index, count)];
1828  GetChars(bytes, index, count, array, 0);
1829  return array;
1830  }
1831 
1850  [__DynamicallyInvokable]
1851  public abstract int GetChars(byte[] bytes, int byteIndex, int byteCount, char[] chars, int charIndex);
1852 
1868  [SecurityCritical]
1869  [CLSCompliant(false)]
1870  [ComVisible(false)]
1871  public unsafe virtual int GetChars(byte* bytes, int byteCount, char* chars, int charCount)
1872  {
1873  if (chars == null || bytes == null)
1874  {
1875  throw new ArgumentNullException((chars == null) ? "chars" : "bytes", Environment.GetResourceString("ArgumentNull_Array"));
1876  }
1877  if (byteCount < 0 || charCount < 0)
1878  {
1879  throw new ArgumentOutOfRangeException((byteCount < 0) ? "byteCount" : "charCount", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
1880  }
1881  byte[] array = new byte[byteCount];
1882  for (int i = 0; i < byteCount; i++)
1883  {
1884  array[i] = bytes[i];
1885  }
1886  char[] array2 = new char[charCount];
1887  int chars2 = GetChars(array, 0, byteCount, array2, 0);
1888  if (chars2 < charCount)
1889  {
1890  charCount = chars2;
1891  }
1892  for (int i = 0; i < charCount; i++)
1893  {
1894  chars[i] = array2[i];
1895  }
1896  return charCount;
1897  }
1898 
1899  [SecurityCritical]
1900  internal unsafe virtual int GetChars(byte* bytes, int byteCount, char* chars, int charCount, DecoderNLS decoder)
1901  {
1902  return GetChars(bytes, byteCount, chars, charCount);
1903  }
1904 
1915  [SecurityCritical]
1916  [CLSCompliant(false)]
1917  [ComVisible(false)]
1918  public unsafe string GetString(byte* bytes, int byteCount)
1919  {
1920  if (bytes == null)
1921  {
1922  throw new ArgumentNullException("bytes", Environment.GetResourceString("ArgumentNull_Array"));
1923  }
1924  if (byteCount < 0)
1925  {
1926  throw new ArgumentOutOfRangeException("byteCount", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
1927  }
1928  return string.CreateStringFromEncoding(bytes, byteCount, this);
1929  }
1930 
1934  [ComVisible(false)]
1935  public bool IsAlwaysNormalized()
1936  {
1937  return IsAlwaysNormalized(NormalizationForm.FormC);
1938  }
1939 
1944  [ComVisible(false)]
1945  public virtual bool IsAlwaysNormalized(NormalizationForm form)
1946  {
1947  return false;
1948  }
1949 
1952  [__DynamicallyInvokable]
1953  public virtual Decoder GetDecoder()
1954  {
1955  return new DefaultDecoder(this);
1956  }
1957 
1958  [SecurityCritical]
1959  private static Encoding CreateDefaultEncoding()
1960  {
1961  int aCP = Win32Native.GetACP();
1962  if (aCP == 1252)
1963  {
1964  return new SBCSCodePageEncoding(aCP);
1965  }
1966  return GetEncoding(aCP);
1967  }
1968 
1971  [__DynamicallyInvokable]
1972  public virtual Encoder GetEncoder()
1973  {
1974  return new DefaultEncoder(this);
1975  }
1976 
1984  [__DynamicallyInvokable]
1985  public abstract int GetMaxByteCount(int charCount);
1986 
1994  [__DynamicallyInvokable]
1995  public abstract int GetMaxCharCount(int byteCount);
1996 
2005  [__DynamicallyInvokable]
2006  public virtual string GetString(byte[] bytes)
2007  {
2008  if (bytes == null)
2009  {
2010  throw new ArgumentNullException("bytes", Environment.GetResourceString("ArgumentNull_Array"));
2011  }
2012  return GetString(bytes, 0, bytes.Length);
2013  }
2014 
2028  [__DynamicallyInvokable]
2029  public virtual string GetString(byte[] bytes, int index, int count)
2030  {
2031  return new string(GetChars(bytes, index, count));
2032  }
2033 
2038  [__DynamicallyInvokable]
2039  public override bool Equals(object value)
2040  {
2041  Encoding encoding = value as Encoding;
2042  if (encoding != null)
2043  {
2044  if (m_codePage == encoding.m_codePage && EncoderFallback.Equals(encoding.EncoderFallback))
2045  {
2046  return DecoderFallback.Equals(encoding.DecoderFallback);
2047  }
2048  return false;
2049  }
2050  return false;
2051  }
2052 
2055  [__DynamicallyInvokable]
2056  public override int GetHashCode()
2057  {
2058  return m_codePage + EncoderFallback.GetHashCode() + DecoderFallback.GetHashCode();
2059  }
2060 
2061  internal virtual char[] GetBestFitUnicodeToBytesData()
2062  {
2063  return EmptyArray<char>.Value;
2064  }
2065 
2066  internal virtual char[] GetBestFitBytesToUnicodeData()
2067  {
2068  return EmptyArray<char>.Value;
2069  }
2070 
2071  internal void ThrowBytesOverflow()
2072  {
2073  throw new ArgumentException(Environment.GetResourceString("Argument_EncodingConversionOverflowBytes", EncodingName, EncoderFallback.GetType()), "bytes");
2074  }
2075 
2076  [SecurityCritical]
2077  internal void ThrowBytesOverflow(EncoderNLS encoder, bool nothingEncoded)
2078  {
2079  if ((encoder?.m_throwOnOverflow ?? true) | nothingEncoded)
2080  {
2081  if (encoder != null && encoder.InternalHasFallbackBuffer)
2082  {
2083  encoder.FallbackBuffer.InternalReset();
2084  }
2085  ThrowBytesOverflow();
2086  }
2087  encoder.ClearMustFlush();
2088  }
2089 
2090  internal void ThrowCharsOverflow()
2091  {
2092  throw new ArgumentException(Environment.GetResourceString("Argument_EncodingConversionOverflowChars", EncodingName, DecoderFallback.GetType()), "chars");
2093  }
2094 
2095  [SecurityCritical]
2096  internal void ThrowCharsOverflow(DecoderNLS decoder, bool nothingDecoded)
2097  {
2098  if ((decoder?.m_throwOnOverflow ?? true) | nothingDecoded)
2099  {
2100  if (decoder != null && decoder.InternalHasFallbackBuffer)
2101  {
2102  decoder.FallbackBuffer.InternalReset();
2103  }
2104  ThrowCharsOverflow();
2105  }
2106  decoder.ClearMustFlush();
2107  }
2108  }
2109 }
Represents a character encoding.To browse the .NET Framework source code for this type,...
Definition: Encoding.cs:15
The exception that is thrown when a null reference (Nothing in Visual Basic) is passed to a method th...
unsafe string GetString(byte *bytes, int byteCount)
When overridden in a derived class, decodes a specified number of bytes starting at a specified addre...
Definition: Encoding.cs:1918
static void RegisterProvider(EncodingProvider provider)
Registers an encoding provider.
Definition: Encoding.cs:1233
virtual unsafe int GetBytes(char *chars, int charCount, byte *bytes, int byteCount)
When overridden in a derived class, encodes a set of characters starting at the specified character p...
Definition: Encoding.cs:1698
virtual char [] GetChars(byte[] bytes)
When overridden in a derived class, decodes all the bytes in the specified byte array into a set of c...
Definition: Encoding.cs:1803
virtual void Add(object key, object value)
Adds an element with the specified key and value into the T:System.Collections.Hashtable.
Definition: Hashtable.cs:916
Converts a set of characters into a sequence of bytes.
Definition: Encoder.cs:11
virtual byte [] GetBytes(char[] chars)
When overridden in a derived class, encodes all the characters in the specified character array into ...
Definition: Encoding.cs:1576
virtual Encoder GetEncoder()
When overridden in a derived class, obtains an encoder that converts a sequence of Unicode characters...
Definition: Encoding.cs:1972
virtual bool IsBrowserSave
When overridden in a derived class, gets a value indicating whether the current encoding can be used ...
Definition: Encoding.cs:795
Indicates that the current interface implementer is a reference to another object.
virtual byte [] GetBytes(string s)
When overridden in a derived class, encodes all the characters in the specified string into a sequenc...
Definition: Encoding.cs:1634
static Encoding Default
Gets an encoding for the operating system's current ANSI code page.
Definition: Encoding.cs:959
static Encoding Unicode
Gets an encoding for the UTF-16 format using the little endian byte order.
Definition: Encoding.cs:975
abstract int GetMaxCharCount(int byteCount)
When overridden in a derived class, calculates the maximum number of characters produced by decoding ...
virtual object Clone()
When overridden in a derived class, creates a shallow copy of the current T:System....
Definition: Encoding.cs:1474
virtual bool IsAlwaysNormalized(NormalizationForm form)
When overridden in a derived class, gets a value indicating whether the current encoding is always no...
Definition: Encoding.cs:1945
Definition: __Canon.cs:3
The exception that is thrown when the value of an argument is outside the allowable range of values a...
abstract int Remaining
When overridden in a derived class, gets the number of characters in the current T:System....
DecoderFallback DecoderFallback
Gets or sets the T:System.Text.DecoderFallback object for the current T:System.Text....
Definition: Encoding.cs:884
static byte [] Convert(Encoding srcEncoding, Encoding dstEncoding, byte[] bytes)
Converts an entire byte array from one encoding to another.
Definition: Encoding.cs:1185
static Encoding GetEncoding(int codepage, EncoderFallback encoderFallback, DecoderFallback decoderFallback)
Returns the encoding associated with the specified code page identifier. Parameters specify an error ...
Definition: Encoding.cs:1331
bool IsAlwaysNormalized()
Gets a value indicating whether the current encoding is always normalized, using the default normaliz...
Definition: Encoding.cs:1935
virtual int GetCharCount(byte[] bytes)
When overridden in a derived class, calculates the number of characters produced by decoding all the ...
Definition: Encoding.cs:1734
EncoderFallback EncoderFallback
Gets or sets the T:System.Text.EncoderFallback object for the current T:System.Text....
Definition: Encoding.cs:857
virtual Decoder GetDecoder()
When overridden in a derived class, obtains a decoder that converts an encoded sequence of bytes into...
Definition: Encoding.cs:1953
Describes the source and destination of a given serialized stream, and provides an additional caller-...
virtual unsafe int GetByteCount(char *chars, int count)
When overridden in a derived class, calculates the number of bytes produced by encoding a set of char...
Definition: Encoding.cs:1544
static Encoding GetEncoding(int codepage)
Returns the encoding associated with the specified code page identifier.
Definition: Encoding.cs:1249
virtual string HeaderName
When overridden in a derived class, gets a name for the current encoding that can be used with mail a...
Definition: Encoding.cs:735
static Encoding ASCII
Gets an encoding for the ASCII (7-bit) character set.
Definition: Encoding.cs:920
virtual bool IsSingleByte
When overridden in a derived class, gets a value indicating whether the current encoding uses single-...
Definition: Encoding.cs:842
Provides a failure-handling mechanism, called a fallback, for an input character that cannot be conve...
Encoding(int codePage)
Initializes a new instance of the T:System.Text.Encoding class that corresponds to the specified code...
Definition: Encoding.cs:1063
virtual bool IsMailNewsDisplay
When overridden in a derived class, gets a value indicating whether the current encoding can be used ...
Definition: Encoding.cs:810
static Encoding BigEndianUnicode
Gets an encoding for the UTF-16 format that uses the big endian byte order.
Definition: Encoding.cs:991
static EncodingInfo [] GetEncodings()
Returns an array that contains all encodings.
Definition: Encoding.cs:1445
static Encoding UTF32
Gets an encoding for the UTF-32 format using the little endian byte order.
Definition: Encoding.cs:1039
Encoding(int codePage, EncoderFallback encoderFallback, DecoderFallback decoderFallback)
Initializes a new instance of the T:System.Text.Encoding class that corresponds to the specified code...
Definition: Encoding.cs:1080
Provides information about, and means to manipulate, the current environment and platform....
Definition: Environment.cs:21
override bool Equals(object value)
Determines whether the specified T:System.Object is equal to the current instance.
Definition: Encoding.cs:2039
virtual bool IsMailNewsSave
When overridden in a derived class, gets a value indicating whether the current encoding can be used ...
Definition: Encoding.cs:825
Represents a UTF-16 encoding of Unicode characters.
virtual int GetBytes(string s, int charIndex, int charCount, byte[] bytes, int byteIndex)
When overridden in a derived class, encodes a set of characters from the specified string into the sp...
Definition: Encoding.cs:1665
virtual string WebName
When overridden in a derived class, gets the name registered with the Internet Assigned Numbers Autho...
Definition: Encoding.cs:750
NormalizationForm
Defines the type of normalization to perform.
Provides a buffer that allows a fallback handler to return an alternate string to a decoder when it c...
virtual int CodePage
When overridden in a derived class, gets the code page identifier of the current T:System....
Definition: Encoding.cs:948
virtual char [] GetChars(byte[] bytes, int index, int count)
When overridden in a derived class, decodes a sequence of bytes from the specified byte array into a ...
Definition: Encoding.cs:1825
static int CompareExchange(ref int location1, int value, int comparand)
Compares two 32-bit signed integers for equality and, if they are equal, replaces the first value.
Represents a collection of key/value pairs that are organized based on the hash code of the key....
Definition: Hashtable.cs:17
Represents a UTF-32 encoding of Unicode characters.
Definition: UTF32Encoding.cs:8
virtual int GetByteCount(string s)
When overridden in a derived class, calculates the number of bytes produced by encoding the character...
Definition: Encoding.cs:1506
Converts a sequence of encoded bytes into a set of characters.
Definition: Decoder.cs:11
virtual string EncodingName
When overridden in a derived class, gets the human-readable description of the current encoding.
Definition: Encoding.cs:724
Represents a UTF-7 encoding of Unicode characters.
Definition: UTF7Encoding.cs:11
Supports cloning, which creates a new instance of a class with the same value as an existing instance...
Definition: ICloneable.cs:7
Provides a failure-handling mechanism, called a fallback, for an encoded input byte sequence that can...
Represents a UTF-8 encoding of Unicode characters.
Definition: UTF8Encoding.cs:11
Represents an ASCII character encoding of Unicode characters.
virtual byte [] GetBytes(char[] chars, int index, int count)
When overridden in a derived class, encodes a set of characters from the specified character array in...
Definition: Encoding.cs:1598
The exception thrown when an error occurs during serialization or deserialization.
static Encoding GetEncoding(string name, EncoderFallback encoderFallback, DecoderFallback decoderFallback)
Returns the encoding associated with the specified code page name. Parameters specify an error handle...
Definition: Encoding.cs:1433
abstract DecoderFallbackBuffer CreateFallbackBuffer()
When overridden in a derived class, initializes a new instance of the T:System.Text....
virtual unsafe int GetCharCount(byte *bytes, int count)
When overridden in a derived class, calculates the number of characters produced by decoding a sequen...
Definition: Encoding.cs:1771
Stores all the data needed to serialize or deserialize an object. This class cannot be inherited.
virtual string GetString(byte[] bytes)
When overridden in a derived class, decodes all the bytes in the specified byte array into a string.
Definition: Encoding.cs:2006
virtual string GetString(byte[] bytes, int index, int count)
When overridden in a derived class, decodes a sequence of bytes from the specified byte array into a ...
Definition: Encoding.cs:2029
The exception that is thrown when one of the arguments provided to a method is not valid.
static byte [] Convert(Encoding srcEncoding, Encoding dstEncoding, byte[] bytes, int index, int count)
Converts a range of bytes in a byte array from one encoding to another.
Definition: Encoding.cs:1214
virtual unsafe int GetChars(byte *bytes, int byteCount, char *chars, int charCount)
When overridden in a derived class, decodes a sequence of bytes starting at the specified byte pointe...
Definition: Encoding.cs:1871
Allows an object to control its own serialization and deserialization.
Definition: ISerializable.cs:8
virtual byte [] GetPreamble()
When overridden in a derived class, returns a sequence of bytes that specifies the encoding used.
Definition: Encoding.cs:1453
virtual bool IsBrowserDisplay
When overridden in a derived class, gets a value indicating whether the current encoding can be used ...
Definition: Encoding.cs:780
virtual string BodyName
When overridden in a derived class, gets a name for the current encoding that can be used with mail a...
Definition: Encoding.cs:709
static Encoding UTF7
Gets an encoding for the UTF-7 format.
Definition: Encoding.cs:1007
override int GetHashCode()
Returns the hash code for the current instance.
Definition: Encoding.cs:2056
bool IsReadOnly
When overridden in a derived class, gets a value indicating whether the current encoding is read-only...
Definition: Encoding.cs:909
Provides a buffer that allows a fallback handler to return an alternate string to an encoder when it ...
Specifies that the class can be serialized.
static Encoding GetEncoding(string name)
Returns the encoding associated with the specified code page name.
Definition: Encoding.cs:1415
static Encoding UTF8
Gets an encoding for the UTF-8 format.
Definition: Encoding.cs:1023
virtual int GetByteCount(char[] chars)
When overridden in a derived class, calculates the number of bytes produced by encoding all the chara...
Definition: Encoding.cs:1489
The exception that is thrown when a method call is invalid for the object's current state.
Encoding()
Initializes a new instance of the T:System.Text.Encoding class.
Definition: Encoding.cs:1053
abstract EncoderFallbackBuffer CreateFallbackBuffer()
When overridden in a derived class, initializes a new instance of the T:System.Text....
virtual int WindowsCodePage
When overridden in a derived class, gets the Windows operating system code page that most closely cor...
Definition: Encoding.cs:765
The exception that is thrown when an invoked method is not supported, or when there is an attempt to ...
abstract bool MovePrevious()
When overridden in a derived class, causes the next call to the M:System.Text.EncoderFallbackBuffer....
abstract int GetMaxByteCount(int charCount)
When overridden in a derived class, calculates the maximum number of bytes produced by encoding the s...
void GetObjectData(SerializationInfo info, StreamingContext context)
Populates a T:System.Runtime.Serialization.SerializationInfo with the data needed to serialize the ta...
Provides atomic operations for variables that are shared by multiple threads.
Definition: Interlocked.cs:10
Provides basic information about an encoding.
Definition: EncodingInfo.cs:5
Provides the base class for an encoding provider, which supplies encodings that are unavailable on a ...