DecoderExceptionFallback.cs 4.1 KB

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