74062465718f4c05303dfe4d855bbcbf831dd165
[mono.git] / mcs / class / corlib / System.Text / Decoder.cs
1 /*
2  * Decoder.cs - Implementation of the "System.Text.Decoder" 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 #if NET_2_0
33 [ComVisible (true)]
34 #endif
35 public abstract class Decoder
36 {
37
38         // Constructor.
39         protected Decoder () {}
40
41 #if NET_2_0
42         DecoderFallback fallback = new DecoderReplacementFallback ();
43         DecoderFallbackBuffer fallback_buffer;
44
45         [ComVisible (false)]
46         public DecoderFallback Fallback {
47                 get { return fallback; }
48                 set {
49                         if (value == null)
50                                 throw new ArgumentNullException ();
51                         fallback = value;
52                         fallback_buffer = null;
53                 }
54         }
55
56         [ComVisible (false)]
57         public DecoderFallbackBuffer FallbackBuffer {
58                 get {
59                         if (fallback_buffer == null)
60                                 fallback_buffer = fallback.CreateFallbackBuffer ();
61                         return fallback_buffer;
62                 }
63         }
64 #endif
65
66         // Get the number of characters needed to decode a buffer.
67         public abstract int GetCharCount (byte[] bytes, int index, int count);
68
69         // Get the characters that result from decoding a buffer.
70         public abstract int GetChars (byte[] bytes, int byteIndex, int byteCount,
71                                                                  char[] chars, int charIndex);
72
73 #if NET_2_0
74         [ComVisible (false)]
75         public virtual int GetCharCount (byte [] bytes, int index, int count, bool flush)
76         {
77                 if (flush)
78                         Reset ();
79                 return GetCharCount (bytes, index, count);
80         }
81
82         [CLSCompliant (false)]
83         [ComVisible (false)]
84         public unsafe virtual int GetCharCount (byte* bytes, int count, bool flush)
85         {
86                 if (bytes == null)
87                         throw new ArgumentNullException ("bytes");
88                 if (count < 0)
89                         throw new ArgumentOutOfRangeException ("count");
90
91                 byte [] barr = new byte [count];
92                 Marshal.Copy ((IntPtr) bytes, barr, 0, count);
93                 return GetCharCount (barr, 0, count, flush);
94         }
95
96         public virtual int GetChars (
97                 byte[] bytes, int byteIndex, int byteCount,
98                 char[] chars, int charIndex, bool flush)
99         {
100                 CheckArguments (bytes, byteIndex, byteCount);
101                 CheckArguments (chars, charIndex);
102
103                 if (flush)
104                         Reset ();
105                 return GetChars (bytes, byteIndex, byteCount, chars, charIndex);
106         }
107
108         [CLSCompliant (false)]
109         [ComVisible (false)]
110         public unsafe virtual int GetChars (byte* bytes, int byteCount,
111                 char* chars, int charCount, bool flush)
112         {
113                 CheckArguments (chars, charCount, bytes, byteCount);
114
115                 char [] carr = new char [charCount];
116                 Marshal.Copy ((IntPtr) chars, carr, 0, charCount);
117                 byte [] barr = new byte [byteCount];
118                 Marshal.Copy ((IntPtr) bytes, barr, 0, byteCount);
119                 return GetChars (barr, 0, byteCount, carr, 0, flush);
120         }
121
122         [ComVisible (false)]
123         public virtual void Reset ()
124         {
125                 if (fallback_buffer != null)
126                         fallback_buffer.Reset ();
127         }
128
129         [CLSCompliant (false)]
130         [ComVisible (false)]
131         public unsafe virtual void Convert (
132                 byte* bytes, int byteCount,
133                 char* chars, int charCount, bool flush,
134                 out int bytesUsed, out int charsUsed, out bool completed)
135         {
136                 CheckArguments (chars, charCount, bytes, byteCount);
137
138                 bytesUsed = byteCount;
139                 while (true) {
140                         charsUsed = GetCharCount (bytes, bytesUsed, flush);
141                         if (charsUsed <= charCount)
142                                 break;
143                         flush = false;
144                         bytesUsed >>= 1;
145                 }
146                 completed = bytesUsed == byteCount;
147                 charsUsed = GetChars (bytes, bytesUsed, chars, charCount, flush);
148         }
149
150         [ComVisible (false)]
151         public virtual void Convert (
152                 byte [] bytes, int byteIndex, int byteCount,
153                 char [] chars, int charIndex, int charCount, bool flush,
154                 out int bytesUsed, out int charsUsed, out bool completed)
155         {
156                 CheckArguments (bytes, byteIndex, byteCount);
157                 CheckArguments (chars, charIndex);
158                 if (charCount < 0 || chars.Length < charIndex + charCount)
159                         throw new ArgumentOutOfRangeException ("charCount");
160
161                 bytesUsed = byteCount;
162                 while (true) {
163                         charsUsed = GetCharCount (bytes, byteIndex, bytesUsed, flush);
164                         if (charsUsed <= charCount)
165                                 break;
166                         flush = false;
167                         bytesUsed >>= 1;
168                 }
169                 completed = bytesUsed == byteCount;
170                 charsUsed = GetChars (bytes, byteIndex, bytesUsed, chars, charIndex, flush);
171         }
172
173         void CheckArguments (char [] chars, int charIndex)
174         {
175                 if (chars == null)
176                         throw new ArgumentNullException ("chars");
177                 if (charIndex < 0 || chars.Length <= charIndex)
178                         throw new ArgumentOutOfRangeException ("charIndex");
179         }
180
181         void CheckArguments (byte [] bytes, int byteIndex, int byteCount)
182         {
183                 if (bytes == null)
184                         throw new ArgumentNullException ("bytes");
185                 if (byteIndex < 0 || bytes.Length <= byteIndex)
186                         throw new ArgumentOutOfRangeException ("byteIndex");
187                 if (byteCount < 0 || bytes.Length < byteIndex + byteCount)
188                         throw new ArgumentOutOfRangeException ("byteCount");
189         }
190
191         unsafe void CheckArguments (char* chars, int charCount, byte* bytes, int byteCount)
192         {
193                 if (chars == null)
194                         throw new ArgumentNullException ("chars");
195                 if (bytes == null)
196                         throw new ArgumentNullException ("bytes");
197                 if (charCount < 0)
198                         throw new ArgumentOutOfRangeException ("charCount");
199                 if (byteCount < 0)
200                         throw new ArgumentOutOfRangeException ("byteCount");
201         }
202 #endif
203
204 }; // class Decoder
205
206 }; // namespace System.Text