The Labs \ Source Viewer \ SSCLI \ System.Text \ Encoder

  1. // ==++==
  2. //
  3. //
  4. // Copyright (c) 2006 Microsoft Corporation. All rights reserved.
  5. //
  6. // The use and distribution terms for this software are contained in the file
  7. // named license.txt, which can be found in the root of this distribution.
  8. // By using this software in any fashion, you are agreeing to be bound by the
  9. // terms of this license.
  10. //
  11. // You must not remove this notice, or any other, from this software.
  12. //
  13. //
  14. // ==--==
  15. namespace System.Text
  16. {
  17.     using System.Runtime.Serialization;
  18.     using System.Text;
  19.     using System;
  20.     // An Encoder is used to encode a sequence of blocks of characters into
  21.     // a sequence of blocks of bytes. Following instantiation of an encoder,
  22.     // sequential blocks of characters are converted into blocks of bytes through
  23.     // calls to the GetBytes method. The encoder maintains state between the
  24.     // conversions, allowing it to correctly encode character sequences that span
  25.     // adjacent blocks.
  26.     //
  27.     // Instances of specific implementations of the Encoder abstract base
  28.     // class are typically obtained through calls to the GetEncoder method
  29.     // of Encoding objects.
  30.     //
  31.     [System.Runtime.InteropServices.ComVisible(true)]
  32.     [Serializable()]
  33.     public abstract class Encoder
  34.     {
  35.         internal EncoderFallback m_fallback = null;
  36.        
  37.         [NonSerialized()]
  38.         internal EncoderFallbackBuffer m_fallbackBuffer = null;
  39.        
  40.         internal void SerializeEncoder(SerializationInfo info)
  41.         {
  42.             info.AddValue("m_fallback", this.m_fallback);
  43.         }
  44.        
  45.         protected Encoder()
  46.         {
  47.             // We don't call default reset because default reset probably isn't good if we aren't initialized.
  48.         }
  49.        
  50.         [System.Runtime.InteropServices.ComVisible(false)]
  51.         public EncoderFallback Fallback {
  52.             get { return m_fallback; }
  53.            
  54.             set {
  55.                 if (value == null)
  56.                     throw new ArgumentNullException("value");
  57.                
  58.                 if (m_fallbackBuffer != null && m_fallbackBuffer.Remaining > 0)
  59.                     throw new ArgumentException(Environment.GetResourceString("Argument_FallbackBufferNotEmpty"), "value");
  60.                
  61.                 m_fallback = value;
  62.                 m_fallbackBuffer = null;
  63.             }
  64.         }
  65.        
  66.         [System.Runtime.InteropServices.ComVisible(false)]
  67.         public EncoderFallbackBuffer FallbackBuffer {
  68.             get {
  69.                 if (m_fallbackBuffer == null) {
  70.                     if (m_fallback != null)
  71.                         m_fallbackBuffer = m_fallback.CreateFallbackBuffer();
  72.                     else
  73.                         m_fallbackBuffer = EncoderFallback.ReplacementFallback.CreateFallbackBuffer();
  74.                 }
  75.                
  76.                 return m_fallbackBuffer;
  77.             }
  78.         }
  79.        
  80.         internal bool InternalHasFallbackBuffer {
  81.             get { return m_fallbackBuffer != null; }
  82.         }
  83.        
  84.         // Reset the Encoder
  85.         //
  86.         // Normally if we call GetBytes() and an error is thrown we don't change the state of the encoder. This
  87.         // would allow the caller to correct the error condition and try again (such as if they need a bigger buffer.)
  88.         //
  89.         // If the caller doesn't want to try again after GetBytes() throws an error, then they need to call Reset().
  90.         //
  91.         // Virtual implimentation has to call GetBytes with flush and a big enough buffer to clear a 0 char string
  92.         // We avoid GetMaxByteCount() because a) we can't call the base encoder and b) it might be really big.
  93.         [System.Runtime.InteropServices.ComVisible(false)]
  94.         public virtual void Reset()
  95.         {
  96.             char[] charTemp = {};
  97.             byte[] byteTemp = new byte[GetByteCount(charTemp, 0, 0, true)];
  98.             GetBytes(charTemp, 0, 0, byteTemp, 0, true);
  99.             if (m_fallbackBuffer != null)
  100.                 m_fallbackBuffer.Reset();
  101.         }
  102.        
  103.         // Returns the number of bytes the next call to GetBytes will
  104.         // produce if presented with the given range of characters and the given
  105.         // value of the flush parameter. The returned value takes into
  106.         // account the state in which the encoder was left following the last call
  107.         // to GetBytes. The state of the encoder is not affected by a call
  108.         // to this method.
  109.         //
  110.         public abstract int GetByteCount(char[] chars, int index, int count, bool flush);
  111.        
  112.         [CLSCompliant(false)]
  113.         [System.Runtime.InteropServices.ComVisible(false)]
  114.         unsafe public virtual int GetByteCount(char* chars, int count, bool flush)
  115.         {
  116.             // Validate input parameters
  117.             if (chars == null)
  118.                 throw new ArgumentNullException("chars", Environment.GetResourceString("ArgumentNull_Array"));
  119.            
  120.             if (count < 0)
  121.                 throw new ArgumentOutOfRangeException("count", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
  122.            
  123.             char[] arrChar = new char[count];
  124.             int index;
  125.            
  126.             for (index = 0; index < count; index++)
  127.                 arrChar[index] = chars[index];
  128.            
  129.             return GetByteCount(arrChar, 0, count, flush);
  130.         }
  131.        
  132.         // Encodes a range of characters in a character array into a range of bytes
  133.         // in a byte array. The method encodes charCount characters from
  134.         // chars starting at index charIndex, storing the resulting
  135.         // bytes in bytes starting at index byteIndex. The encoding
  136.         // takes into account the state in which the encoder was left following the
  137.         // last call to this method. The flush parameter indicates whether
  138.         // the encoder should flush any shift-states and partial characters at the
  139.         // end of the conversion. To ensure correct termination of a sequence of
  140.         // blocks of encoded bytes, the last call to GetBytes should specify
  141.         // a value of true for the flush parameter.
  142.         //
  143.         // An exception occurs if the byte array is not large enough to hold the
  144.         // complete encoding of the characters. The GetByteCount method can
  145.         // be used to determine the exact number of bytes that will be produced for
  146.         // a given range of characters. Alternatively, the GetMaxByteCount
  147.         // method of the Encoding that produced this encoder can be used to
  148.         // determine the maximum number of bytes that will be produced for a given
  149.         // number of characters, regardless of the actual character values.
  150.         //
  151.         public abstract int GetBytes(char[] chars, int charIndex, int charCount, byte[] bytes, int byteIndex, bool flush);
  152.        
  153.         //
  154.         //
  155.         //
  156.         [CLSCompliant(false)]
  157.         [System.Runtime.InteropServices.ComVisible(false)]
  158.         unsafe public virtual int GetBytes(char* chars, int charCount, byte* bytes, int byteCount, bool flush)
  159.         {
  160.             // Validate input parameters
  161.             if (bytes == null || chars == null)
  162.                 throw new ArgumentNullException(bytes == null ? "bytes" : "chars", Environment.GetResourceString("ArgumentNull_Array"));
  163.            
  164.             if (charCount < 0 || byteCount < 0)
  165.                 throw new ArgumentOutOfRangeException((charCount < 0 ? "charCount" : "byteCount"), Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
  166.            
  167.             // Get the char array to convert
  168.             char[] arrChar = new char[charCount];
  169.            
  170.             int index;
  171.             for (index = 0; index < charCount; index++)
  172.                 arrChar[index] = chars[index];
  173.            
  174.             // Get the byte array to fill
  175.             byte[] arrByte = new byte[byteCount];
  176.            
  177.             // Do the work
  178.             int result = GetBytes(arrChar, 0, charCount, arrByte, 0, flush);
  179.            
  180.             // The only way this could fail is a bug in GetBytes
  181.             BCLDebug.Assert(result <= byteCount, "Returned more bytes than we have space for");
  182.            
  183.             // Copy the byte array
  184.             // WARNING: We MUST make sure that we don't copy too many bytes. We can't
  185.             // rely on result because it could be a 3rd party implimentation. We need
  186.             // to make sure we never copy more than byteCount bytes no matter the value
  187.             // of result
  188.             if (result < byteCount)
  189.                 byteCount = result;
  190.            
  191.             // Don't copy too many bytes!
  192.             for (index = 0; index < byteCount; index++)
  193.                 bytes[index] = arrByte[index];
  194.            
  195.             return byteCount;
  196.         }
  197.        
  198.         //
  199.         //
  200.         [System.Runtime.InteropServices.ComVisible(false)]
  201.         public virtual void Convert(char[] chars, int charIndex, int charCount, byte[] bytes, int byteIndex, int byteCount, bool flush, out int charsUsed, out int bytesUsed, out bool completed
  202.         )
  203.         {
  204.             // Validate parameters
  205.             if (chars == null || bytes == null)
  206.                 throw new ArgumentNullException((chars == null ? "chars" : "bytes"), Environment.GetResourceString("ArgumentNull_Array"));
  207.            
  208.             if (charIndex < 0 || charCount < 0)
  209.                 throw new ArgumentOutOfRangeException((charIndex < 0 ? "charIndex" : "charCount"), Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
  210.            
  211.             if (byteIndex < 0 || byteCount < 0)
  212.                 throw new ArgumentOutOfRangeException((byteIndex < 0 ? "byteIndex" : "byteCount"), Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
  213.            
  214.             if (chars.Length - charIndex < charCount)
  215.                 throw new ArgumentOutOfRangeException("chars", Environment.GetResourceString("ArgumentOutOfRange_IndexCountBuffer"));
  216.            
  217.             if (bytes.Length - byteIndex < byteCount)
  218.                 throw new ArgumentOutOfRangeException("bytes", Environment.GetResourceString("ArgumentOutOfRange_IndexCountBuffer"));
  219.            
  220.             charsUsed = charCount;
  221.            
  222.             // Its easy to do if it won't overrun our buffer.
  223.             // Note: We don't want to call unsafe version because that might be an untrusted version
  224.             // which could be really unsafe and we don't want to mix it up.
  225.             while (charsUsed > 0) {
  226.                 if (GetByteCount(chars, charIndex, charsUsed, flush) <= byteCount) {
  227.                     bytesUsed = GetBytes(chars, charIndex, charsUsed, bytes, byteIndex, flush);
  228.                     completed = (charsUsed == charCount && (m_fallbackBuffer == null || m_fallbackBuffer.Remaining == 0));
  229.                     return;
  230.                 }
  231.                
  232.                 // Try again with 1/2 the count, won't flush then 'cause won't read it all
  233.                 flush = false;
  234.                 charsUsed /= 2;
  235.             }
  236.            
  237.             // Oops, we didn't have anything, we'll have to throw an overflow
  238.             throw new ArgumentException(Environment.GetResourceString("Argument_ConversionOverflow"));
  239.         }
  240.        
  241.         //
  242.         //
  243.         [CLSCompliant(false)]
  244.         [System.Runtime.InteropServices.ComVisible(false)]
  245.         unsafe public virtual void Convert(char* chars, int charCount, byte* bytes, int byteCount, bool flush, out int charsUsed, out int bytesUsed, out bool completed)
  246.         {
  247.             // Validate input parameters
  248.             if (bytes == null || chars == null)
  249.                 throw new ArgumentNullException(bytes == null ? "bytes" : "chars", Environment.GetResourceString("ArgumentNull_Array"));
  250.             if (charCount < 0 || byteCount < 0)
  251.                 throw new ArgumentOutOfRangeException((charCount < 0 ? "charCount" : "byteCount"), Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
  252.            
  253.             // Get ready to do it
  254.             charsUsed = charCount;
  255.            
  256.             // Its easy to do if it won't overrun our buffer.
  257.             while (charsUsed > 0) {
  258.                 if (GetByteCount(chars, charsUsed, flush) <= byteCount) {
  259.                     bytesUsed = GetBytes(chars, charsUsed, bytes, byteCount, flush);
  260.                     completed = (charsUsed == charCount && (m_fallbackBuffer == null || m_fallbackBuffer.Remaining == 0));
  261.                     return;
  262.                 }
  263.                
  264.                 // Try again with 1/2 the count, won't flush then 'cause won't read it all
  265.                 flush = false;
  266.                 charsUsed /= 2;
  267.             }
  268.            
  269.             // Oops, we didn't have anything, we'll have to throw an overflow
  270.             throw new ArgumentException(Environment.GetResourceString("Argument_ConversionOverflow"));
  271.         }
  272.     }
  273. }

Developer Fusion