Encoder.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /*
  2. * Encoder.cs - Implementation of the "System.Text.Encoder" 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 Encoder
  33. {
  34. // Constructor.
  35. protected Encoder() {}
  36. #if NET_2_0
  37. EncoderFallback fallback = new EncoderReplacementFallback ();
  38. EncoderFallbackBuffer fallback_buffer;
  39. [ComVisible (false)]
  40. public EncoderFallback 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 EncoderFallbackBuffer 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 bytes needed to encode a buffer.
  59. public abstract int GetByteCount(char[] chars, int index,
  60. int count, bool flush);
  61. // Get the bytes that result from decoding a buffer.
  62. public abstract int GetBytes(char[] chars, int charIndex, int charCount,
  63. byte[] bytes, int byteIndex, bool flush);
  64. #if NET_2_0
  65. [CLSCompliant (false)]
  66. [ComVisible (false)]
  67. public unsafe virtual int GetByteCount (char* chars, int charCount, bool flush)
  68. {
  69. if (chars == null)
  70. throw new ArgumentNullException ("chars");
  71. if (charCount < 0)
  72. throw new ArgumentOutOfRangeException ("charCount");
  73. char [] carr = new char [charCount];
  74. Marshal.Copy ((IntPtr) chars, carr, 0, charCount);
  75. return GetByteCount (carr, 0, charCount, flush);
  76. }
  77. [CLSCompliant (false)]
  78. [ComVisible (false)]
  79. public unsafe virtual int GetBytes (char* chars, int charCount,
  80. byte* bytes, int byteCount, bool flush)
  81. {
  82. CheckArguments (chars, charCount, bytes, byteCount);
  83. char [] carr = new char [charCount];
  84. Marshal.Copy ((IntPtr) chars, carr, 0, charCount);
  85. byte [] barr = new byte [byteCount];
  86. Marshal.Copy ((IntPtr) bytes, barr, 0, byteCount);
  87. return GetBytes (carr, 0, charCount, barr, 0, flush);
  88. }
  89. [ComVisible (false)]
  90. public virtual void Reset ()
  91. {
  92. if (fallback_buffer != null)
  93. fallback_buffer.Reset ();
  94. }
  95. [CLSCompliant (false)]
  96. [ComVisible (false)]
  97. public unsafe virtual void Convert (
  98. char* chars, int charCount,
  99. byte* bytes, int byteCount, bool flush,
  100. out int charsUsed, out int bytesUsed, out bool completed)
  101. {
  102. CheckArguments (chars, charCount, bytes, byteCount);
  103. charsUsed = charCount;
  104. while (true) {
  105. bytesUsed = GetByteCount (chars, charsUsed, flush);
  106. if (bytesUsed <= byteCount)
  107. break;
  108. flush = false;
  109. charsUsed >>= 1;
  110. }
  111. completed = charsUsed == charCount;
  112. bytesUsed = GetBytes (chars, charsUsed, bytes, byteCount, flush);
  113. }
  114. [ComVisible (false)]
  115. public virtual void Convert (
  116. char [] chars, int charIndex, int charCount,
  117. byte [] bytes, int byteIndex, int byteCount, bool flush,
  118. out int charsUsed, out int bytesUsed, out bool completed)
  119. {
  120. if (chars == null)
  121. throw new ArgumentNullException ("chars");
  122. if (bytes == null)
  123. throw new ArgumentNullException ("bytes");
  124. if (charIndex < 0 || chars.Length <= charIndex)
  125. throw new ArgumentOutOfRangeException ("charIndex");
  126. if (charCount < 0 || chars.Length < charIndex + charCount)
  127. throw new ArgumentOutOfRangeException ("charCount");
  128. if (byteIndex < 0 || bytes.Length <= byteIndex)
  129. throw new ArgumentOutOfRangeException ("byteIndex");
  130. if (byteCount < 0 || bytes.Length < byteIndex + byteCount)
  131. throw new ArgumentOutOfRangeException ("byteCount");
  132. charsUsed = charCount;
  133. while (true) {
  134. bytesUsed = GetByteCount (chars, charIndex, charsUsed, flush);
  135. if (bytesUsed <= byteCount)
  136. break;
  137. flush = false;
  138. charsUsed >>= 1;
  139. }
  140. completed = charsUsed == charCount;
  141. bytesUsed = GetBytes (chars, charIndex, charsUsed, bytes, byteIndex, flush);
  142. }
  143. unsafe void CheckArguments (char* chars, int charCount, byte* bytes, int byteCount)
  144. {
  145. if (chars == null)
  146. throw new ArgumentNullException ("chars");
  147. if (bytes == null)
  148. throw new ArgumentNullException ("bytes");
  149. if (charCount < 0)
  150. throw new ArgumentOutOfRangeException ("charCount");
  151. if (byteCount < 0)
  152. throw new ArgumentOutOfRangeException ("byteCount");
  153. }
  154. #endif
  155. }; // class Encoder
  156. }; // namespace System.Text