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

  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. // EncoderExceptionFallback.cs
  16. namespace System.Text
  17. {
  18.     using System;
  19.     using System.Runtime.Serialization;
  20.    
  21.     [Serializable()]
  22.     public sealed class EncoderExceptionFallback : EncoderFallback
  23.     {
  24.         // Construction
  25.         public EncoderExceptionFallback()
  26.         {
  27.         }
  28.        
  29.         public override EncoderFallbackBuffer CreateFallbackBuffer()
  30.         {
  31.             return new EncoderExceptionFallbackBuffer();
  32.         }
  33.        
  34.         // Maximum number of characters that this instance of this fallback could return
  35.         public override int MaxCharCount {
  36.             get { return 0; }
  37.         }
  38.        
  39.         public override bool Equals(object value)
  40.         {
  41.             EncoderExceptionFallback that = value as EncoderExceptionFallback;
  42.             if (that != null) {
  43.                 return (true);
  44.             }
  45.             return (false);
  46.         }
  47.        
  48.         public override int GetHashCode()
  49.         {
  50.             return 654;
  51.         }
  52.     }
  53.    
  54.    
  55.     public sealed class EncoderExceptionFallbackBuffer : EncoderFallbackBuffer
  56.     {
  57.         public override bool Fallback(char charUnknown, int index)
  58.         {
  59.             // Fall back our char
  60.             throw new EncoderFallbackException(Environment.GetResourceString("Argument_InvalidCodePageConversionIndex", (int)charUnknown, index), charUnknown, index);
  61.         }
  62.        
  63.         public override bool Fallback(char charUnknownHigh, char charUnknownLow, int index)
  64.         {
  65.             if (!Char.IsHighSurrogate(charUnknownHigh)) {
  66.                 throw new ArgumentOutOfRangeException("charUnknownHigh", Environment.GetResourceString("ArgumentOutOfRange_Range", 55296, 56319));
  67.             }
  68.             if (!Char.IsLowSurrogate(charUnknownLow)) {
  69.                 throw new ArgumentOutOfRangeException("CharUnknownLow", Environment.GetResourceString("ArgumentOutOfRange_Range", 56320, 57343));
  70.             }
  71.            
  72.             int iTemp = Char.ConvertToUtf32(charUnknownHigh, charUnknownLow);
  73.            
  74.             // Fall back our char
  75.             throw new EncoderFallbackException(Environment.GetResourceString("Argument_InvalidCodePageConversionIndex", iTemp, index), charUnknownHigh, charUnknownLow, index);
  76.         }
  77.        
  78.         public override char GetNextChar()
  79.         {
  80.             return (char)0;
  81.         }
  82.        
  83.         public override bool MovePrevious()
  84.         {
  85.             // Exception fallback doesn't have anywhere to back up to.
  86.             return false;
  87.         }
  88.        
  89.         // Exceptions are always empty
  90.         public override int Remaining {
  91.             get { return 0; }
  92.         }
  93.     }
  94.    
  95.     [Serializable()]
  96.     public sealed class EncoderFallbackException : ArgumentException
  97.     {
  98.         char charUnknown;
  99.         char charUnknownHigh;
  100.         char charUnknownLow;
  101.         int index;
  102.        
  103.         public EncoderFallbackException() : base(Environment.GetResourceString("Arg_ArgumentException"))
  104.         {
  105.             SetErrorCode(__HResults.COR_E_ARGUMENT);
  106.         }
  107.        
  108.         public EncoderFallbackException(string message) : base(message)
  109.         {
  110.             SetErrorCode(__HResults.COR_E_ARGUMENT);
  111.         }
  112.        
  113.         public EncoderFallbackException(string message, Exception innerException) : base(message, innerException)
  114.         {
  115.             SetErrorCode(__HResults.COR_E_ARGUMENT);
  116.         }
  117.        
  118.         internal EncoderFallbackException(SerializationInfo info, StreamingContext context) : base(info, context)
  119.         {
  120.         }
  121.        
  122.         internal EncoderFallbackException(string message, char charUnknown, int index) : base(message)
  123.         {
  124.             this.charUnknown = charUnknown;
  125.             this.index = index;
  126.         }
  127.        
  128.         internal EncoderFallbackException(string message, char charUnknownHigh, char charUnknownLow, int index) : base(message)
  129.         {
  130.             if (!Char.IsHighSurrogate(charUnknownHigh)) {
  131.                 throw new ArgumentOutOfRangeException("charUnknownHigh", Environment.GetResourceString("ArgumentOutOfRange_Range", 55296, 56319));
  132.             }
  133.             if (!Char.IsLowSurrogate(charUnknownLow)) {
  134.                 throw new ArgumentOutOfRangeException("CharUnknownLow", Environment.GetResourceString("ArgumentOutOfRange_Range", 56320, 57343));
  135.             }
  136.            
  137.             this.charUnknownHigh = charUnknownHigh;
  138.             this.charUnknownLow = charUnknownLow;
  139.             this.index = index;
  140.         }
  141.        
  142.         public char CharUnknown {
  143.             get { return (charUnknown); }
  144.         }
  145.        
  146.         public char CharUnknownHigh {
  147.             get { return (charUnknownHigh); }
  148.         }
  149.        
  150.         public char CharUnknownLow {
  151.             get { return (charUnknownLow); }
  152.         }
  153.        
  154.         public int Index {
  155.             get { return index; }
  156.         }
  157.        
  158.         // Return true if the unknown character is a surrogate pair.
  159.         public bool IsUnknownSurrogate()
  160.         {
  161.             return (this.charUnknownHigh != '\0');
  162.         }
  163.     }
  164. }

Developer Fusion