DecoderExceptionFallback.cs 4.2 KB

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