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