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