1 // Licensed to the .NET Foundation under one or more agreements. 2 // The .NET Foundation licenses this file to you under the MIT license. 3 // See the LICENSE file in the project root for more information. 4 5 using System; 6 using System.Globalization; 7 using System.Runtime.Serialization; 8 9 namespace System.Text 10 { 11 public sealed class DecoderExceptionFallback : DecoderFallback 12 { 13 // Construction DecoderExceptionFallback()14 public DecoderExceptionFallback() 15 { 16 } 17 CreateFallbackBuffer()18 public override DecoderFallbackBuffer CreateFallbackBuffer() 19 { 20 return new DecoderExceptionFallbackBuffer(); 21 } 22 23 // Maximum number of characters that this instance of this fallback could return 24 public override int MaxCharCount 25 { 26 get 27 { 28 return 0; 29 } 30 } 31 Equals(Object value)32 public override bool Equals(Object value) 33 { 34 DecoderExceptionFallback that = value as DecoderExceptionFallback; 35 if (that != null) 36 { 37 return (true); 38 } 39 return (false); 40 } 41 GetHashCode()42 public override int GetHashCode() 43 { 44 return 879; 45 } 46 } 47 48 49 public sealed class DecoderExceptionFallbackBuffer : DecoderFallbackBuffer 50 { Fallback(byte[] bytesUnknown, int index)51 public override bool Fallback(byte[] bytesUnknown, int index) 52 { 53 Throw(bytesUnknown, index); 54 return true; 55 } 56 GetNextChar()57 public override char GetNextChar() 58 { 59 return (char)0; 60 } 61 MovePrevious()62 public override bool MovePrevious() 63 { 64 // Exception fallback doesn't have anywhere to back up to. 65 return false; 66 } 67 68 // Exceptions are always empty 69 public override int Remaining 70 { 71 get 72 { 73 return 0; 74 } 75 } 76 Throw(byte[] bytesUnknown, int index)77 private void Throw(byte[] bytesUnknown, int index) 78 { 79 // Create a string representation of our bytes. 80 StringBuilder strBytes = new StringBuilder(bytesUnknown.Length * 3); 81 82 int i; 83 for (i = 0; i < bytesUnknown.Length && i < 20; i++) 84 { 85 strBytes.Append('['); 86 strBytes.Append(bytesUnknown[i].ToString("X2", CultureInfo.InvariantCulture)); 87 strBytes.Append(']'); 88 } 89 90 // In case the string's really long 91 if (i == 20) 92 strBytes.Append(" ..."); 93 94 // Known index 95 throw new DecoderFallbackException( 96 SR.Format(SR.Argument_InvalidCodePageBytesIndex, 97 strBytes, index), bytesUnknown, index); 98 } 99 } 100 101 // Exception for decoding unknown byte sequences. 102 [Serializable] 103 [System.Runtime.CompilerServices.TypeForwardedFrom("mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")] 104 public sealed class DecoderFallbackException : ArgumentException 105 { 106 private byte[] _bytesUnknown = null; 107 private int _index = 0; 108 DecoderFallbackException()109 public DecoderFallbackException() 110 : base(SR.Arg_ArgumentException) 111 { 112 HResult = HResults.COR_E_ARGUMENT; 113 } 114 DecoderFallbackException(String message)115 public DecoderFallbackException(String message) 116 : base(message) 117 { 118 HResult = HResults.COR_E_ARGUMENT; 119 } 120 DecoderFallbackException(String message, Exception innerException)121 public DecoderFallbackException(String message, Exception innerException) 122 : base(message, innerException) 123 { 124 HResult = HResults.COR_E_ARGUMENT; 125 } 126 DecoderFallbackException(String message, byte[] bytesUnknown, int index)127 public DecoderFallbackException(String message, byte[] bytesUnknown, int index) 128 : base(message) 129 { 130 _bytesUnknown = bytesUnknown; 131 _index = index; 132 } 133 DecoderFallbackException(SerializationInfo serializationInfo, StreamingContext streamingContext)134 private DecoderFallbackException(SerializationInfo serializationInfo, StreamingContext streamingContext) 135 : base(serializationInfo, streamingContext) 136 { 137 } 138 139 public byte[] BytesUnknown 140 { 141 get 142 { 143 return (_bytesUnknown); 144 } 145 } 146 147 public int Index 148 { 149 get 150 { 151 return _index; 152 } 153 } 154 } 155 } 156