Encoding.cs 19 KB

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