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.
6 using System.Runtime.Serialization;
7 using System.Globalization;
11 public sealed class DecoderExceptionFallback : DecoderFallback
14 public DecoderExceptionFallback()
18 public override DecoderFallbackBuffer CreateFallbackBuffer()
20 return new DecoderExceptionFallbackBuffer();
23 // Maximum number of characters that this instance of this fallback could return
24 public override int MaxCharCount
32 public override bool Equals(Object value)
34 DecoderExceptionFallback that = value as DecoderExceptionFallback;
42 public override int GetHashCode()
49 public sealed class DecoderExceptionFallbackBuffer : DecoderFallbackBuffer
51 public override bool Fallback(byte[] bytesUnknown, int index)
53 Throw(bytesUnknown, index);
57 public override char GetNextChar()
62 public override bool MovePrevious()
64 // Exception fallback doesn't have anywhere to back up to.
68 // Exceptions are always empty
69 public override int Remaining
77 private void Throw(byte[] bytesUnknown, int index)
79 // Create a string representation of our bytes.
80 StringBuilder strBytes = new StringBuilder(bytesUnknown.Length * 3);
83 for (i = 0; i < bytesUnknown.Length && i < 20; i++)
86 strBytes.Append(bytesUnknown[i].ToString("X2", CultureInfo.InvariantCulture));
90 // In case the string's really long
92 strBytes.Append(" ...");
95 throw new DecoderFallbackException(
96 SR.Format(SR.Argument_InvalidCodePageBytesIndex, strBytes, index), bytesUnknown, index);
100 // Exception for decoding unknown byte sequences.
101 public sealed class DecoderFallbackException : ArgumentException
103 private byte[] bytesUnknown = null;
104 private int index = 0;
106 public DecoderFallbackException()
107 : base(SR.Arg_ArgumentException)
109 HResult = __HResults.COR_E_ARGUMENT;
112 public DecoderFallbackException(String message)
115 HResult = __HResults.COR_E_ARGUMENT;
118 public DecoderFallbackException(String message, Exception innerException)
119 : base(message, innerException)
121 HResult = __HResults.COR_E_ARGUMENT;
124 internal DecoderFallbackException(SerializationInfo info, StreamingContext context) : base(info, context)
126 throw new PlatformNotSupportedException();
129 public DecoderFallbackException(
130 String message, byte[] bytesUnknown, int index) : base(message)
132 this.bytesUnknown = bytesUnknown;
136 public byte[] BytesUnknown
140 return (bytesUnknown);