Beispiel #1
0
        int GetChars(byte[] bytes, int byteIndex, int byteCount,
            char[] chars, int charIndex,
            ref DecoderFallbackBuffer buffer)
        {
            if (bytes == null)
                throw new ArgumentNullException("bytes");
            if (chars == null)
                throw new ArgumentNullException("chars");
            if (byteIndex < 0 || byteIndex > bytes.Length)
                throw new ArgumentOutOfRangeException("byteIndex");
            if (byteCount < 0 || byteCount > (bytes.Length - byteIndex))
                throw new ArgumentOutOfRangeException("byteCount");
            if (charIndex < 0 || charIndex > chars.Length)
                throw new ArgumentOutOfRangeException("charIndex");

            if ((chars.Length - charIndex) < byteCount)
                throw new ArgumentException("Insufficient Space");

            var count = byteCount;
            while (count-- > 0)
            {
                var c = (char) bytes[byteIndex++];
                if (c < '\x80')
                    chars[charIndex++] = c;
                else
                {
                    if (buffer == null)
                        buffer = DecoderFallback.CreateFallbackBuffer();
                    var thisByte = new byte[] {bytes[byteIndex - 1]};
                    buffer.Fallback(thisByte, 0);
                    while (buffer.Remaining > 0)
                    {
                        if (charIndex < chars.Length)
                        {
                            chars[charIndex++] = buffer.GetNextChar();
                            continue;
                        }
                        throw new ArgumentException(
                            "The output char buffer is too small to contain the " +
                            "decoded characters.");
                    }
                }
            }
            return byteCount;
        }