Encoding.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767
  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. #if false
  310. // Build a code page class name.
  311. String cpName = "System.Text.CP" + codePage.ToString ();
  312. // Look for a code page converter in this assembly.
  313. Assembly assembly = Assembly.GetExecutingAssembly ();
  314. Type type = assembly.GetType (cpName);
  315. if (type != null) {
  316. return (Encoding)(Activator.CreateInstance (type));
  317. }
  318. // Look in any assembly, in case the application
  319. // has provided its own code page handler.
  320. type = Type.GetType (cpName);
  321. if (type != null) {
  322. return (Encoding)(Activator.CreateInstance (type));
  323. }
  324. #endif
  325. // We have no idea how to handle this code page.
  326. throw new NotSupportedException
  327. (String.Format (_("NotSupp_CodePage"), codePage.ToString ()));
  328. }
  329. #if !ECMA_COMPAT
  330. // Table of builtin web encoding names and the corresponding code pages.
  331. private static readonly String[] encodingNames =
  332. {"ascii", "us-ascii", "utf-7", "utf-8", "utf-16",
  333. "unicodeFFFE", "iso-8859-1"};
  334. private static readonly int[] encodingCodePages =
  335. {ASCIIEncoding.ASCII_CODE_PAGE,
  336. ASCIIEncoding.ASCII_CODE_PAGE,
  337. UTF7Encoding.UTF7_CODE_PAGE,
  338. UTF8Encoding.UTF8_CODE_PAGE,
  339. UnicodeEncoding.UNICODE_CODE_PAGE,
  340. UnicodeEncoding.BIG_UNICODE_CODE_PAGE,
  341. Latin1Encoding.ISOLATIN_CODE_PAGE};
  342. // Get an encoding object for a specific web encoding name.
  343. public static Encoding GetEncoding (String name)
  344. {
  345. // Validate the parameters.
  346. if (name == null) {
  347. throw new ArgumentNullException ("name");
  348. }
  349. // Search the table for a name match.
  350. int posn;
  351. for (posn = 0; posn < encodingNames.Length; ++posn) {
  352. if (String.Compare(name, encodingNames[posn], true,
  353. CultureInfo.InvariantCulture) == 0) {
  354. return GetEncoding (encodingCodePages[posn]);
  355. }
  356. }
  357. // Try to obtain a web encoding handler from the I18N handler.
  358. Encoding enc = (Encoding)(InvokeI18N ("GetEncoding", name));
  359. if (enc != null) {
  360. return enc;
  361. }
  362. #if false
  363. // Build a web encoding class name.
  364. String encName = "System.Text.ENC" +
  365. name.ToLower (CultureInfo.InvariantCulture)
  366. .Replace ('-', '_');
  367. // Look for a code page converter in this assembly.
  368. Assembly assembly = Assembly.GetExecutingAssembly ();
  369. Type type = assembly.GetType (encName);
  370. if (type != null) {
  371. return (Encoding)(Activator.CreateInstance (type));
  372. }
  373. // Look in any assembly, in case the application
  374. // has provided its own code page handler.
  375. type = Type.GetType (encName);
  376. if (type != null) {
  377. return (Encoding)(Activator.CreateInstance (type));
  378. }
  379. #endif
  380. // We have no idea how to handle this encoding name.
  381. throw new NotSupportedException (String.Format (_("NotSupp_EncodingName"), name));
  382. }
  383. #endif // !ECMA_COMPAT
  384. // Get a hash code for this instance.
  385. public override int GetHashCode ()
  386. {
  387. return codePage;
  388. }
  389. // Get the maximum number of bytes needed to encode a
  390. // specified number of characters.
  391. public abstract int GetMaxByteCount (int charCount);
  392. // Get the maximum number of characters needed to decode a
  393. // specified number of bytes.
  394. public abstract int GetMaxCharCount (int byteCount);
  395. // Get the identifying preamble for this encoding.
  396. public virtual byte[] GetPreamble ()
  397. {
  398. return new byte [0];
  399. }
  400. // Decode a buffer of bytes into a string.
  401. public virtual String GetString (byte[] bytes, int index, int count)
  402. {
  403. return new String (GetChars(bytes, index, count));
  404. }
  405. public virtual String GetString (byte[] bytes)
  406. {
  407. return new String (GetChars(bytes));
  408. }
  409. #if !ECMA_COMPAT
  410. // Get the mail body name for this encoding.
  411. public virtual String BodyName
  412. {
  413. get {
  414. return null;
  415. }
  416. }
  417. // Get the code page represented by this object.
  418. public virtual int CodePage
  419. {
  420. get {
  421. return codePage;
  422. }
  423. }
  424. // Get the human-readable name for this encoding.
  425. public virtual String EncodingName
  426. {
  427. get {
  428. return null;
  429. }
  430. }
  431. // Get the mail agent header name for this encoding.
  432. public virtual String HeaderName
  433. {
  434. get {
  435. return null;
  436. }
  437. }
  438. // Determine if this encoding can be displayed in a Web browser.
  439. public virtual bool IsBrowserDisplay
  440. {
  441. get {
  442. return false;
  443. }
  444. }
  445. // Determine if this encoding can be saved from a Web browser.
  446. public virtual bool IsBrowserSave
  447. {
  448. get {
  449. return false;
  450. }
  451. }
  452. // Determine if this encoding can be displayed in a mail/news agent.
  453. public virtual bool IsMailNewsDisplay
  454. {
  455. get {
  456. return false;
  457. }
  458. }
  459. // Determine if this encoding can be saved from a mail/news agent.
  460. public virtual bool IsMailNewsSave
  461. {
  462. get {
  463. return false;
  464. }
  465. }
  466. // Get the IANA-preferred Web name for this encoding.
  467. public virtual String WebName
  468. {
  469. get {
  470. return null;
  471. }
  472. }
  473. // Get the Windows code page represented by this object.
  474. public virtual int WindowsCodePage
  475. {
  476. get {
  477. // We make no distinction between normal and
  478. // Windows code pages in this implementation.
  479. return codePage;
  480. }
  481. }
  482. #endif // !ECMA_COMPAT
  483. // Storage for standard encoding objects.
  484. private static Encoding asciiEncoding = null;
  485. private static Encoding bigEndianEncoding = null;
  486. private static Encoding defaultEncoding = null;
  487. private static Encoding utf7Encoding = null;
  488. private static Encoding utf8EncodingWithMarkers = null;
  489. private static Encoding utf8EncodingWithoutMarkers = null;
  490. private static Encoding unicodeEncoding = null;
  491. private static Encoding isoLatin1Encoding = null;
  492. private static Encoding unixConsoleEncoding = null;
  493. // Get the standard ASCII encoding object.
  494. public static Encoding ASCII
  495. {
  496. get {
  497. if (asciiEncoding == null) {
  498. lock (typeof(Encoding)) {
  499. if (asciiEncoding == null) {
  500. asciiEncoding = new ASCIIEncoding ();
  501. }
  502. }
  503. }
  504. return asciiEncoding;
  505. }
  506. }
  507. // Get the standard big-endian Unicode encoding object.
  508. public static Encoding BigEndianUnicode
  509. {
  510. get {
  511. if (bigEndianEncoding == null) {
  512. lock (typeof(Encoding)) {
  513. if (bigEndianEncoding == null) {
  514. bigEndianEncoding = new UnicodeEncoding (true, true);
  515. }
  516. }
  517. }
  518. return bigEndianEncoding;
  519. }
  520. }
  521. [MethodImpl (MethodImplOptions.InternalCall)]
  522. extern private static string InternalCodePage ();
  523. // Get the default encoding object.
  524. public static Encoding Default
  525. {
  526. get {
  527. if (defaultEncoding == null) {
  528. lock (typeof(Encoding)) {
  529. if (defaultEncoding == null) {
  530. // See if the underlying system knows what
  531. // code page handler we should be using.
  532. string codePage = InternalCodePage ();
  533. try {
  534. defaultEncoding = GetEncoding (codePage);
  535. } catch (NotSupportedException) {
  536. defaultEncoding = UTF8Unmarked;
  537. }
  538. }
  539. }
  540. }
  541. return defaultEncoding;
  542. }
  543. }
  544. // Get the ISO Latin1 encoding object.
  545. private static Encoding ISOLatin1
  546. {
  547. get {
  548. if (isoLatin1Encoding == null) {
  549. lock (typeof(Encoding)) {
  550. if (isoLatin1Encoding == null) {
  551. isoLatin1Encoding = new Latin1Encoding ();
  552. }
  553. }
  554. }
  555. return isoLatin1Encoding;
  556. }
  557. }
  558. // Get the standard UTF-7 encoding object.
  559. #if ECMA_COMPAT
  560. private
  561. #else
  562. public
  563. #endif
  564. static Encoding UTF7
  565. {
  566. get {
  567. if (utf7Encoding == null) {
  568. lock (typeof(Encoding)) {
  569. if (utf7Encoding == null) {
  570. utf7Encoding = new UTF7Encoding ();
  571. }
  572. }
  573. }
  574. return utf7Encoding;
  575. }
  576. }
  577. // Get the standard UTF-8 encoding object.
  578. public static Encoding UTF8
  579. {
  580. get {
  581. if (utf8EncodingWithMarkers == null) {
  582. lock (typeof(Encoding)) {
  583. if (utf8EncodingWithMarkers == null) {
  584. utf8EncodingWithMarkers = new UTF8Encoding (true);
  585. }
  586. }
  587. }
  588. return utf8EncodingWithMarkers;
  589. }
  590. }
  591. //
  592. // Only internal, to be used by the class libraries
  593. //
  594. internal static Encoding UTF8Unmarked {
  595. get {
  596. if (utf8EncodingWithoutMarkers == null) {
  597. lock (typeof (Encoding)){
  598. if (utf8EncodingWithoutMarkers == null){
  599. utf8EncodingWithoutMarkers = new UTF8Encoding (false, true);
  600. }
  601. }
  602. }
  603. return utf8EncodingWithoutMarkers;
  604. }
  605. }
  606. // Get the standard little-endian Unicode encoding object.
  607. public static Encoding Unicode
  608. {
  609. get {
  610. if (unicodeEncoding == null) {
  611. lock (typeof(Encoding)) {
  612. if (unicodeEncoding == null) {
  613. unicodeEncoding = new UnicodeEncoding (false, true);
  614. }
  615. }
  616. }
  617. return unicodeEncoding;
  618. }
  619. }
  620. internal static Encoding UnixConsoleEncoding {
  621. get {
  622. if (unixConsoleEncoding == null) {
  623. lock (typeof(Encoding)){
  624. if (unixConsoleEncoding == null)
  625. unixConsoleEncoding = Default;
  626. }
  627. }
  628. return unixConsoleEncoding;
  629. }
  630. }
  631. // Forwarding decoder implementation.
  632. private sealed class ForwardingDecoder : Decoder
  633. {
  634. private Encoding encoding;
  635. // Constructor.
  636. public ForwardingDecoder (Encoding enc)
  637. {
  638. encoding = enc;
  639. }
  640. // Override inherited methods.
  641. public override int GetCharCount (byte[] bytes, int index, int count)
  642. {
  643. return encoding.GetCharCount (bytes, index, count);
  644. }
  645. public override int GetChars (byte[] bytes, int byteIndex,
  646. int byteCount, char[] chars,
  647. int charIndex)
  648. {
  649. return encoding.GetChars (bytes, byteIndex, byteCount, chars, charIndex);
  650. }
  651. } // class ForwardingDecoder
  652. // Forwarding encoder implementation.
  653. private sealed class ForwardingEncoder : Encoder
  654. {
  655. private Encoding encoding;
  656. // Constructor.
  657. public ForwardingEncoder (Encoding enc)
  658. {
  659. encoding = enc;
  660. }
  661. // Override inherited methods.
  662. public override int GetByteCount (char[] chars, int index, int count, bool flush)
  663. {
  664. return encoding.GetByteCount (chars, index, count);
  665. }
  666. public override int GetBytes (char[] chars, int charIndex,
  667. int charCount, byte[] bytes,
  668. int byteCount, bool flush)
  669. {
  670. return encoding.GetBytes (chars, charIndex, charCount, bytes, byteCount);
  671. }
  672. } // class ForwardingEncoder
  673. }; // class Encoding
  674. }; // namespace System.Text