2 * Decoder.cs - Implementation of the "System.Text.Decoder" 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 Decoder
37 protected Decoder () {}
39 DecoderFallback fallback = new DecoderReplacementFallback ();
40 DecoderFallbackBuffer fallback_buffer;
43 public DecoderFallback Fallback {
44 get { return fallback; }
47 throw new ArgumentNullException ();
49 fallback_buffer = null;
54 public DecoderFallbackBuffer FallbackBuffer {
56 if (fallback_buffer == null)
57 fallback_buffer = fallback.CreateFallbackBuffer ();
58 return fallback_buffer;
62 // Get the number of characters needed to decode a buffer.
63 public abstract int GetCharCount (byte[] bytes, int index, int count);
65 // Get the characters that result from decoding a buffer.
66 public abstract int GetChars (byte[] bytes, int byteIndex, int byteCount,
67 char[] chars, int charIndex);
70 public virtual int GetCharCount (byte [] bytes, int index, int count, bool flush)
74 return GetCharCount (bytes, index, count);
77 [CLSCompliant (false)]
79 public unsafe virtual int GetCharCount (byte* bytes, int count, bool flush)
82 throw new ArgumentNullException ("bytes");
84 throw new ArgumentOutOfRangeException ("count");
86 byte [] barr = new byte [count];
87 Marshal.Copy ((IntPtr) bytes, barr, 0, count);
88 return GetCharCount (barr, 0, count, flush);
91 public virtual int GetChars (
92 byte[] bytes, int byteIndex, int byteCount,
93 char[] chars, int charIndex, bool flush)
95 CheckArguments (bytes, byteIndex, byteCount);
96 CheckArguments (chars, charIndex);
100 return GetChars (bytes, byteIndex, byteCount, chars, charIndex);
103 [CLSCompliant (false)]
105 public unsafe virtual int GetChars (byte* bytes, int byteCount,
106 char* chars, int charCount, bool flush)
108 CheckArguments (chars, charCount, bytes, byteCount);
110 char [] carr = new char [charCount];
111 Marshal.Copy ((IntPtr) chars, carr, 0, charCount);
112 byte [] barr = new byte [byteCount];
113 Marshal.Copy ((IntPtr) bytes, barr, 0, byteCount);
114 return GetChars (barr, 0, byteCount, carr, 0, flush);
118 public virtual void Reset ()
120 if (fallback_buffer != null)
121 fallback_buffer.Reset ();
124 [CLSCompliant (false)]
126 public unsafe virtual void Convert (
127 byte* bytes, int byteCount,
128 char* chars, int charCount, bool flush,
129 out int bytesUsed, out int charsUsed, out bool completed)
131 CheckArguments (chars, charCount, bytes, byteCount);
133 bytesUsed = byteCount;
135 charsUsed = GetCharCount (bytes, bytesUsed, flush);
136 if (charsUsed <= charCount)
141 completed = bytesUsed == byteCount;
142 charsUsed = GetChars (bytes, bytesUsed, chars, charCount, flush);
146 public virtual void Convert (
147 byte [] bytes, int byteIndex, int byteCount,
148 char [] chars, int charIndex, int charCount, bool flush,
149 out int bytesUsed, out int charsUsed, out bool completed)
151 CheckArguments (bytes, byteIndex, byteCount);
153 throw new ArgumentNullException ("chars");
155 throw new ArgumentOutOfRangeException ("charIndex");
156 if (charCount < 0 || chars.Length < charIndex + charCount)
157 throw new ArgumentOutOfRangeException ("charCount");
159 bytesUsed = byteCount;
161 charsUsed = GetCharCount (bytes, byteIndex, bytesUsed, flush);
162 if (charsUsed <= charCount)
167 completed = bytesUsed == byteCount;
168 charsUsed = GetChars (bytes, byteIndex, bytesUsed, chars, charIndex, flush);
171 void CheckArguments (char [] chars, int charIndex)
174 throw new ArgumentNullException ("chars");
175 if (charIndex < 0 || chars.Length < charIndex)
176 throw new ArgumentOutOfRangeException ("charIndex");
179 void CheckArguments (byte [] bytes, int byteIndex, int byteCount)
182 throw new ArgumentNullException ("bytes");
184 throw new ArgumentOutOfRangeException ("byteIndex");
185 if (byteCount < 0 || bytes.Length < byteIndex + byteCount)
186 throw new ArgumentOutOfRangeException ("byteCount");
189 unsafe void CheckArguments (char* chars, int charCount, byte* bytes, int byteCount)
192 throw new ArgumentNullException ("chars");
194 throw new ArgumentNullException ("bytes");
196 throw new ArgumentOutOfRangeException ("charCount");
198 throw new ArgumentOutOfRangeException ("byteCount");
202 }; // namespace System.Text