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