Refactorized convert(byte[], int, ...), now uses the pointer version.
[mono.git] / mcs / class / corlib / System.Text / Decoder.cs
index 001a0f5bac38743e9fa004e460a503a615d2726d..bc8e6241cb7413b9c2c8a49a7517ec7661345e7e 100644 (file)
@@ -29,16 +29,17 @@ using System;
 using System.Runtime.InteropServices;
 
 [Serializable]
+[ComVisible (true)]
 public abstract class Decoder
 {
 
        // Constructor.
        protected Decoder () {}
 
-#if NET_2_0
-       DecoderFallback fallback;
+       DecoderFallback fallback = new DecoderReplacementFallback ();
        DecoderFallbackBuffer fallback_buffer;
 
+       [ComVisible (false)]
        public DecoderFallback Fallback {
                get { return fallback; }
                set {
@@ -49,6 +50,7 @@ public abstract class Decoder
                }
        }
 
+       [ComVisible (false)]
        public DecoderFallbackBuffer FallbackBuffer {
                get {
                        if (fallback_buffer == null)
@@ -56,7 +58,6 @@ public abstract class Decoder
                        return fallback_buffer;
                }
        }
-#endif
 
        // Get the number of characters needed to decode a buffer.
        public abstract int GetCharCount (byte[] bytes, int index, int count);
@@ -65,7 +66,7 @@ public abstract class Decoder
        public abstract int GetChars (byte[] bytes, int byteIndex, int byteCount,
                                                                 char[] chars, int charIndex);
 
-#if NET_2_0
+       [ComVisible (false)]
        public virtual int GetCharCount (byte [] bytes, int index, int count, bool flush)
        {
                if (flush)
@@ -74,8 +75,14 @@ public abstract class Decoder
        }
 
        [CLSCompliant (false)]
+       [ComVisible (false)]
        public unsafe virtual int GetCharCount (byte* bytes, int count, bool flush)
        {
+               if (bytes == null)
+                       throw new ArgumentNullException ("bytes");
+               if (count < 0)
+                       throw new ArgumentOutOfRangeException ("count");
+
                byte [] barr = new byte [count];
                Marshal.Copy ((IntPtr) bytes, barr, 0, count);
                return GetCharCount (barr, 0, count, flush);
@@ -85,15 +92,21 @@ public abstract class Decoder
                byte[] bytes, int byteIndex, int byteCount,
                char[] chars, int charIndex, bool flush)
        {
+               CheckArguments (bytes, byteIndex, byteCount);
+               CheckArguments (chars, charIndex);
+
                if (flush)
                        Reset ();
                return GetChars (bytes, byteIndex, byteCount, chars, charIndex);
        }
 
        [CLSCompliant (false)]
+       [ComVisible (false)]
        public unsafe virtual int GetChars (byte* bytes, int byteCount,
                char* chars, int charCount, bool flush)
        {
+               CheckArguments (chars, charCount, bytes, byteCount);
+
                char [] carr = new char [charCount];
                Marshal.Copy ((IntPtr) chars, carr, 0, charCount);
                byte [] barr = new byte [byteCount];
@@ -101,13 +114,86 @@ public abstract class Decoder
                return GetChars (barr, 0, byteCount, carr, 0, flush);
        }
 
+       [ComVisible (false)]
        public virtual void Reset ()
        {
                if (fallback_buffer != null)
                        fallback_buffer.Reset ();
        }
-#endif
 
+       [CLSCompliant (false)]
+       [ComVisible (false)]
+       public unsafe virtual void Convert (
+               byte* bytes, int byteCount,
+               char* chars, int charCount, bool flush,
+               out int bytesUsed, out int charsUsed, out bool completed)
+       {
+               CheckArguments (chars, charCount, bytes, byteCount);
+
+               bytesUsed = byteCount;
+               while (true) {
+                       charsUsed = GetCharCount (bytes, bytesUsed, flush);
+                       if (charsUsed <= charCount)
+                               break;
+                       flush = false;
+                       bytesUsed >>= 1;
+               }
+               completed = bytesUsed == byteCount;
+               charsUsed = GetChars (bytes, bytesUsed, chars, charCount, flush);
+       }
+
+       [ComVisible (false)]
+       public unsafe virtual void Convert (
+               byte [] bytes, int byteIndex, int byteCount,
+               char [] chars, int charIndex, int charCount, bool flush,
+               out int bytesUsed, out int charsUsed, out bool completed)
+       {
+               CheckArguments (bytes, byteIndex, byteCount);
+               CheckArguments (chars, charIndex);
+               if (charCount < 0 || chars.Length < charIndex + charCount)
+                       throw new ArgumentOutOfRangeException ("charCount");
+
+               // refactorize passing control to byte* version
+               fixed (char* cptr = chars) {
+                       fixed (byte* bptr = bytes) {
+                               Convert(bptr + byteIndex, byteCount,
+                                       cptr + charIndex, charCount,
+                                       flush,
+                                       out bytesUsed, out charsUsed,
+                                       out completed);
+                       }
+               }
+       }
+
+       void CheckArguments (char [] chars, int charIndex)
+       {
+               if (chars == null)
+                       throw new ArgumentNullException ("chars");
+               if (charIndex < 0 || chars.Length < charIndex)
+                       throw new ArgumentOutOfRangeException ("charIndex");
+       }
+
+       void CheckArguments (byte [] bytes, int byteIndex, int byteCount)
+       {
+               if (bytes == null)
+                       throw new ArgumentNullException ("bytes");
+               if (byteIndex < 0)
+                       throw new ArgumentOutOfRangeException ("byteIndex");
+               if (byteCount < 0 || bytes.Length < byteIndex + byteCount)
+                       throw new ArgumentOutOfRangeException ("byteCount");
+       }
+
+       unsafe void CheckArguments (char* chars, int charCount, byte* bytes, int byteCount)
+       {
+               if (chars == null)
+                       throw new ArgumentNullException ("chars");
+               if (bytes == null)
+                       throw new ArgumentNullException ("bytes");
+               if (charCount < 0)
+                       throw new ArgumentOutOfRangeException ("charCount");
+               if (byteCount < 0)
+                       throw new ArgumentOutOfRangeException ("byteCount");
+       }
 }; // class Decoder
 
 }; // namespace System.Text