New test.
[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 #if NET_2_0
33 [ComVisible (true)]
34 #endif
35 public abstract class Encoder
36 {
37
38         // Constructor.
39         protected Encoder() {}
40
41 #if NET_2_0
42         EncoderFallback fallback = new EncoderReplacementFallback ();
43         EncoderFallbackBuffer fallback_buffer;
44
45         [ComVisible (false)]
46         public EncoderFallback 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 EncoderFallbackBuffer 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 bytes needed to encode a buffer.
67         public abstract int GetByteCount(char[] chars, int index,
68                                                                          int count, bool flush);
69
70         // Get the bytes that result from decoding a buffer.
71         public abstract int GetBytes(char[] chars, int charIndex, int charCount,
72                                                                  byte[] bytes, int byteIndex, bool flush);
73
74 #if NET_2_0
75         [CLSCompliant (false)]
76         [ComVisible (false)]
77         public unsafe virtual int GetByteCount (char* chars, int charCount, bool flush)
78         {
79                 if (chars == null)
80                         throw new ArgumentNullException ("chars");
81                 if (charCount < 0)
82                         throw new ArgumentOutOfRangeException ("charCount");
83
84                 char [] carr = new char [charCount];
85                 Marshal.Copy ((IntPtr) chars, carr, 0, charCount);
86                 return GetByteCount (carr, 0, charCount, flush);
87         }
88
89         [CLSCompliant (false)]
90         [ComVisible (false)]
91         public unsafe virtual int GetBytes (char* chars, int charCount,
92                 byte* bytes, int byteCount, bool flush)
93         {
94                 CheckArguments (chars, charCount, bytes, byteCount);
95
96                 char [] carr = new char [charCount];
97                 Marshal.Copy ((IntPtr) chars, carr, 0, charCount);
98                 byte [] barr = new byte [byteCount];
99                 Marshal.Copy ((IntPtr) bytes, barr, 0, byteCount);
100                 return GetBytes (carr, 0, charCount, barr, 0, flush);
101         }
102
103         [ComVisible (false)]
104         public virtual void Reset ()
105         {
106                 if (fallback_buffer != null)
107                         fallback_buffer.Reset ();
108         }
109
110         [CLSCompliant (false)]
111         [ComVisible (false)]
112         public unsafe virtual void Convert (
113                 char* chars, int charCount,
114                 byte* bytes, int byteCount, bool flush,
115                 out int charsUsed, out int bytesUsed, out bool completed)
116         {
117                 CheckArguments (chars, charCount, bytes, byteCount);
118
119                 charsUsed = charCount;
120                 while (true) {
121                         bytesUsed = GetByteCount (chars, charsUsed, flush);
122                         if (bytesUsed <= byteCount)
123                                 break;
124                         flush = false;
125                         charsUsed >>= 1;
126                 }
127                 completed = charsUsed == charCount;
128                 bytesUsed = GetBytes (chars, charsUsed, bytes, byteCount, flush);
129         }
130
131         [ComVisible (false)]
132         public virtual void Convert (
133                 char [] chars, int charIndex, int charCount,
134                 byte [] bytes, int byteIndex, int byteCount, bool flush,
135                 out int charsUsed, out int bytesUsed, out bool completed)
136         {
137                 if (chars == null)
138                         throw new ArgumentNullException ("chars");
139                 if (bytes == null)
140                         throw new ArgumentNullException ("bytes");
141                 if (charIndex < 0 || chars.Length <= charIndex)
142                         throw new ArgumentOutOfRangeException ("charIndex");
143                 if (charCount < 0 || chars.Length < charIndex + charCount)
144                         throw new ArgumentOutOfRangeException ("charCount");
145                 if (byteIndex < 0 || bytes.Length <= byteIndex)
146                         throw new ArgumentOutOfRangeException ("byteIndex");
147                 if (byteCount < 0 || bytes.Length < byteIndex + byteCount)
148                         throw new ArgumentOutOfRangeException ("byteCount");
149
150                 charsUsed = charCount;
151                 while (true) {
152                         bytesUsed = GetByteCount (chars, charIndex, charsUsed, flush);
153                         if (bytesUsed <= byteCount)
154                                 break;
155                         flush = false;
156                         charsUsed >>= 1;
157                 }
158                 completed = charsUsed == charCount;
159                 bytesUsed = GetBytes (chars, charIndex, charsUsed, bytes, byteIndex, flush);
160         }
161
162         unsafe void CheckArguments (char* chars, int charCount, byte* bytes, int byteCount)
163         {
164                 if (chars == null)
165                         throw new ArgumentNullException ("chars");
166                 if (bytes == null)
167                         throw new ArgumentNullException ("bytes");
168                 if (charCount < 0)
169                         throw new ArgumentOutOfRangeException ("charCount");
170                 if (byteCount < 0)
171                         throw new ArgumentOutOfRangeException ("byteCount");
172         }
173 #endif
174 }; // class Encoder
175
176 }; // namespace System.Text