2009-12-02 Jb Evain <jbevain@novell.com>
[mcs.git] / class / corlib / System.Text / Encoder.cs
blob683444b16bc577e0745ca91da1269cbdaae8a08e
1 /*
2 * Encoder.cs - Implementation of the "System.Text.Encoder" class.
4 * Copyright (c) 2001 Southern Storm Software, Pty Ltd
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:
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
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.
25 namespace System.Text
28 using System;
29 using System.Runtime.InteropServices;
31 [Serializable]
32 [ComVisible (true)]
33 public abstract class Encoder
36 // Constructor.
37 protected Encoder() {}
39 EncoderFallback fallback = new EncoderReplacementFallback ();
40 EncoderFallbackBuffer fallback_buffer;
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;
53 [ComVisible (false)]
54 public EncoderFallbackBuffer FallbackBuffer {
55 get {
56 if (fallback_buffer == null)
57 fallback_buffer = Fallback.CreateFallbackBuffer ();
58 return fallback_buffer;
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);
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);
70 [CLSCompliant (false)]
71 [ComVisible (false)]
72 public unsafe virtual int GetByteCount (char* chars, int count, bool flush)
74 if (chars == null)
75 throw new ArgumentNullException ("chars");
76 if (count < 0)
77 throw new ArgumentOutOfRangeException ("count");
79 char [] carr = new char [count];
80 Marshal.Copy ((IntPtr) chars, carr, 0, count);
81 return GetByteCount (carr, 0, count, flush);
84 [CLSCompliant (false)]
85 [ComVisible (false)]
86 public unsafe virtual int GetBytes (char* chars, int charCount,
87 byte* bytes, int byteCount, bool flush)
89 CheckArguments (chars, charCount, bytes, byteCount);
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);
98 [ComVisible (false)]
99 public virtual void Reset ()
101 if (fallback_buffer != null)
102 fallback_buffer.Reset ();
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)
112 CheckArguments (chars, charCount, bytes, byteCount);
114 charsUsed = charCount;
115 while (true) {
116 bytesUsed = GetByteCount (chars, charsUsed, flush);
117 if (bytesUsed <= byteCount)
118 break;
119 flush = false;
120 charsUsed >>= 1;
122 completed = charsUsed == charCount;
123 bytesUsed = GetBytes (chars, charsUsed, bytes, byteCount, flush);
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)
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");
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;
153 completed = charsUsed == charCount;
154 bytesUsed = GetBytes (chars, charIndex, charsUsed, bytes, byteIndex, flush);
157 unsafe void CheckArguments (char* chars, int charCount, byte* bytes, int byteCount)
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");
168 }; // class Encoder
170 }; // namespace System.Text