Decoder.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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. #if NET_2_0
  30. [ComVisible (true)]
  31. #endif
  32. public abstract class Decoder
  33. {
  34. // Constructor.
  35. protected Decoder () {}
  36. #if NET_2_0
  37. DecoderFallback fallback = new DecoderReplacementFallback ();
  38. DecoderFallbackBuffer fallback_buffer;
  39. [ComVisible (false)]
  40. public DecoderFallback Fallback {
  41. get { return fallback; }
  42. set {
  43. if (value == null)
  44. throw new ArgumentNullException ();
  45. fallback = value;
  46. fallback_buffer = null;
  47. }
  48. }
  49. [ComVisible (false)]
  50. public DecoderFallbackBuffer FallbackBuffer {
  51. get {
  52. if (fallback_buffer == null)
  53. fallback_buffer = fallback.CreateFallbackBuffer ();
  54. return fallback_buffer;
  55. }
  56. }
  57. #endif
  58. // Get the number of characters needed to decode a buffer.
  59. public abstract int GetCharCount (byte[] bytes, int index, int count);
  60. // Get the characters that result from decoding a buffer.
  61. public abstract int GetChars (byte[] bytes, int byteIndex, int byteCount,
  62. char[] chars, int charIndex);
  63. #if NET_2_0
  64. [ComVisible (false)]
  65. public virtual int GetCharCount (byte [] bytes, int index, int count, bool flush)
  66. {
  67. if (flush)
  68. Reset ();
  69. return GetCharCount (bytes, index, count);
  70. }
  71. [CLSCompliant (false)]
  72. [ComVisible (false)]
  73. public unsafe virtual int GetCharCount (byte* bytes, int count, bool flush)
  74. {
  75. if (bytes == null)
  76. throw new ArgumentNullException ("bytes");
  77. if (count < 0)
  78. throw new ArgumentOutOfRangeException ("count");
  79. byte [] barr = new byte [count];
  80. Marshal.Copy ((IntPtr) bytes, barr, 0, count);
  81. return GetCharCount (barr, 0, count, flush);
  82. }
  83. public virtual int GetChars (
  84. byte[] bytes, int byteIndex, int byteCount,
  85. char[] chars, int charIndex, bool flush)
  86. {
  87. CheckArguments (bytes, byteIndex, byteCount);
  88. CheckArguments (chars, charIndex);
  89. if (flush)
  90. Reset ();
  91. return GetChars (bytes, byteIndex, byteCount, chars, charIndex);
  92. }
  93. [CLSCompliant (false)]
  94. [ComVisible (false)]
  95. public unsafe virtual int GetChars (byte* bytes, int byteCount,
  96. char* chars, int charCount, bool flush)
  97. {
  98. CheckArguments (chars, charCount, bytes, byteCount);
  99. char [] carr = new char [charCount];
  100. Marshal.Copy ((IntPtr) chars, carr, 0, charCount);
  101. byte [] barr = new byte [byteCount];
  102. Marshal.Copy ((IntPtr) bytes, barr, 0, byteCount);
  103. return GetChars (barr, 0, byteCount, carr, 0, flush);
  104. }
  105. [ComVisible (false)]
  106. public virtual void Reset ()
  107. {
  108. if (fallback_buffer != null)
  109. fallback_buffer.Reset ();
  110. }
  111. [CLSCompliant (false)]
  112. [ComVisible (false)]
  113. public unsafe virtual void Convert (
  114. byte* bytes, int byteCount,
  115. char* chars, int charCount, bool flush,
  116. out int bytesUsed, out int charsUsed, out bool completed)
  117. {
  118. CheckArguments (chars, charCount, bytes, byteCount);
  119. bytesUsed = byteCount;
  120. while (true) {
  121. charsUsed = GetCharCount (bytes, bytesUsed, flush);
  122. if (charsUsed <= charCount)
  123. break;
  124. flush = false;
  125. bytesUsed >>= 1;
  126. }
  127. completed = bytesUsed == byteCount;
  128. charsUsed = GetChars (bytes, bytesUsed, chars, charCount, flush);
  129. }
  130. [ComVisible (false)]
  131. public virtual void Convert (
  132. byte [] bytes, int byteIndex, int byteCount,
  133. char [] chars, int charIndex, int charCount, bool flush,
  134. out int bytesUsed, out int charsUsed, out bool completed)
  135. {
  136. CheckArguments (bytes, byteIndex, byteCount);
  137. CheckArguments (chars, charIndex);
  138. if (charCount < 0 || chars.Length < charIndex + charCount)
  139. throw new ArgumentOutOfRangeException ("charCount");
  140. bytesUsed = byteCount;
  141. while (true) {
  142. charsUsed = GetCharCount (bytes, byteIndex, bytesUsed, flush);
  143. if (charsUsed <= charCount)
  144. break;
  145. flush = false;
  146. bytesUsed >>= 1;
  147. }
  148. completed = bytesUsed == byteCount;
  149. charsUsed = GetChars (bytes, byteIndex, bytesUsed, chars, charIndex, flush);
  150. }
  151. void CheckArguments (char [] chars, int charIndex)
  152. {
  153. if (chars == null)
  154. throw new ArgumentNullException ("chars");
  155. if (charIndex < 0 || chars.Length <= charIndex)
  156. throw new ArgumentOutOfRangeException ("charIndex");
  157. }
  158. void CheckArguments (byte [] bytes, int byteIndex, int byteCount)
  159. {
  160. if (bytes == null)
  161. throw new ArgumentNullException ("bytes");
  162. if (byteIndex < 0 || bytes.Length <= byteIndex)
  163. throw new ArgumentOutOfRangeException ("byteIndex");
  164. if (byteCount < 0 || bytes.Length < byteIndex + byteCount)
  165. throw new ArgumentOutOfRangeException ("byteCount");
  166. }
  167. unsafe void CheckArguments (char* chars, int charCount, byte* bytes, int byteCount)
  168. {
  169. if (chars == null)
  170. throw new ArgumentNullException ("chars");
  171. if (bytes == null)
  172. throw new ArgumentNullException ("bytes");
  173. if (charCount < 0)
  174. throw new ArgumentOutOfRangeException ("charCount");
  175. if (byteCount < 0)
  176. throw new ArgumentOutOfRangeException ("byteCount");
  177. }
  178. #endif
  179. }; // class Decoder
  180. }; // namespace System.Text