Decoder.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /*
  2. * Decoder.cs - Implementation of the "System.Text.Decoder" class.
  3. *
  4. * Copyright (c) 2001 Southern Storm Software, Pty Ltd
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining
  7. * a copy of this software and associated documentation files (the "Software"),
  8. * to deal in the Software without restriction, including without limitation
  9. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  10. * and/or sell copies of the Software, and to permit persons to whom the
  11. * Software is furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included
  14. * in all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  17. * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  19. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
  20. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  21. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  22. * OTHER DEALINGS IN THE SOFTWARE.
  23. */
  24. namespace System.Text
  25. {
  26. using System;
  27. using System.Runtime.InteropServices;
  28. [Serializable]
  29. [ComVisible (true)]
  30. public abstract class Decoder
  31. {
  32. // Constructor.
  33. protected Decoder () {}
  34. DecoderFallback fallback = new DecoderReplacementFallback ();
  35. DecoderFallbackBuffer fallback_buffer;
  36. [ComVisible (false)]
  37. public DecoderFallback Fallback {
  38. get { return fallback; }
  39. set {
  40. if (value == null)
  41. throw new ArgumentNullException ();
  42. fallback = value;
  43. fallback_buffer = null;
  44. }
  45. }
  46. [ComVisible (false)]
  47. public DecoderFallbackBuffer FallbackBuffer {
  48. get {
  49. if (fallback_buffer == null)
  50. fallback_buffer = fallback.CreateFallbackBuffer ();
  51. return fallback_buffer;
  52. }
  53. }
  54. // Get the number of characters needed to decode a buffer.
  55. public abstract int GetCharCount (byte[] bytes, int index, int count);
  56. // Get the characters that result from decoding a buffer.
  57. public abstract int GetChars (byte[] bytes, int byteIndex, int byteCount,
  58. char[] chars, int charIndex);
  59. [ComVisible (false)]
  60. public virtual int GetCharCount (byte [] bytes, int index, int count, bool flush)
  61. {
  62. if (flush)
  63. Reset ();
  64. return GetCharCount (bytes, index, count);
  65. }
  66. [CLSCompliant (false)]
  67. [ComVisible (false)]
  68. public unsafe virtual int GetCharCount (byte* bytes, int count, bool flush)
  69. {
  70. if (bytes == null)
  71. throw new ArgumentNullException ("bytes");
  72. if (count < 0)
  73. throw new ArgumentOutOfRangeException ("count");
  74. byte [] barr = new byte [count];
  75. Marshal.Copy ((IntPtr) bytes, barr, 0, count);
  76. return GetCharCount (barr, 0, count, flush);
  77. }
  78. public virtual int GetChars (
  79. byte[] bytes, int byteIndex, int byteCount,
  80. char[] chars, int charIndex, bool flush)
  81. {
  82. CheckArguments (bytes, byteIndex, byteCount);
  83. CheckArguments (chars, charIndex);
  84. if (flush)
  85. Reset ();
  86. return GetChars (bytes, byteIndex, byteCount, chars, charIndex);
  87. }
  88. [CLSCompliant (false)]
  89. [ComVisible (false)]
  90. public unsafe virtual int GetChars (byte* bytes, int byteCount,
  91. char* chars, int charCount, bool flush)
  92. {
  93. CheckArguments (chars, charCount, bytes, byteCount);
  94. char [] carr = new char [charCount];
  95. Marshal.Copy ((IntPtr) chars, carr, 0, charCount);
  96. byte [] barr = new byte [byteCount];
  97. Marshal.Copy ((IntPtr) bytes, barr, 0, byteCount);
  98. return GetChars (barr, 0, byteCount, carr, 0, flush);
  99. }
  100. [ComVisible (false)]
  101. public virtual void Reset ()
  102. {
  103. if (fallback_buffer != null)
  104. fallback_buffer.Reset ();
  105. }
  106. [CLSCompliant (false)]
  107. [ComVisible (false)]
  108. public unsafe virtual void Convert (
  109. byte* bytes, int byteCount,
  110. char* chars, int charCount, bool flush,
  111. out int bytesUsed, out int charsUsed, out bool completed)
  112. {
  113. CheckArguments (chars, charCount, bytes, byteCount);
  114. bytesUsed = byteCount;
  115. while (true) {
  116. charsUsed = GetCharCount (bytes, bytesUsed, flush);
  117. if (charsUsed <= charCount)
  118. break;
  119. flush = false;
  120. bytesUsed >>= 1;
  121. }
  122. completed = bytesUsed == byteCount;
  123. charsUsed = GetChars (bytes, bytesUsed, chars, charCount, flush);
  124. }
  125. [ComVisible (false)]
  126. public virtual void Convert (
  127. byte [] bytes, int byteIndex, int byteCount,
  128. char [] chars, int charIndex, int charCount, bool flush,
  129. out int bytesUsed, out int charsUsed, out bool completed)
  130. {
  131. CheckArguments (bytes, byteIndex, byteCount);
  132. if (chars == null)
  133. throw new ArgumentNullException ("chars");
  134. if (charIndex < 0)
  135. throw new ArgumentOutOfRangeException ("charIndex");
  136. if (charCount < 0 || chars.Length < charIndex + charCount)
  137. throw new ArgumentOutOfRangeException ("charCount");
  138. bytesUsed = byteCount;
  139. while (true) {
  140. charsUsed = GetCharCount (bytes, byteIndex, bytesUsed, flush);
  141. if (charsUsed <= charCount)
  142. break;
  143. flush = false;
  144. bytesUsed >>= 1;
  145. }
  146. completed = bytesUsed == byteCount;
  147. charsUsed = GetChars (bytes, byteIndex, bytesUsed, chars, charIndex, flush);
  148. }
  149. void CheckArguments (char [] chars, int charIndex)
  150. {
  151. if (chars == null)
  152. throw new ArgumentNullException ("chars");
  153. if (charIndex < 0 || chars.Length < charIndex)
  154. throw new ArgumentOutOfRangeException ("charIndex");
  155. }
  156. void CheckArguments (byte [] bytes, int byteIndex, int byteCount)
  157. {
  158. if (bytes == null)
  159. throw new ArgumentNullException ("bytes");
  160. if (byteIndex < 0)
  161. throw new ArgumentOutOfRangeException ("byteIndex");
  162. if (byteCount < 0 || bytes.Length < byteIndex + byteCount)
  163. throw new ArgumentOutOfRangeException ("byteCount");
  164. }
  165. unsafe void CheckArguments (char* chars, int charCount, byte* bytes, int byteCount)
  166. {
  167. if (chars == null)
  168. throw new ArgumentNullException ("chars");
  169. if (bytes == null)
  170. throw new ArgumentNullException ("bytes");
  171. if (charCount < 0)
  172. throw new ArgumentOutOfRangeException ("charCount");
  173. if (byteCount < 0)
  174. throw new ArgumentOutOfRangeException ("byteCount");
  175. }
  176. }; // class Decoder
  177. }; // namespace System.Text