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.
5 using System
.Diagnostics
.CodeAnalysis
;
6 using System
.Globalization
;
7 using System
.Runtime
.Serialization
;
11 public sealed class DecoderExceptionFallback
: DecoderFallback
13 public override DecoderFallbackBuffer
CreateFallbackBuffer() =>
14 new DecoderExceptionFallbackBuffer();
16 // Maximum number of characters that this instance of this fallback could return
17 public override int MaxCharCount
=> 0;
19 public override bool Equals(object? value) =>
20 value is DecoderExceptionFallback
;
22 public override int GetHashCode() => 879;
26 public sealed class DecoderExceptionFallbackBuffer
: DecoderFallbackBuffer
28 public override bool Fallback(byte[] bytesUnknown
, int index
)
30 Throw(bytesUnknown
, index
);
34 public override char GetNextChar() => (char)0;
36 // Exception fallback doesn't have anywhere to back up to.
37 public override bool MovePrevious() => false;
39 // Exceptions are always empty
40 public override int Remaining
=> 0;
43 private void Throw(byte[] bytesUnknown
, int index
)
45 bytesUnknown
??= Array
.Empty
<byte>();
47 // Create a string representation of our bytes.
48 StringBuilder strBytes
= new StringBuilder(bytesUnknown
.Length
* 4);
50 const int MaxLength
= 20;
51 for (int i
= 0; i
< bytesUnknown
.Length
&& i
< MaxLength
; i
++)
54 strBytes
.Append(bytesUnknown
[i
].ToString("X2", CultureInfo
.InvariantCulture
));
58 // In case the string's really long
59 if (bytesUnknown
.Length
> MaxLength
)
61 strBytes
.Append(" ...");
65 throw new DecoderFallbackException(
66 SR
.Format(SR
.Argument_InvalidCodePageBytesIndex
,
67 strBytes
, index
), bytesUnknown
, index
);
71 // Exception for decoding unknown byte sequences.
73 [System
.Runtime
.CompilerServices
.TypeForwardedFrom("mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")]
74 public sealed class DecoderFallbackException
: ArgumentException
76 private readonly byte[]? _bytesUnknown
= null;
77 private readonly int _index
= 0;
79 public DecoderFallbackException()
80 : base(SR
.Arg_ArgumentException
)
82 HResult
= HResults
.COR_E_ARGUMENT
;
85 public DecoderFallbackException(string? message
)
88 HResult
= HResults
.COR_E_ARGUMENT
;
91 public DecoderFallbackException(string? message
, Exception
? innerException
)
92 : base(message
, innerException
)
94 HResult
= HResults
.COR_E_ARGUMENT
;
97 public DecoderFallbackException(string? message
, byte[]? bytesUnknown
, int index
)
100 _bytesUnknown
= bytesUnknown
;
104 private DecoderFallbackException(SerializationInfo serializationInfo
, StreamingContext streamingContext
)
105 : base(serializationInfo
, streamingContext
)
109 public byte[]? BytesUnknown
=> _bytesUnknown
;
111 public int Index
=> _index
;