| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113 |
- // Licensed to the .NET Foundation under one or more agreements.
- // The .NET Foundation licenses this file to you under the MIT license.
- // See the LICENSE file in the project root for more information.
- using System.Diagnostics.CodeAnalysis;
- using System.Globalization;
- using System.Runtime.Serialization;
- namespace System.Text
- {
- public sealed class DecoderExceptionFallback : DecoderFallback
- {
- public override DecoderFallbackBuffer CreateFallbackBuffer() =>
- new DecoderExceptionFallbackBuffer();
- // Maximum number of characters that this instance of this fallback could return
- public override int MaxCharCount => 0;
- public override bool Equals(object? value) =>
- value is DecoderExceptionFallback;
- public override int GetHashCode() => 879;
- }
- public sealed class DecoderExceptionFallbackBuffer : DecoderFallbackBuffer
- {
- public override bool Fallback(byte[] bytesUnknown, int index)
- {
- Throw(bytesUnknown, index);
- return true;
- }
- public override char GetNextChar() => (char)0;
- // Exception fallback doesn't have anywhere to back up to.
- public override bool MovePrevious() => false;
- // Exceptions are always empty
- public override int Remaining => 0;
- [DoesNotReturn]
- private void Throw(byte[] bytesUnknown, int index)
- {
- bytesUnknown ??= Array.Empty<byte>();
- // Create a string representation of our bytes.
- StringBuilder strBytes = new StringBuilder(bytesUnknown.Length * 4);
- const int MaxLength = 20;
- for (int i = 0; i < bytesUnknown.Length && i < MaxLength; i++)
- {
- strBytes.Append('[');
- strBytes.Append(bytesUnknown[i].ToString("X2", CultureInfo.InvariantCulture));
- strBytes.Append(']');
- }
- // In case the string's really long
- if (bytesUnknown.Length > MaxLength)
- {
- strBytes.Append(" ...");
- }
- // Known index
- throw new DecoderFallbackException(
- SR.Format(SR.Argument_InvalidCodePageBytesIndex,
- strBytes, index), bytesUnknown, index);
- }
- }
- // Exception for decoding unknown byte sequences.
- [Serializable]
- [System.Runtime.CompilerServices.TypeForwardedFrom("mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")]
- public sealed class DecoderFallbackException : ArgumentException
- {
- private readonly byte[]? _bytesUnknown = null;
- private readonly int _index = 0;
- public DecoderFallbackException()
- : base(SR.Arg_ArgumentException)
- {
- HResult = HResults.COR_E_ARGUMENT;
- }
- public DecoderFallbackException(string? message)
- : base(message)
- {
- HResult = HResults.COR_E_ARGUMENT;
- }
- public DecoderFallbackException(string? message, Exception? innerException)
- : base(message, innerException)
- {
- HResult = HResults.COR_E_ARGUMENT;
- }
- public DecoderFallbackException(string? message, byte[]? bytesUnknown, int index)
- : base(message)
- {
- _bytesUnknown = bytesUnknown;
- _index = index;
- }
- private DecoderFallbackException(SerializationInfo serializationInfo, StreamingContext streamingContext)
- : base(serializationInfo, streamingContext)
- {
- }
- public byte[]? BytesUnknown => _bytesUnknown;
- public int Index => _index;
- }
- }
|