Merge pull request #1304 from slluis/mac-proxy-autoconfig
[mono.git] / mcs / class / corlib / System.Text / EncodingEncoder.cs
1 //
2 // System.Text.EncodingEncoder.cs
3 //
4 // Authors:
5 //   Marcos Henrich (marcos.henrich@xamarin.com)
6 //
7 // Copyright (C) 2014 Xamarin Inc (http://www.xamarin.com)
8 //
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:
16 // 
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 // 
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.
27 //
28
29 using System;
30 using System.Runtime.InteropServices;
31
32 namespace System.Text {
33
34 abstract class EncodingEncoder : Encoder {
35         protected readonly Encoding encoding;
36
37         // Constructor.
38         protected EncodingEncoder (Encoding encoding)
39         {
40                 this.encoding = encoding;
41                 var fallback = encoding.EncoderFallback;
42                 if (fallback != null)
43                         Fallback = fallback;
44         }
45
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)
50         {
51                 if (chars == null)
52                         throw new ArgumentNullException ("chars");
53                 if (bytes == null)
54                         throw new ArgumentNullException ("bytes");
55                 if (charCount < 0)
56                         throw new ArgumentOutOfRangeException ("charCount");
57                 if (byteCount < 0)
58                         throw new ArgumentOutOfRangeException ("byteCount");
59
60                 charsUsed = encoding.GetCharCount (bytes, byteCount);
61
62                 if (charsUsed > charCount)
63                         charsUsed = charCount;
64
65                 bytesUsed = encoding.GetBytes (chars, charsUsed, bytes, byteCount);
66
67                 completed = charsUsed == charCount;
68         }
69
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)
74         {
75                 if (chars == null)
76                         throw new ArgumentNullException ("chars");
77                 if (bytes == null)
78                         throw new ArgumentNullException ("bytes");
79                 if (charIndex < 0)
80                         throw new ArgumentOutOfRangeException ("charIndex");
81                 if (charCount < 0 || chars.Length < charIndex + charCount)
82                         throw new ArgumentOutOfRangeException ("charCount");
83                 if (byteIndex < 0)
84                         throw new ArgumentOutOfRangeException ("byteIndex");
85                 if (byteCount < 0 || bytes.Length < byteIndex + byteCount)
86                         throw new ArgumentOutOfRangeException ("byteCount");
87
88                 charsUsed = encoding.GetCharCount (bytes, byteIndex, byteCount);
89
90                 if (charsUsed > charCount)
91                         charsUsed = charCount;
92
93                 bytesUsed = encoding.GetBytes (chars, charIndex, charsUsed, bytes, byteIndex);
94
95                 completed = charsUsed == charCount;
96         }
97 }; // class EncodingEncoder
98
99 }; // namespace System.Text