2 // System.Text.EncodingEncoder.cs
5 // Marcos Henrich (marcos.henrich@xamarin.com)
7 // Copyright (C) 2014 Xamarin Inc (http://www.xamarin.com)
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 using System.Runtime.InteropServices;
32 namespace System.Text {
34 abstract class EncodingEncoder : Encoder {
35 protected readonly Encoding encoding;
38 protected EncodingEncoder (Encoding encoding)
40 this.encoding = encoding;
41 var fallback = encoding.EncoderFallback;
46 public unsafe override void Convert (
47 char* chars, int charCount,
48 byte* bytes, int byteCount, bool flush,
49 out int charsUsed, out int bytesUsed, out bool completed)
52 throw new ArgumentNullException ("chars");
54 throw new ArgumentNullException ("bytes");
56 throw new ArgumentOutOfRangeException ("charCount");
58 throw new ArgumentOutOfRangeException ("byteCount");
60 charsUsed = encoding.GetCharCount (bytes, byteCount);
62 if (charsUsed > charCount)
63 charsUsed = charCount;
65 bytesUsed = encoding.GetBytes (chars, charsUsed, bytes, byteCount);
67 completed = charsUsed == charCount;
70 public override void Convert (
71 char [] chars, int charIndex, int charCount,
72 byte [] bytes, int byteIndex, int byteCount, bool flush,
73 out int charsUsed, out int bytesUsed, out bool completed)
76 throw new ArgumentNullException ("chars");
78 throw new ArgumentNullException ("bytes");
80 throw new ArgumentOutOfRangeException ("charIndex");
81 if (charCount < 0 || chars.Length < charIndex + charCount)
82 throw new ArgumentOutOfRangeException ("charCount");
84 throw new ArgumentOutOfRangeException ("byteIndex");
85 if (byteCount < 0 || bytes.Length < byteIndex + byteCount)
86 throw new ArgumentOutOfRangeException ("byteCount");
88 charsUsed = encoding.GetCharCount (bytes, byteIndex, byteCount);
90 if (charsUsed > charCount)
91 charsUsed = charCount;
93 bytesUsed = encoding.GetBytes (chars, charIndex, charsUsed, bytes, byteIndex);
95 completed = charsUsed == charCount;
97 }; // class EncodingEncoder
99 }; // namespace System.Text