Ejemplo n.º 1
0
        private unsafe int InternalReadChars(char[] buffer, int index, int count)
        {
            int charCount = count;

            if (this.m_charBytes == null)
            {
                this.m_charBytes = new byte[128];
            }
            while (charCount > 0)
            {
                int        count1     = charCount;
                DecoderNLS decoderNls = this.m_decoder as DecoderNLS;
                if (decoderNls != null && decoderNls.HasState && count1 > 1)
                {
                    --count1;
                }
                if (this.m_2BytesPerChar)
                {
                    count1 <<= 1;
                }
                if (count1 > 128)
                {
                    count1 = 128;
                }
                int    num = 0;
                int    byteCount;
                byte[] numArray;
                if (this.m_isMemoryStream)
                {
                    MemoryStream memoryStream = this.m_stream as MemoryStream;
                    num = memoryStream.InternalGetPosition();
                    int count2 = count1;
                    byteCount = memoryStream.InternalEmulateRead(count2);
                    numArray  = memoryStream.InternalGetBuffer();
                }
                else
                {
                    byteCount = this.m_stream.Read(this.m_charBytes, 0, count1);
                    numArray  = this.m_charBytes;
                }
                if (byteCount == 0)
                {
                    return(count - charCount);
                }
                int chars;

                fixed(byte *numPtr = numArray)
                fixed(char *chPtr = buffer)
                chars             = this.m_decoder.GetChars(numPtr + num, byteCount, chPtr + index, charCount, false);

                charCount -= chars;
                index     += chars;
            }
            return(count - charCount);
        }
Ejemplo n.º 2
0
        private int InternalReadChars(char[] buffer, int index, int count)
        {
            Contract.Requires(buffer != null);
            Contract.Requires(index >= 0 && count >= 0);
            Contract.Assert(m_stream != null);

            int numBytes       = 0;
            int charsRemaining = count;

            if (m_charBytes == null)
            {
                m_charBytes = new byte[MaxCharBytesSize];
            }

            while (charsRemaining > 0)
            {
                int charsRead = 0;
                // We really want to know what the minimum number of bytes per char
                // is for our encoding.  Otherwise for UnicodeEncoding we'd have to
                // do ~1+log(n) reads to read n characters.
                numBytes = charsRemaining;

                // special case for DecoderNLS subclasses when there is a hanging byte from the previous loop
                DecoderNLS decoder = m_decoder as DecoderNLS;
                if (decoder != null && decoder.HasState && numBytes > 1)
                {
                    numBytes -= 1;
                }

                if (m_2BytesPerChar)
                {
                    numBytes <<= 1;
                }
                if (numBytes > MaxCharBytesSize)
                {
                    numBytes = MaxCharBytesSize;
                }

                int    position   = 0;
                byte[] byteBuffer = null;
                if (m_isMemoryStream)
                {
                    MemoryStream mStream = m_stream as MemoryStream;
                    Contract.Assert(mStream != null, "m_stream as MemoryStream != null");

                    position   = mStream.InternalGetPosition();
                    numBytes   = mStream.InternalEmulateRead(numBytes);
                    byteBuffer = mStream.InternalGetBuffer();
                }
                else
                {
                    numBytes   = m_stream.Read(m_charBytes, 0, numBytes);
                    byteBuffer = m_charBytes;
                }

                if (numBytes == 0)
                {
                    return(count - charsRemaining);
                }

                Contract.Assert(byteBuffer != null, "expected byteBuffer to be non-null");

                checked {
                    if (position < 0 || numBytes < 0 || position + numBytes > byteBuffer.Length)
                    {
                        throw new ArgumentOutOfRangeException("byteCount");
                    }

                    if (index < 0 || charsRemaining < 0 || index + charsRemaining > buffer.Length)
                    {
                        throw new ArgumentOutOfRangeException("charsRemaining");
                    }

                    unsafe
                    {
                        fixed(byte *pBytes = byteBuffer)
                        {
                            fixed(char *pChars = buffer)
                            {
                                charsRead = m_decoder.GetChars(pBytes + position, numBytes, pChars + index, charsRemaining, false);
                            }
                        }
                    }
                }

                charsRemaining -= charsRead;
                index          += charsRead;
            }

            // this should never fail
            Contract.Assert(charsRemaining >= 0, "We read too many characters.");

            // we may have read fewer than the number of characters requested if end of stream reached
            // or if the encoding makes the char count too big for the buffer (e.g. fallback sequence)
            return(count - charsRemaining);
        }
Ejemplo n.º 3
0
        private int InternalReadChars(char[] buffer, int index, int count)
        {
            int numBytes       = 0;
            int charsRemaining = count;

            if (m_charBytes == null)
            {
                m_charBytes = new byte[MaxCharBytesSize];
            }

            while (charsRemaining > 0)
            {
                int charsRead = 0;
                // We really want to know what the minimum number of bytes per char
                // is for our encoding.  Otherwise for UnicodeEncoding we'd have to
                // do ~1+log(n) reads to read n characters.
                numBytes = charsRemaining;

                // special case for DecoderNLS subclasses when there is a hanging byte from the previous loop
                DecoderNLS decoder = m_decoder as DecoderNLS;
                if (decoder != null && decoder.HasState && numBytes > 1)
                {
                    numBytes -= 1;
                }

                if (m_2BytesPerChar)
                {
                    numBytes <<= 1;
                }
                if (numBytes > MaxCharBytesSize)
                {
                    numBytes = MaxCharBytesSize;
                }

                int    position   = 0;
                byte[] byteBuffer = null;
                if (m_isMemoryStream)
                {
                    MemoryStream mStream = m_stream as MemoryStream;

                    position   = mStream.InternalGetPosition();
                    numBytes   = mStream.InternalEmulateRead(numBytes);
                    byteBuffer = mStream.InternalGetBuffer();
                }
                else
                {
                    numBytes   = m_stream.Read(m_charBytes, 0, numBytes);
                    byteBuffer = m_charBytes;
                }

                if (numBytes == 0)
                {
                    return(count - charsRemaining);
                }

                unsafe
                {
                    fixed(byte *pBytes = byteBuffer)
                    fixed(char *pChars = buffer)
                    {
                        charsRead = m_decoder.GetChars(pBytes + position, numBytes, pChars + index, charsRemaining, false);
                    }
                }

                charsRemaining -= charsRead;
                index          += charsRead;
            }

            // we may have read fewer than the number of characters requested if end of stream reached
            // or if the encoding makes the char count too big for the buffer (e.g. fallback sequence)
            return(count - charsRemaining);
        }
Ejemplo n.º 4
0
        private unsafe int InternalReadChars(char[] buffer, int index, int count)
        {
            int i = count;

            if (this.m_charBytes == null)
            {
                this.m_charBytes = new byte[128];
            }
            while (i > 0)
            {
                int        num        = i;
                DecoderNLS decoderNLS = this.m_decoder as DecoderNLS;
                if (decoderNLS != null && decoderNLS.HasState && num > 1)
                {
                    num--;
                }
                if (this.m_2BytesPerChar)
                {
                    num <<= 1;
                }
                if (num > 128)
                {
                    num = 128;
                }
                int    num2 = 0;
                byte[] array;
                if (this.m_isMemoryStream)
                {
                    MemoryStream memoryStream = this.m_stream as MemoryStream;
                    num2  = memoryStream.InternalGetPosition();
                    num   = memoryStream.InternalEmulateRead(num);
                    array = memoryStream.InternalGetBuffer();
                }
                else
                {
                    num   = this.m_stream.Read(this.m_charBytes, 0, num);
                    array = this.m_charBytes;
                }
                if (num == 0)
                {
                    return(count - i);
                }
                int chars;
                checked
                {
                    if (num2 < 0 || num < 0 || num2 + num > array.Length)
                    {
                        throw new ArgumentOutOfRangeException("byteCount");
                    }
                    if (index < 0 || i < 0 || index + i > buffer.Length)
                    {
                        throw new ArgumentOutOfRangeException("charsRemaining");
                    }

                    fixed(byte *ptr = array)
                    {
                        fixed(char *ptr2 = buffer)
                        {
                            chars = this.m_decoder.GetChars(ptr + num2, num, ptr2 + index, i, false);
                        }
                    }
                }
                i     -= chars;
                index += chars;
            }
            return(count - i);
        }