mscorlib(4.0.0.0) API with additions
Encoder.cs
3 using System.Security;
4 
5 namespace System.Text
6 {
9  [ComVisible(true)]
10  [__DynamicallyInvokable]
11  public abstract class Encoder
12  {
13  internal EncoderFallback m_fallback;
14 
15  [NonSerialized]
16  internal EncoderFallbackBuffer m_fallbackBuffer;
17 
24  [ComVisible(false)]
25  [__DynamicallyInvokable]
27  {
28  [__DynamicallyInvokable]
29  get
30  {
31  return m_fallback;
32  }
33  [__DynamicallyInvokable]
34  set
35  {
36  if (value == null)
37  {
38  throw new ArgumentNullException("value");
39  }
40  if (m_fallbackBuffer != null && m_fallbackBuffer.Remaining > 0)
41  {
42  throw new ArgumentException(Environment.GetResourceString("Argument_FallbackBufferNotEmpty"), "value");
43  }
44  m_fallback = value;
45  m_fallbackBuffer = null;
46  }
47  }
48 
51  [ComVisible(false)]
52  [__DynamicallyInvokable]
54  {
55  [__DynamicallyInvokable]
56  get
57  {
58  if (m_fallbackBuffer == null)
59  {
60  if (m_fallback != null)
61  {
62  m_fallbackBuffer = m_fallback.CreateFallbackBuffer();
63  }
64  else
65  {
67  }
68  }
69  return m_fallbackBuffer;
70  }
71  }
72 
73  internal bool InternalHasFallbackBuffer => m_fallbackBuffer != null;
74 
75  internal void SerializeEncoder(SerializationInfo info)
76  {
77  info.AddValue("m_fallback", m_fallback);
78  }
79 
81  [__DynamicallyInvokable]
82  protected Encoder()
83  {
84  }
85 
87  [ComVisible(false)]
88  [__DynamicallyInvokable]
89  public virtual void Reset()
90  {
91  char[] chars = new char[0];
92  byte[] bytes = new byte[GetByteCount(chars, 0, 0, flush: true)];
93  GetBytes(chars, 0, 0, bytes, 0, flush: true);
94  if (m_fallbackBuffer != null)
95  {
96  m_fallbackBuffer.Reset();
97  }
98  }
99 
114  [__DynamicallyInvokable]
115  public abstract int GetByteCount(char[] chars, int index, int count, bool flush);
116 
129  [SecurityCritical]
130  [CLSCompliant(false)]
131  [ComVisible(false)]
132  public unsafe virtual int GetByteCount(char* chars, int count, bool flush)
133  {
134  if (chars == null)
135  {
136  throw new ArgumentNullException("chars", Environment.GetResourceString("ArgumentNull_Array"));
137  }
138  if (count < 0)
139  {
140  throw new ArgumentOutOfRangeException("count", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
141  }
142  char[] array = new char[count];
143  for (int i = 0; i < count; i++)
144  {
145  array[i] = chars[i];
146  }
147  return GetByteCount(array, 0, count, flush);
148  }
149 
170  [__DynamicallyInvokable]
171  public abstract int GetBytes(char[] chars, int charIndex, int charCount, byte[] bytes, int byteIndex, bool flush);
172 
190  [SecurityCritical]
191  [CLSCompliant(false)]
192  [ComVisible(false)]
193  public unsafe virtual int GetBytes(char* chars, int charCount, byte* bytes, int byteCount, bool flush)
194  {
195  if (bytes == null || chars == null)
196  {
197  throw new ArgumentNullException((bytes == null) ? "bytes" : "chars", Environment.GetResourceString("ArgumentNull_Array"));
198  }
199  if (charCount < 0 || byteCount < 0)
200  {
201  throw new ArgumentOutOfRangeException((charCount < 0) ? "charCount" : "byteCount", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
202  }
203  char[] array = new char[charCount];
204  for (int i = 0; i < charCount; i++)
205  {
206  array[i] = chars[i];
207  }
208  byte[] array2 = new byte[byteCount];
209  int bytes2 = GetBytes(array, 0, charCount, array2, 0, flush);
210  if (bytes2 < byteCount)
211  {
212  byteCount = bytes2;
213  }
214  for (int i = 0; i < byteCount; i++)
215  {
216  bytes[i] = array2[i];
217  }
218  return byteCount;
219  }
220 
240  [ComVisible(false)]
241  [__DynamicallyInvokable]
242  public virtual void Convert(char[] chars, int charIndex, int charCount, byte[] bytes, int byteIndex, int byteCount, bool flush, out int charsUsed, out int bytesUsed, out bool completed)
243  {
244  if (chars == null || bytes == null)
245  {
246  throw new ArgumentNullException((chars == null) ? "chars" : "bytes", Environment.GetResourceString("ArgumentNull_Array"));
247  }
248  if (charIndex < 0 || charCount < 0)
249  {
250  throw new ArgumentOutOfRangeException((charIndex < 0) ? "charIndex" : "charCount", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
251  }
252  if (byteIndex < 0 || byteCount < 0)
253  {
254  throw new ArgumentOutOfRangeException((byteIndex < 0) ? "byteIndex" : "byteCount", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
255  }
256  if (chars.Length - charIndex < charCount)
257  {
258  throw new ArgumentOutOfRangeException("chars", Environment.GetResourceString("ArgumentOutOfRange_IndexCountBuffer"));
259  }
260  if (bytes.Length - byteIndex < byteCount)
261  {
262  throw new ArgumentOutOfRangeException("bytes", Environment.GetResourceString("ArgumentOutOfRange_IndexCountBuffer"));
263  }
264  for (charsUsed = charCount; charsUsed > 0; charsUsed /= 2)
265  {
266  if (GetByteCount(chars, charIndex, charsUsed, flush) <= byteCount)
267  {
268  bytesUsed = GetBytes(chars, charIndex, charsUsed, bytes, byteIndex, flush);
269  completed = (charsUsed == charCount && (m_fallbackBuffer == null || m_fallbackBuffer.Remaining == 0));
270  return;
271  }
272  flush = false;
273  }
274  throw new ArgumentException(Environment.GetResourceString("Argument_ConversionOverflow"));
275  }
276 
294  [SecurityCritical]
295  [CLSCompliant(false)]
296  [ComVisible(false)]
297  public unsafe virtual void Convert(char* chars, int charCount, byte* bytes, int byteCount, bool flush, out int charsUsed, out int bytesUsed, out bool completed)
298  {
299  if (bytes == null || chars == null)
300  {
301  throw new ArgumentNullException((bytes == null) ? "bytes" : "chars", Environment.GetResourceString("ArgumentNull_Array"));
302  }
303  if (charCount < 0 || byteCount < 0)
304  {
305  throw new ArgumentOutOfRangeException((charCount < 0) ? "charCount" : "byteCount", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
306  }
307  for (charsUsed = charCount; charsUsed > 0; charsUsed /= 2)
308  {
309  if (GetByteCount(chars, charsUsed, flush) <= byteCount)
310  {
311  bytesUsed = GetBytes(chars, charsUsed, bytes, byteCount, flush);
312  completed = (charsUsed == charCount && (m_fallbackBuffer == null || m_fallbackBuffer.Remaining == 0));
313  return;
314  }
315  flush = false;
316  }
317  throw new ArgumentException(Environment.GetResourceString("Argument_ConversionOverflow"));
318  }
319  }
320 }
static EncoderFallback ReplacementFallback
Gets an object that outputs a substitute string in place of an input character that cannot be encoded...
virtual void Convert(char[] chars, int charIndex, int charCount, byte[] bytes, int byteIndex, int byteCount, bool flush, out int charsUsed, out int bytesUsed, out bool completed)
Converts an array of Unicode characters to an encoded byte sequence and stores the result in an array...
Definition: Encoder.cs:242
The exception that is thrown when a null reference (Nothing in Visual Basic) is passed to a method th...
Converts a set of characters into a sequence of bytes.
Definition: Encoder.cs:11
virtual unsafe int GetByteCount(char *chars, int count, bool flush)
When overridden in a derived class, calculates the number of bytes produced by encoding a set of char...
Definition: Encoder.cs:132
abstract int GetBytes(char[] chars, int charIndex, int charCount, byte[] bytes, int byteIndex, bool flush)
When overridden in a derived class, encodes a set of characters from the specified character array an...
Definition: __Canon.cs:3
The exception that is thrown when the value of an argument is outside the allowable range of values a...
abstract int Remaining
When overridden in a derived class, gets the number of characters in the current T:System....
Provides a failure-handling mechanism, called a fallback, for an input character that cannot be conve...
virtual void Reset()
Initializes all data and state information pertaining to this fallback buffer.
Provides information about, and means to manipulate, the current environment and platform....
Definition: Environment.cs:21
virtual void Reset()
When overridden in a derived class, sets the encoder back to its initial state.
Definition: Encoder.cs:89
Stores all the data needed to serialize or deserialize an object. This class cannot be inherited.
EncoderFallbackBuffer FallbackBuffer
Gets the T:System.Text.EncoderFallbackBuffer object associated with the current T:System....
Definition: Encoder.cs:54
The exception that is thrown when one of the arguments provided to a method is not valid.
abstract int GetByteCount(char[] chars, int index, int count, bool flush)
When overridden in a derived class, calculates the number of bytes produced by encoding a set of char...
Encoder()
Initializes a new instance of the T:System.Text.Encoder class.
Definition: Encoder.cs:82
Provides a buffer that allows a fallback handler to return an alternate string to an encoder when it ...
Specifies that the class can be serialized.
abstract EncoderFallbackBuffer CreateFallbackBuffer()
When overridden in a derived class, initializes a new instance of the T:System.Text....
EncoderFallback Fallback
Gets or sets a T:System.Text.EncoderFallback object for the current T:System.Text....
Definition: Encoder.cs:27
virtual unsafe int GetBytes(char *chars, int charCount, byte *bytes, int byteCount, bool flush)
When overridden in a derived class, encodes a set of characters starting at the specified character p...
Definition: Encoder.cs:193
virtual unsafe void Convert(char *chars, int charCount, byte *bytes, int byteCount, bool flush, out int charsUsed, out int bytesUsed, out bool completed)
Converts a buffer of Unicode characters to an encoded byte sequence and stores the result in another ...
Definition: Encoder.cs:297