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.
29 using System.Runtime.InteropServices;
33 public abstract class Encoder
37 protected Encoder() {}
39 EncoderFallback fallback = new EncoderReplacementFallback ();
40 EncoderFallbackBuffer fallback_buffer;
43 public EncoderFallback Fallback {
44 get { return fallback; }
47 throw new ArgumentNullException ();
49 fallback_buffer = null;
54 public EncoderFallbackBuffer FallbackBuffer {
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)]
72 public unsafe virtual int GetByteCount (char* chars, int count, bool flush)
75 throw new ArgumentNullException ("chars");
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)]
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);
99 public virtual void Reset ()
101 if (fallback_buffer != null)
102 fallback_buffer.Reset ();
105 [CLSCompliant (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;
116 bytesUsed = GetByteCount (chars, charsUsed, flush);
117 if (bytesUsed <= byteCount)
122 completed = charsUsed == charCount;
123 bytesUsed = GetBytes (chars, charsUsed, bytes, byteCount, flush);
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)
133 throw new ArgumentNullException ("chars");
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;
147 bytesUsed = GetByteCount (chars, charIndex, charsUsed, flush);
148 if (bytesUsed <= byteCount)
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)
160 throw new ArgumentNullException ("chars");
162 throw new ArgumentNullException ("bytes");
164 throw new ArgumentOutOfRangeException ("charCount");
166 throw new ArgumentOutOfRangeException ("byteCount");
170 }; // namespace System.Text