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;
35 public abstract class Decoder
39 protected Decoder () {}
42 DecoderFallback fallback = new DecoderReplacementFallback ();
43 DecoderFallbackBuffer fallback_buffer;
46 public DecoderFallback Fallback {
47 get { return fallback; }
50 throw new ArgumentNullException ();
52 fallback_buffer = null;
57 public DecoderFallbackBuffer FallbackBuffer {
59 if (fallback_buffer == null)
60 fallback_buffer = fallback.CreateFallbackBuffer ();
61 return fallback_buffer;
66 // Get the number of characters needed to decode a buffer.
67 public abstract int GetCharCount (byte[] bytes, int index, int count);
69 // Get the characters that result from decoding a buffer.
70 public abstract int GetChars (byte[] bytes, int byteIndex, int byteCount,
71 char[] chars, int charIndex);
75 public virtual int GetCharCount (byte [] bytes, int index, int count, bool flush)
79 return GetCharCount (bytes, index, count);
82 [CLSCompliant (false)]
84 public unsafe virtual int GetCharCount (byte* bytes, int count, bool flush)
87 throw new ArgumentNullException ("bytes");
89 throw new ArgumentOutOfRangeException ("count");
91 byte [] barr = new byte [count];
92 Marshal.Copy ((IntPtr) bytes, barr, 0, count);
93 return GetCharCount (barr, 0, count, flush);
96 public virtual int GetChars (
97 byte[] bytes, int byteIndex, int byteCount,
98 char[] chars, int charIndex, bool flush)
100 CheckArguments (bytes, byteIndex, byteCount);
101 CheckArguments (chars, charIndex);
105 return GetChars (bytes, byteIndex, byteCount, chars, charIndex);
108 [CLSCompliant (false)]
110 public unsafe virtual int GetChars (byte* bytes, int byteCount,
111 char* chars, int charCount, bool flush)
113 CheckArguments (chars, charCount, bytes, byteCount);
115 char [] carr = new char [charCount];
116 Marshal.Copy ((IntPtr) chars, carr, 0, charCount);
117 byte [] barr = new byte [byteCount];
118 Marshal.Copy ((IntPtr) bytes, barr, 0, byteCount);
119 return GetChars (barr, 0, byteCount, carr, 0, flush);
123 public virtual void Reset ()
125 if (fallback_buffer != null)
126 fallback_buffer.Reset ();
129 [CLSCompliant (false)]
131 public unsafe virtual void Convert (
132 byte* bytes, int byteCount,
133 char* chars, int charCount, bool flush,
134 out int bytesUsed, out int charsUsed, out bool completed)
136 CheckArguments (chars, charCount, bytes, byteCount);
138 bytesUsed = byteCount;
140 charsUsed = GetCharCount (bytes, bytesUsed, flush);
141 if (charsUsed <= charCount)
146 completed = bytesUsed == byteCount;
147 charsUsed = GetChars (bytes, bytesUsed, chars, charCount, flush);
151 public virtual void Convert (
152 byte [] bytes, int byteIndex, int byteCount,
153 char [] chars, int charIndex, int charCount, bool flush,
154 out int bytesUsed, out int charsUsed, out bool completed)
156 CheckArguments (bytes, byteIndex, byteCount);
157 CheckArguments (chars, charIndex);
158 if (charCount < 0 || chars.Length < charIndex + charCount)
159 throw new ArgumentOutOfRangeException ("charCount");
161 bytesUsed = byteCount;
163 charsUsed = GetCharCount (bytes, byteIndex, bytesUsed, flush);
164 if (charsUsed <= charCount)
169 completed = bytesUsed == byteCount;
170 charsUsed = GetChars (bytes, byteIndex, bytesUsed, chars, charIndex, flush);
173 void CheckArguments (char [] chars, int charIndex)
176 throw new ArgumentNullException ("chars");
177 if (charIndex < 0 || chars.Length <= charIndex)
178 throw new ArgumentOutOfRangeException ("charIndex");
181 void CheckArguments (byte [] bytes, int byteIndex, int byteCount)
184 throw new ArgumentNullException ("bytes");
185 if (byteIndex < 0 || bytes.Length <= byteIndex)
186 throw new ArgumentOutOfRangeException ("byteIndex");
187 if (byteCount < 0 || bytes.Length < byteIndex + byteCount)
188 throw new ArgumentOutOfRangeException ("byteCount");
191 unsafe void CheckArguments (char* chars, int charCount, byte* bytes, int byteCount)
194 throw new ArgumentNullException ("chars");
196 throw new ArgumentNullException ("bytes");
198 throw new ArgumentOutOfRangeException ("charCount");
200 throw new ArgumentOutOfRangeException ("byteCount");
206 }; // namespace System.Text