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.
6 using System.Globalization;
10 public sealed class DecoderExceptionFallback : DecoderFallback
13 public DecoderExceptionFallback()
17 public override DecoderFallbackBuffer CreateFallbackBuffer()
19 return new DecoderExceptionFallbackBuffer();
22 // Maximum number of characters that this instance of this fallback could return
23 public override int MaxCharCount
31 public override bool Equals(Object value)
33 DecoderExceptionFallback that = value as DecoderExceptionFallback;
41 public override int GetHashCode()
48 public sealed class DecoderExceptionFallbackBuffer : DecoderFallbackBuffer
50 public override bool Fallback(byte[] bytesUnknown, int index)
52 Throw(bytesUnknown, index);
56 public override char GetNextChar()
61 public override bool MovePrevious()
63 // Exception fallback doesn't have anywhere to back up to.
67 // Exceptions are always empty
68 public override int Remaining
76 private void Throw(byte[] bytesUnknown, int index)
78 // Create a string representation of our bytes.
79 StringBuilder strBytes = new StringBuilder(bytesUnknown.Length * 3);
82 for (i = 0; i < bytesUnknown.Length && i < 20; i++)
85 strBytes.Append(bytesUnknown[i].ToString("X2", CultureInfo.InvariantCulture));
89 // In case the string's really long
91 strBytes.Append(" ...");
94 throw new DecoderFallbackException(
95 SR.Format(SR.Argument_InvalidCodePageBytesIndex,
96 strBytes, index), bytesUnknown, index);
100 // Exception for decoding unknown byte sequences.
101 public sealed class DecoderFallbackException : ArgumentException
103 private byte[] _bytesUnknown = null;
104 private int _index = 0;
106 public DecoderFallbackException()
107 : base(SR.Arg_ArgumentException)
109 HResult = __HResults.COR_E_ARGUMENT;
112 public DecoderFallbackException(String message)
115 HResult = __HResults.COR_E_ARGUMENT;
118 public DecoderFallbackException(String message, Exception innerException)
119 : base(message, innerException)
121 HResult = __HResults.COR_E_ARGUMENT;
124 public DecoderFallbackException(String message, byte[] bytesUnknown, int index)
127 _bytesUnknown = bytesUnknown;
131 public byte[] BytesUnknown
135 return (_bytesUnknown);