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;
35 public abstract class Encoder
39 protected Encoder() {}
42 EncoderFallback fallback = new EncoderReplacementFallback ();
43 EncoderFallbackBuffer fallback_buffer;
46 public EncoderFallback Fallback {
47 get { return fallback; }
50 throw new ArgumentNullException ();
52 fallback_buffer = null;
57 public EncoderFallbackBuffer FallbackBuffer {
59 if (fallback_buffer == null)
60 fallback_buffer = Fallback.CreateFallbackBuffer ();
61 return fallback_buffer;
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);
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);
75 [CLSCompliant (false)]
77 public unsafe virtual int GetByteCount (char* chars, int charCount, bool flush)
80 throw new ArgumentNullException ("chars");
82 throw new ArgumentOutOfRangeException ("charCount");
84 char [] carr = new char [charCount];
85 Marshal.Copy ((IntPtr) chars, carr, 0, charCount);
86 return GetByteCount (carr, 0, charCount, flush);
89 [CLSCompliant (false)]
91 public unsafe virtual int GetBytes (char* chars, int charCount,
92 byte* bytes, int byteCount, bool flush)
94 CheckArguments (chars, charCount, bytes, byteCount);
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);
104 public virtual void Reset ()
106 if (fallback_buffer != null)
107 fallback_buffer.Reset ();
110 [CLSCompliant (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)
117 CheckArguments (chars, charCount, bytes, byteCount);
119 charsUsed = charCount;
121 bytesUsed = GetByteCount (chars, charsUsed, flush);
122 if (bytesUsed <= byteCount)
127 completed = charsUsed == charCount;
128 bytesUsed = GetBytes (chars, charsUsed, bytes, byteCount, flush);
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)
138 throw new ArgumentNullException ("chars");
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");
150 charsUsed = charCount;
152 bytesUsed = GetByteCount (chars, charIndex, charsUsed, flush);
153 if (bytesUsed <= byteCount)
158 completed = charsUsed == charCount;
159 bytesUsed = GetBytes (chars, charIndex, charsUsed, bytes, byteIndex, flush);
162 unsafe void CheckArguments (char* chars, int charCount, byte* bytes, int byteCount)
165 throw new ArgumentNullException ("chars");
167 throw new ArgumentNullException ("bytes");
169 throw new ArgumentOutOfRangeException ("charCount");
171 throw new ArgumentOutOfRangeException ("byteCount");
176 }; // namespace System.Text