Update mcs/class/Commons.Xml.Relaxng/Commons.Xml.Relaxng/RelaxngPattern.cs
[mono.git] / mcs / class / corlib / System.Text / Encoder.cs
1 /*
2  * Encoder.cs - Implementation of the "System.Text.Encoder" class.
3  *
4  * Copyright (c) 2001  Southern Storm Software, Pty Ltd
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining
7  * a copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included
14  * in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22  * OTHER DEALINGS IN THE SOFTWARE.
23  */
24
25 namespace System.Text
26 {
27
28 using System;
29 using System.Runtime.InteropServices;
30
31 [Serializable]
32 [ComVisible (true)]
33 public abstract class Encoder
34 {
35
36         // Constructor.
37         protected Encoder() {}
38
39         EncoderFallback fallback = new EncoderReplacementFallback ();
40         EncoderFallbackBuffer fallback_buffer;
41
42         [ComVisible (false)]
43         public EncoderFallback Fallback {
44                 get { return fallback; }
45                 set {
46                         if (value == null)
47                                 throw new ArgumentNullException ();
48                         fallback = value;
49                         fallback_buffer = null;
50                 }
51         }
52
53         [ComVisible (false)]
54         public EncoderFallbackBuffer FallbackBuffer {
55                 get {
56                         if (fallback_buffer == null)
57                                 fallback_buffer = Fallback.CreateFallbackBuffer ();
58                         return fallback_buffer;
59                 }
60         }
61
62         // Get the number of bytes needed to encode a buffer.
63         public abstract int GetByteCount(char[] chars, int index,
64                                                                          int count, bool flush);
65
66         // Get the bytes that result from decoding a buffer.
67         public abstract int GetBytes(char[] chars, int charIndex, int charCount,
68                                                                  byte[] bytes, int byteIndex, bool flush);
69
70         [CLSCompliant (false)]
71         [ComVisible (false)]
72         public unsafe virtual int GetByteCount (char* chars, int count, bool flush)
73         {
74                 if (chars == null)
75                         throw new ArgumentNullException ("chars");
76                 if (count < 0)
77                         throw new ArgumentOutOfRangeException ("count");
78
79                 char [] carr = new char [count];
80                 Marshal.Copy ((IntPtr) chars, carr, 0, count);
81                 return GetByteCount (carr, 0, count, flush);
82         }
83
84         [CLSCompliant (false)]
85         [ComVisible (false)]
86         public unsafe virtual int GetBytes (char* chars, int charCount,
87                 byte* bytes, int byteCount, bool flush)
88         {
89                 CheckArguments (chars, charCount, bytes, byteCount);
90
91                 char [] carr = new char [charCount];
92                 Marshal.Copy ((IntPtr) chars, carr, 0, charCount);
93                 byte [] barr = new byte [byteCount];
94                 Marshal.Copy ((IntPtr) bytes, barr, 0, byteCount);
95                 return GetBytes (carr, 0, charCount, barr, 0, flush);
96         }
97
98         [ComVisible (false)]
99         public virtual void Reset ()
100         {
101                 if (fallback_buffer != null)
102                         fallback_buffer.Reset ();
103         }
104
105         [CLSCompliant (false)]
106         [ComVisible (false)]
107         public unsafe virtual void Convert (
108                 char* chars, int charCount,
109                 byte* bytes, int byteCount, bool flush,
110                 out int charsUsed, out int bytesUsed, out bool completed)
111         {
112                 CheckArguments (chars, charCount, bytes, byteCount);
113
114                 charsUsed = charCount;
115                 while (true) {
116                         bytesUsed = GetByteCount (chars, charsUsed, flush);
117                         if (bytesUsed <= byteCount)
118                                 break;
119                         flush = false;
120                         charsUsed >>= 1;
121                 }
122                 completed = charsUsed == charCount;
123                 bytesUsed = GetBytes (chars, charsUsed, bytes, byteCount, flush);
124         }
125
126         [ComVisible (false)]
127         public virtual void Convert (
128                 char [] chars, int charIndex, int charCount,
129                 byte [] bytes, int byteIndex, int byteCount, bool flush,
130                 out int charsUsed, out int bytesUsed, out bool completed)
131         {
132                 if (chars == null)
133                         throw new ArgumentNullException ("chars");
134                 if (bytes == null)
135                         throw new ArgumentNullException ("bytes");
136                 if (charIndex < 0 || chars.Length <= charIndex)
137                         throw new ArgumentOutOfRangeException ("charIndex");
138                 if (charCount < 0 || chars.Length < charIndex + charCount)
139                         throw new ArgumentOutOfRangeException ("charCount");
140                 if (byteIndex < 0 || bytes.Length <= byteIndex)
141                         throw new ArgumentOutOfRangeException ("byteIndex");
142                 if (byteCount < 0 || bytes.Length < byteIndex + byteCount)
143                         throw new ArgumentOutOfRangeException ("byteCount");
144
145                 charsUsed = charCount;
146                 while (true) {
147                         bytesUsed = GetByteCount (chars, charIndex, charsUsed, flush);
148                         if (bytesUsed <= byteCount)
149                                 break;
150                         flush = false;
151                         charsUsed >>= 1;
152                 }
153                 completed = charsUsed == charCount;
154                 bytesUsed = GetBytes (chars, charIndex, charsUsed, bytes, byteIndex, flush);
155         }
156
157         unsafe void CheckArguments (char* chars, int charCount, byte* bytes, int byteCount)
158         {
159                 if (chars == null)
160                         throw new ArgumentNullException ("chars");
161                 if (bytes == null)
162                         throw new ArgumentNullException ("bytes");
163                 if (charCount < 0)
164                         throw new ArgumentOutOfRangeException ("charCount");
165                 if (byteCount < 0)
166                         throw new ArgumentOutOfRangeException ("byteCount");
167         }
168 }; // class Encoder
169
170 }; // namespace System.Text