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

  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. // DecoderExceptionFallback.cs
  16. //
  17. namespace System.Text
  18. {
  19.     using System;
  20.     using System.Runtime.Serialization;
  21.     using System.Globalization;
  22.    
  23.     [Serializable()]
  24.     public sealed class DecoderExceptionFallback : DecoderFallback
  25.     {
  26.         // Construction
  27.         public DecoderExceptionFallback()
  28.         {
  29.         }
  30.        
  31.         public override DecoderFallbackBuffer CreateFallbackBuffer()
  32.         {
  33.             return new DecoderExceptionFallbackBuffer();
  34.         }
  35.        
  36.         // Maximum number of characters that this instance of this fallback could return
  37.         public override int MaxCharCount {
  38.             get { return 0; }
  39.         }
  40.        
  41.         public override bool Equals(object value)
  42.         {
  43.             DecoderExceptionFallback that = value as DecoderExceptionFallback;
  44.             if (that != null) {
  45.                 return (true);
  46.             }
  47.             return (false);
  48.         }
  49.        
  50.         public override int GetHashCode()
  51.         {
  52.             return 879;
  53.         }
  54.     }
  55.    
  56.    
  57.     public sealed class DecoderExceptionFallbackBuffer : DecoderFallbackBuffer
  58.     {
  59.         public override bool Fallback(byte[] bytesUnknown, int index)
  60.         {
  61.             Throw(bytesUnknown, index);
  62.             return true;
  63.         }
  64.        
  65.         public override char GetNextChar()
  66.         {
  67.             return (char)0;
  68.         }
  69.        
  70.         public override bool MovePrevious()
  71.         {
  72.             // Exception fallback doesn't have anywhere to back up to.
  73.             return false;
  74.         }
  75.        
  76.         // Exceptions are always empty
  77.         public override int Remaining {
  78.             get { return 0; }
  79.         }
  80.        
  81.         private void Throw(byte[] bytesUnknown, int index)
  82.         {
  83.             // Create a string representation of our bytes.
  84.             StringBuilder strBytes = new StringBuilder(bytesUnknown.Length * 3);
  85.            
  86.             int i;
  87.             for (i = 0; i < bytesUnknown.Length && i < 20; i++) {
  88.                 strBytes.Append("[");
  89.                 strBytes.Append(bytesUnknown[i].ToString("X2", CultureInfo.InvariantCulture));
  90.                 strBytes.Append("]");
  91.             }
  92.            
  93.             // In case the string's really long
  94.             if (i == 20)
  95.                 strBytes.Append(" ...");
  96.            
  97.             // Known index
  98.             throw new DecoderFallbackException(Environment.GetResourceString("Argument_InvalidCodePageBytesIndex", strBytes, index), bytesUnknown, index);
  99.         }
  100.        
  101.     }
  102.    
  103.     // Exception for decoding unknown byte sequences.
  104.     [Serializable()]
  105.     public sealed class DecoderFallbackException : ArgumentException
  106.     {
  107.         byte[] bytesUnknown = null;
  108.         int index = 0;
  109.        
  110.         public DecoderFallbackException() : base(Environment.GetResourceString("Arg_ArgumentException"))
  111.         {
  112.             SetErrorCode(__HResults.COR_E_ARGUMENT);
  113.         }
  114.        
  115.         public DecoderFallbackException(string message) : base(message)
  116.         {
  117.             SetErrorCode(__HResults.COR_E_ARGUMENT);
  118.         }
  119.        
  120.         public DecoderFallbackException(string message, Exception innerException) : base(message, innerException)
  121.         {
  122.             SetErrorCode(__HResults.COR_E_ARGUMENT);
  123.         }
  124.        
  125.         internal DecoderFallbackException(SerializationInfo info, StreamingContext context) : base(info, context)
  126.         {
  127.         }
  128.        
  129.         public DecoderFallbackException(string message, byte[] bytesUnknown, int index) : base(message)
  130.         {
  131.             this.bytesUnknown = bytesUnknown;
  132.             this.index = index;
  133.         }
  134.        
  135.         public byte[] BytesUnknown {
  136.             get { return (bytesUnknown); }
  137.         }
  138.        
  139.         public int Index {
  140.             get { return this.index; }
  141.         }
  142.     }
  143. }

Developer Fusion