Encoder.cs 5.1 KB

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