DecoderExceptionFallback.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. DecoderExceptionFallback that = value as DecoderExceptionFallback;
  30. if (that != null)
  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. private void Throw(byte[] bytesUnknown, int index)
  66. {
  67. // Create a string representation of our bytes.
  68. StringBuilder strBytes = new StringBuilder(bytesUnknown.Length * 3);
  69. int i;
  70. for (i = 0; i < bytesUnknown.Length && i < 20; i++)
  71. {
  72. strBytes.Append('[');
  73. strBytes.Append(bytesUnknown[i].ToString("X2", CultureInfo.InvariantCulture));
  74. strBytes.Append(']');
  75. }
  76. // In case the string's really long
  77. if (i == 20)
  78. strBytes.Append(" ...");
  79. // Known index
  80. throw new DecoderFallbackException(
  81. SR.Format(SR.Argument_InvalidCodePageBytesIndex,
  82. strBytes, index), bytesUnknown, index);
  83. }
  84. }
  85. // Exception for decoding unknown byte sequences.
  86. [Serializable]
  87. [System.Runtime.CompilerServices.TypeForwardedFrom("mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")]
  88. public sealed class DecoderFallbackException : ArgumentException
  89. {
  90. private byte[] _bytesUnknown = null;
  91. private int _index = 0;
  92. public DecoderFallbackException()
  93. : base(SR.Arg_ArgumentException)
  94. {
  95. HResult = HResults.COR_E_ARGUMENT;
  96. }
  97. public DecoderFallbackException(string message)
  98. : base(message)
  99. {
  100. HResult = HResults.COR_E_ARGUMENT;
  101. }
  102. public DecoderFallbackException(string message, Exception innerException)
  103. : base(message, innerException)
  104. {
  105. HResult = HResults.COR_E_ARGUMENT;
  106. }
  107. public DecoderFallbackException(string message, byte[] bytesUnknown, int index)
  108. : base(message)
  109. {
  110. _bytesUnknown = bytesUnknown;
  111. _index = index;
  112. }
  113. private DecoderFallbackException(SerializationInfo serializationInfo, StreamingContext streamingContext)
  114. : base(serializationInfo, streamingContext)
  115. {
  116. }
  117. public byte[] BytesUnknown
  118. {
  119. get
  120. {
  121. return (_bytesUnknown);
  122. }
  123. }
  124. public int Index
  125. {
  126. get
  127. {
  128. return _index;
  129. }
  130. }
  131. }
  132. }