Encoding.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798
  1. /*
  2. * Encoding.cs - Implementation of the "System.Text.Encoding" class.
  3. *
  4. * Copyright (c) 2001, 2002 Southern Storm Software, Pty Ltd
  5. * Copyright (c) 2002, Ximian, Inc.
  6. * Copyright (c) 2003, Novell, Inc.
  7. *
  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:
  14. *
  15. * The above copyright notice and this permission notice shall be included
  16. * in all copies or substantial portions of the Software.
  17. *
  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.
  25. */
  26. namespace System.Text
  27. {
  28. using System;
  29. using System.Reflection;
  30. using System.Globalization;
  31. using System.Security;
  32. using System.Runtime.CompilerServices;
  33. [Serializable]
  34. public abstract class Encoding
  35. {
  36. // Code page used by this encoding.
  37. internal int codePage;
  38. internal int windows_code_page;
  39. // Constructor.
  40. protected Encoding ()
  41. {
  42. codePage = 0;
  43. }
  44. #if ECMA_COMPAT
  45. protected internal
  46. #else
  47. protected
  48. #endif
  49. Encoding (int codePage)
  50. {
  51. this.codePage = windows_code_page = codePage;
  52. }
  53. // until we change the callers:
  54. internal static string _ (string arg) {
  55. return arg;
  56. }
  57. // Convert between two encodings.
  58. public static byte[] Convert (Encoding srcEncoding, Encoding dstEncoding,
  59. byte[] bytes)
  60. {
  61. if (srcEncoding == null) {
  62. throw new ArgumentNullException ("srcEncoding");
  63. }
  64. if (dstEncoding == null) {
  65. throw new ArgumentNullException ("dstEncoding");
  66. }
  67. if (bytes == null) {
  68. throw new ArgumentNullException ("bytes");
  69. }
  70. return dstEncoding.GetBytes (srcEncoding.GetChars (bytes, 0, bytes.Length));
  71. }
  72. public static byte[] Convert (Encoding srcEncoding, Encoding dstEncoding,
  73. byte[] bytes, int index, int count)
  74. {
  75. if (srcEncoding == null) {
  76. throw new ArgumentNullException ("srcEncoding");
  77. }
  78. if (dstEncoding == null) {
  79. throw new ArgumentNullException ("dstEncoding");
  80. }
  81. if (bytes == null) {
  82. throw new ArgumentNullException ("bytes");
  83. }
  84. if (index < 0 || index > bytes.Length) {
  85. throw new ArgumentOutOfRangeException
  86. ("index", _("ArgRange_Array"));
  87. }
  88. if (count < 0 || (bytes.Length - index) < count) {
  89. throw new ArgumentOutOfRangeException
  90. ("count", _("ArgRange_Array"));
  91. }
  92. return dstEncoding.GetBytes (srcEncoding.GetChars (bytes, index, count));
  93. }
  94. // Determine if two Encoding objects are equal.
  95. public override bool Equals (Object obj)
  96. {
  97. Encoding enc = (obj as Encoding);
  98. if (enc != null) {
  99. return (codePage == enc.codePage);
  100. } else {
  101. return false;
  102. }
  103. }
  104. // Get the number of characters needed to encode a character buffer.
  105. public abstract int GetByteCount (char[] chars, int index, int count);
  106. // Convenience wrappers for "GetByteCount".
  107. public virtual int GetByteCount (String s)
  108. {
  109. if (s != null) {
  110. char[] chars = s.ToCharArray ();
  111. return GetByteCount (chars, 0, chars.Length);
  112. } else {
  113. throw new ArgumentNullException ("s");
  114. }
  115. }
  116. public virtual int GetByteCount (char[] chars)
  117. {
  118. if (chars != null) {
  119. return GetByteCount (chars, 0, chars.Length);
  120. } else {
  121. throw new ArgumentNullException ("chars");
  122. }
  123. }
  124. // Get the bytes that result from encoding a character buffer.
  125. public abstract int GetBytes (char[] chars, int charIndex, int charCount,
  126. byte[] bytes, int byteIndex);
  127. // Convenience wrappers for "GetBytes".
  128. public virtual int GetBytes (String s, int charIndex, int charCount,
  129. byte[] bytes, int byteIndex)
  130. {
  131. if (s == null) {
  132. throw new ArgumentNullException ("s");
  133. }
  134. return GetBytes (s.ToCharArray(), charIndex, charCount, bytes, byteIndex);
  135. }
  136. public virtual byte[] GetBytes (String s)
  137. {
  138. if (s == null) {
  139. throw new ArgumentNullException ("s");
  140. }
  141. char[] chars = s.ToCharArray ();
  142. int numBytes = GetByteCount (chars, 0, chars.Length);
  143. byte[] bytes = new byte [numBytes];
  144. GetBytes (chars, 0, chars.Length, bytes, 0);
  145. return bytes;
  146. }
  147. public virtual byte[] GetBytes (char[] chars, int index, int count)
  148. {
  149. int numBytes = GetByteCount (chars, index, count);
  150. byte[] bytes = new byte [numBytes];
  151. GetBytes (chars, index, count, bytes, 0);
  152. return bytes;
  153. }
  154. public virtual byte[] GetBytes (char[] chars)
  155. {
  156. int numBytes = GetByteCount (chars, 0, chars.Length);
  157. byte[] bytes = new byte [numBytes];
  158. GetBytes (chars, 0, chars.Length, bytes, 0);
  159. return bytes;
  160. }
  161. // Get the number of characters needed to decode a byte buffer.
  162. public abstract int GetCharCount (byte[] bytes, int index, int count);
  163. // Convenience wrappers for "GetCharCount".
  164. public virtual int GetCharCount (byte[] bytes)
  165. {
  166. if (bytes == null) {
  167. throw new ArgumentNullException ("bytes");
  168. }
  169. return GetCharCount (bytes, 0, bytes.Length);
  170. }
  171. // Get the characters that result from decoding a byte buffer.
  172. public abstract int GetChars (byte[] bytes, int byteIndex, int byteCount,
  173. char[] chars, int charIndex);
  174. // Convenience wrappers for "GetChars".
  175. public virtual char[] GetChars (byte[] bytes, int index, int count)
  176. {
  177. int numChars = GetCharCount (bytes, index, count);
  178. char[] chars = new char [numChars];
  179. GetChars (bytes, index, count, chars, 0);
  180. return chars;
  181. }
  182. public virtual char[] GetChars (byte[] bytes)
  183. {
  184. if (bytes == null) {
  185. throw new ArgumentNullException ("bytes");
  186. }
  187. int numChars = GetCharCount (bytes, 0, bytes.Length);
  188. char[] chars = new char [numChars];
  189. GetChars (bytes, 0, bytes.Length, chars, 0);
  190. return chars;
  191. }
  192. // Get a decoder that forwards requests to this object.
  193. public virtual Decoder GetDecoder ()
  194. {
  195. return new ForwardingDecoder (this);
  196. }
  197. // Get an encoder that forwards requests to this object.
  198. public virtual Encoder GetEncoder ()
  199. {
  200. return new ForwardingEncoder (this);
  201. }
  202. // Loaded copy of the "I18N" assembly. We need to move
  203. // this into a class in "System.Private" eventually.
  204. private static Assembly i18nAssembly;
  205. private static bool i18nDisabled;
  206. // Invoke a specific method on the "I18N" manager object.
  207. // Returns NULL if the method failed.
  208. private static Object InvokeI18N (String name, params Object[] args)
  209. {
  210. lock (typeof(Encoding)) {
  211. // Bail out if we previously detected that there
  212. // is insufficent engine support for I18N handling.
  213. if (i18nDisabled) {
  214. return null;
  215. }
  216. // Find or load the "I18N" assembly.
  217. if (i18nAssembly == null) {
  218. try {
  219. try {
  220. i18nAssembly = Assembly.Load (Consts.AssemblyI18N);
  221. } catch (NotImplementedException) {
  222. // Assembly loading unsupported by the engine.
  223. i18nDisabled = true;
  224. return null;
  225. }
  226. if (i18nAssembly == null) {
  227. return null;
  228. }
  229. } catch (SystemException) {
  230. return null;
  231. }
  232. }
  233. // Find the "I18N.Common.Manager" class.
  234. Type managerClass;
  235. try {
  236. managerClass = i18nAssembly.GetType ("I18N.Common.Manager");
  237. } catch (NotImplementedException) {
  238. // "GetType" is not supported by the engine.
  239. i18nDisabled = true;
  240. return null;
  241. }
  242. if (managerClass == null) {
  243. return null;
  244. }
  245. // Get the value of the "PrimaryManager" property.
  246. Object manager;
  247. try {
  248. manager = managerClass.InvokeMember
  249. ("PrimaryManager",
  250. BindingFlags.GetProperty |
  251. BindingFlags.Static |
  252. BindingFlags.Public,
  253. null, null, null, null, null, null);
  254. if (manager == null) {
  255. return null;
  256. }
  257. } catch (MissingMethodException) {
  258. return null;
  259. } catch (SecurityException) {
  260. return null;
  261. } catch (NotImplementedException) {
  262. // "InvokeMember" is not supported by the engine.
  263. i18nDisabled = true;
  264. return null;
  265. }
  266. // Invoke the requested method on the manager.
  267. try {
  268. return managerClass.InvokeMember
  269. (name,
  270. BindingFlags.InvokeMethod |
  271. BindingFlags.Instance |
  272. BindingFlags.Public,
  273. null, manager, args, null, null, null);
  274. } catch (MissingMethodException) {
  275. return null;
  276. } catch (SecurityException) {
  277. return null;
  278. }
  279. }
  280. }
  281. // Get an encoder for a specific code page.
  282. #if ECMA_COMPAT
  283. private
  284. #else
  285. public
  286. #endif
  287. static Encoding GetEncoding (int codePage)
  288. {
  289. // Check for the builtin code pages first.
  290. switch (codePage) {
  291. case 0: return Default;
  292. case ASCIIEncoding.ASCII_CODE_PAGE:
  293. return ASCII;
  294. case UTF7Encoding.UTF7_CODE_PAGE:
  295. return UTF7;
  296. case UTF8Encoding.UTF8_CODE_PAGE:
  297. return UTF8;
  298. case UnicodeEncoding.UNICODE_CODE_PAGE:
  299. return Unicode;
  300. case UnicodeEncoding.BIG_UNICODE_CODE_PAGE:
  301. return BigEndianUnicode;
  302. case Latin1Encoding.ISOLATIN_CODE_PAGE:
  303. return ISOLatin1;
  304. default: break;
  305. }
  306. // Try to obtain a code page handler from the I18N handler.
  307. Encoding enc = (Encoding)(InvokeI18N ("GetEncoding", codePage));
  308. if (enc != null) {
  309. return enc;
  310. }
  311. // Build a code page class name.
  312. String cpName = "System.Text.CP" + codePage.ToString ();
  313. // Look for a code page converter in this assembly.
  314. Assembly assembly = Assembly.GetExecutingAssembly ();
  315. Type type = assembly.GetType (cpName);
  316. if (type != null) {
  317. return (Encoding)(Activator.CreateInstance (type));
  318. }
  319. // Look in any assembly, in case the application
  320. // has provided its own code page handler.
  321. type = Type.GetType (cpName);
  322. if (type != null) {
  323. return (Encoding)(Activator.CreateInstance (type));
  324. }
  325. // We have no idea how to handle this code page.
  326. throw new NotSupportedException
  327. (String.Format ("CodePage {0} not supported", codePage.ToString ()));
  328. }
  329. #if !ECMA_COMPAT
  330. // Table of builtin web encoding names and the corresponding code pages.
  331. private static readonly object[] encodings =
  332. {
  333. ASCIIEncoding.ASCII_CODE_PAGE,
  334. "ascii", "us_ascii", "us", "ansi_x3.4_1968",
  335. "ansi_x3.4_1986", "cp367", "csascii", "ibm367",
  336. "iso_ir_6", "iso646_us", "iso_646.irv:1991",
  337. UTF7Encoding.UTF7_CODE_PAGE,
  338. "utf_7", "csunicode11utf7", "unicode_1_1_utf_7",
  339. "unicode_2_0_utf_7", "x_unicode_1_1_utf_7",
  340. "x_unicode_2_0_utf_7",
  341. UTF8Encoding.UTF8_CODE_PAGE,
  342. "utf_8", "unicode_1_1_utf_8", "unicode_2_0_utf_8",
  343. "x_unicode_1_1_utf_8", "x_unicode_2_0_utf_8",
  344. UnicodeEncoding.UNICODE_CODE_PAGE,
  345. "utf_16", "UTF_16LE", "ucs_2", "unicode",
  346. "iso_10646_ucs2",
  347. UnicodeEncoding.BIG_UNICODE_CODE_PAGE,
  348. "unicodefffe", "utf_16be",
  349. Latin1Encoding.ISOLATIN_CODE_PAGE,
  350. "iso_8859_1",
  351. };
  352. // Get an encoding object for a specific web encoding name.
  353. public static Encoding GetEncoding (String name)
  354. {
  355. // Validate the parameters.
  356. if (name == null) {
  357. throw new ArgumentNullException ("name");
  358. }
  359. string converted = name.ToLowerInvariant ().Replace ('-', '_');
  360. // Search the table for a name match.
  361. int code = 0;
  362. for (int i = 0; i < encodings.Length; ++i) {
  363. object o = encodings [i];
  364. if (o is int){
  365. code = (int) o;
  366. continue;
  367. }
  368. if (converted == ((string)encodings [i]))
  369. return GetEncoding (code);
  370. }
  371. // Try to obtain a web encoding handler from the I18N handler.
  372. Encoding enc = (Encoding)(InvokeI18N ("GetEncoding", name));
  373. if (enc != null) {
  374. return enc;
  375. }
  376. // Build a web encoding class name.
  377. String encName = "System.Text.ENC" + converted;
  378. // Look for a code page converter in this assembly.
  379. Assembly assembly = Assembly.GetExecutingAssembly ();
  380. Type type = assembly.GetType (encName);
  381. if (type != null) {
  382. return (Encoding)(Activator.CreateInstance (type));
  383. }
  384. // Look in any assembly, in case the application
  385. // has provided its own code page handler.
  386. type = Type.GetType (encName);
  387. if (type != null) {
  388. return (Encoding)(Activator.CreateInstance (type));
  389. }
  390. // We have no idea how to handle this encoding name.
  391. throw new NotSupportedException (String.Format ("Encoding name `{0}' not supported", name));
  392. }
  393. #endif // !ECMA_COMPAT
  394. // Get a hash code for this instance.
  395. public override int GetHashCode ()
  396. {
  397. return codePage;
  398. }
  399. // Get the maximum number of bytes needed to encode a
  400. // specified number of characters.
  401. public abstract int GetMaxByteCount (int charCount);
  402. // Get the maximum number of characters needed to decode a
  403. // specified number of bytes.
  404. public abstract int GetMaxCharCount (int byteCount);
  405. // Get the identifying preamble for this encoding.
  406. public virtual byte[] GetPreamble ()
  407. {
  408. return new byte [0];
  409. }
  410. // Decode a buffer of bytes into a string.
  411. public virtual String GetString (byte[] bytes, int index, int count)
  412. {
  413. return new String (GetChars(bytes, index, count));
  414. }
  415. public virtual String GetString (byte[] bytes)
  416. {
  417. return new String (GetChars(bytes));
  418. }
  419. #if !ECMA_COMPAT
  420. internal protected string body_name;
  421. internal protected string encoding_name;
  422. internal protected string header_name;
  423. internal protected bool is_mail_news_display;
  424. internal protected bool is_mail_news_save;
  425. internal protected bool is_browser_save = false;
  426. internal protected bool is_browser_display = false;
  427. internal protected string web_name;
  428. // Get the mail body name for this encoding.
  429. public virtual String BodyName
  430. {
  431. get {
  432. return body_name;
  433. }
  434. }
  435. // Get the code page represented by this object.
  436. public virtual int CodePage
  437. {
  438. get {
  439. return codePage;
  440. }
  441. }
  442. // Get the human-readable name for this encoding.
  443. public virtual String EncodingName
  444. {
  445. get {
  446. return encoding_name;
  447. }
  448. }
  449. // Get the mail agent header name for this encoding.
  450. public virtual String HeaderName
  451. {
  452. get {
  453. return header_name;
  454. }
  455. }
  456. // Determine if this encoding can be displayed in a Web browser.
  457. public virtual bool IsBrowserDisplay
  458. {
  459. get {
  460. return is_browser_display;
  461. }
  462. }
  463. // Determine if this encoding can be saved from a Web browser.
  464. public virtual bool IsBrowserSave
  465. {
  466. get {
  467. return is_browser_save;
  468. }
  469. }
  470. // Determine if this encoding can be displayed in a mail/news agent.
  471. public virtual bool IsMailNewsDisplay
  472. {
  473. get {
  474. return is_mail_news_display;
  475. }
  476. }
  477. // Determine if this encoding can be saved from a mail/news agent.
  478. public virtual bool IsMailNewsSave
  479. {
  480. get {
  481. return is_mail_news_save;
  482. }
  483. }
  484. // Get the IANA-preferred Web name for this encoding.
  485. public virtual String WebName
  486. {
  487. get {
  488. return web_name;
  489. }
  490. }
  491. // Get the Windows code page represented by this object.
  492. public virtual int WindowsCodePage
  493. {
  494. get {
  495. // We make no distinction between normal and
  496. // Windows code pages in this implementation.
  497. return windows_code_page;
  498. }
  499. }
  500. #endif // !ECMA_COMPAT
  501. // Storage for standard encoding objects.
  502. private static Encoding asciiEncoding = null;
  503. private static Encoding bigEndianEncoding = null;
  504. private static Encoding defaultEncoding = null;
  505. private static Encoding utf7Encoding = null;
  506. private static Encoding utf8EncodingWithMarkers = null;
  507. private static Encoding utf8EncodingWithoutMarkers = null;
  508. private static Encoding unicodeEncoding = null;
  509. private static Encoding isoLatin1Encoding = null;
  510. private static Encoding unixConsoleEncoding = null;
  511. // Get the standard ASCII encoding object.
  512. public static Encoding ASCII
  513. {
  514. get {
  515. if (asciiEncoding == null) {
  516. lock (typeof(Encoding)) {
  517. if (asciiEncoding == null) {
  518. asciiEncoding = new ASCIIEncoding ();
  519. }
  520. }
  521. }
  522. return asciiEncoding;
  523. }
  524. }
  525. // Get the standard big-endian Unicode encoding object.
  526. public static Encoding BigEndianUnicode
  527. {
  528. get {
  529. if (bigEndianEncoding == null) {
  530. lock (typeof(Encoding)) {
  531. if (bigEndianEncoding == null) {
  532. bigEndianEncoding = new UnicodeEncoding (true, true);
  533. }
  534. }
  535. }
  536. return bigEndianEncoding;
  537. }
  538. }
  539. [MethodImpl (MethodImplOptions.InternalCall)]
  540. extern internal static string InternalCodePage (ref int code_page);
  541. // Get the default encoding object.
  542. public static Encoding Default
  543. {
  544. get {
  545. if (defaultEncoding == null) {
  546. lock (typeof(Encoding)) {
  547. if (defaultEncoding == null) {
  548. // See if the underlying system knows what
  549. // code page handler we should be using.
  550. int code_page = 1;
  551. string code_page_name = InternalCodePage (ref code_page);
  552. try {
  553. if (code_page == -1)
  554. defaultEncoding = GetEncoding (code_page_name);
  555. else {
  556. // map the codepage from internal to our numbers
  557. code_page = code_page & 0x0fffffff;
  558. switch (code_page){
  559. case 1: code_page = ASCIIEncoding.ASCII_CODE_PAGE; break;
  560. case 2: code_page = UTF7Encoding.UTF7_CODE_PAGE; break;
  561. case 3: code_page = UTF8Encoding.UTF8_CODE_PAGE; break;
  562. case 4: code_page = UnicodeEncoding.UNICODE_CODE_PAGE; break;
  563. case 5: code_page = UnicodeEncoding.BIG_UNICODE_CODE_PAGE; break;
  564. case 6: code_page = Latin1Encoding.ISOLATIN_CODE_PAGE; break;
  565. }
  566. defaultEncoding = GetEncoding (code_page);
  567. }
  568. } catch (NotSupportedException) {
  569. defaultEncoding = UTF8Unmarked;
  570. }
  571. }
  572. }
  573. }
  574. return defaultEncoding;
  575. }
  576. }
  577. // Get the ISO Latin1 encoding object.
  578. private static Encoding ISOLatin1
  579. {
  580. get {
  581. if (isoLatin1Encoding == null) {
  582. lock (typeof(Encoding)) {
  583. if (isoLatin1Encoding == null) {
  584. isoLatin1Encoding = new Latin1Encoding ();
  585. }
  586. }
  587. }
  588. return isoLatin1Encoding;
  589. }
  590. }
  591. // Get the standard UTF-7 encoding object.
  592. #if ECMA_COMPAT
  593. private
  594. #else
  595. public
  596. #endif
  597. static Encoding UTF7
  598. {
  599. get {
  600. if (utf7Encoding == null) {
  601. lock (typeof(Encoding)) {
  602. if (utf7Encoding == null) {
  603. utf7Encoding = new UTF7Encoding ();
  604. }
  605. }
  606. }
  607. return utf7Encoding;
  608. }
  609. }
  610. // Get the standard UTF-8 encoding object.
  611. public static Encoding UTF8
  612. {
  613. get {
  614. if (utf8EncodingWithMarkers == null) {
  615. lock (typeof(Encoding)) {
  616. if (utf8EncodingWithMarkers == null) {
  617. utf8EncodingWithMarkers = new UTF8Encoding (true);
  618. }
  619. }
  620. }
  621. return utf8EncodingWithMarkers;
  622. }
  623. }
  624. //
  625. // Only internal, to be used by the class libraries: Unmarked and non-input-validating
  626. //
  627. internal static Encoding UTF8Unmarked {
  628. get {
  629. if (utf8EncodingWithoutMarkers == null) {
  630. lock (typeof (Encoding)){
  631. if (utf8EncodingWithoutMarkers == null){
  632. utf8EncodingWithoutMarkers = new UTF8Encoding (false, false);
  633. }
  634. }
  635. }
  636. return utf8EncodingWithoutMarkers;
  637. }
  638. }
  639. // Get the standard little-endian Unicode encoding object.
  640. public static Encoding Unicode
  641. {
  642. get {
  643. if (unicodeEncoding == null) {
  644. lock (typeof(Encoding)) {
  645. if (unicodeEncoding == null) {
  646. unicodeEncoding = new UnicodeEncoding (false, true);
  647. }
  648. }
  649. }
  650. return unicodeEncoding;
  651. }
  652. }
  653. // Forwarding decoder implementation.
  654. private sealed class ForwardingDecoder : Decoder
  655. {
  656. private Encoding encoding;
  657. // Constructor.
  658. public ForwardingDecoder (Encoding enc)
  659. {
  660. encoding = enc;
  661. }
  662. // Override inherited methods.
  663. public override int GetCharCount (byte[] bytes, int index, int count)
  664. {
  665. return encoding.GetCharCount (bytes, index, count);
  666. }
  667. public override int GetChars (byte[] bytes, int byteIndex,
  668. int byteCount, char[] chars,
  669. int charIndex)
  670. {
  671. return encoding.GetChars (bytes, byteIndex, byteCount, chars, charIndex);
  672. }
  673. } // class ForwardingDecoder
  674. // Forwarding encoder implementation.
  675. private sealed class ForwardingEncoder : Encoder
  676. {
  677. private Encoding encoding;
  678. // Constructor.
  679. public ForwardingEncoder (Encoding enc)
  680. {
  681. encoding = enc;
  682. }
  683. // Override inherited methods.
  684. public override int GetByteCount (char[] chars, int index, int count, bool flush)
  685. {
  686. return encoding.GetByteCount (chars, index, count);
  687. }
  688. public override int GetBytes (char[] chars, int charIndex,
  689. int charCount, byte[] bytes,
  690. int byteCount, bool flush)
  691. {
  692. return encoding.GetBytes (chars, charIndex, charCount, bytes, byteCount);
  693. }
  694. } // class ForwardingEncoder
  695. }; // class Encoding
  696. }; // namespace System.Text