Пример #1
0
        internal static int CopyStream(Stream source, Stream target, Encoding encoding, int charCount, bool needCrc, out uint crc)
        {
            crc = 0U;
            byte[] array = null;
            WBXmlStreamHelper.CharArrayBuffer charArrayBuffer = null;
            int num = 0;

            if (charCount == 0)
            {
                return(0);
            }
            if (charCount < 0)
            {
                charCount = int.MaxValue;
            }
            int result;

            try
            {
                array           = WBXmlStreamHelper.BytesBufferPool.Acquire();
                charArrayBuffer = WBXmlStreamHelper.CharsBufferPool.Acquire();
                if (array == null)
                {
                    throw new InvalidOperationException("bytesBuffer should never be null.");
                }
                if (charArrayBuffer == null)
                {
                    throw new InvalidOperationException("charsBuffer should never be null.");
                }
                StreamReader streamReader = new StreamReader(source, encoding);
                do
                {
                    int num2 = (6144 > charCount - num - 1) ? (charCount - num - 1) : 6144;
                    if (num2 == 0)
                    {
                        num2 = 1;
                    }
                    int num3 = streamReader.ReadBlock(charArrayBuffer.Buffer, 0, num2);
                    if (num3 == 0)
                    {
                        break;
                    }
                    if (WBXmlStreamHelper.NeedToHandleSurrogate(charArrayBuffer.Buffer, num2, num3))
                    {
                        int num4 = streamReader.Read();
                        if (num4 != -1)
                        {
                            charArrayBuffer.Buffer[num3] = (char)num4;
                            char c  = charArrayBuffer.Buffer[num3];
                            char c2 = charArrayBuffer.Buffer[num3 - 1];
                            if (!char.IsSurrogatePair(c2, c))
                            {
                                string.Format(CultureInfo.InvariantCulture, "Invalid surrogate chars: first={0}, second={1}", new object[]
                                {
                                    c2,
                                    c
                                });
                            }
                            num3++;
                        }
                    }
                    num += num3;
                    int bytes = encoding.GetBytes(charArrayBuffer.Buffer, 0, num3, array, 0);
                    target.Write(array, 0, bytes);
                    if (needCrc)
                    {
                        crc = WBXmlStreamHelper.UpdateCrc32(crc, array, 0, bytes);
                    }
                }while (num < charCount);
                result = num;
            }
            finally
            {
                if (array != null)
                {
                    WBXmlStreamHelper.BytesBufferPool.Release(array);
                }
                if (charArrayBuffer != null)
                {
                    charArrayBuffer.Clear();
                    WBXmlStreamHelper.CharsBufferPool.Release(charArrayBuffer);
                }
            }
            return(result);
        }
Пример #2
0
        internal static int CopyStreamWithBase64Conversion(Stream source, Stream target, int byteCount, bool encode)
        {
            if (byteCount == 0)
            {
                return(0);
            }
            if (byteCount < 0)
            {
                byteCount = int.MaxValue;
            }
            byte[] array = null;
            WBXmlStreamHelper.CharArrayBuffer charArrayBuffer = null;
            int num  = 0;
            int num2 = 0;
            int result;

            try
            {
                array           = WBXmlStreamHelper.BytesBufferPool.Acquire();
                charArrayBuffer = WBXmlStreamHelper.CharsBufferPool.Acquire();
                if (array == null)
                {
                    throw new InvalidOperationException("bytesBuffer should never be null.");
                }
                if (charArrayBuffer == null)
                {
                    throw new InvalidOperationException("base64CharsBuffer should never be null.");
                }
                do
                {
                    int count = (18432 > byteCount - num) ? (byteCount - num) : 18432;
                    int num3  = source.Read(array, 0, count);
                    if (num3 == 0)
                    {
                        break;
                    }
                    if (encode)
                    {
                        int charCount = Convert.ToBase64CharArray(array, 0, num3, charArrayBuffer.Buffer, 0);
                        int bytes     = Encoding.UTF8.GetBytes(charArrayBuffer.Buffer, 0, charCount, array, 0);
                        target.Write(array, 0, bytes);
                        num2 += bytes;
                    }
                    else
                    {
                        int    chars  = Encoding.UTF8.GetChars(array, 0, num3, charArrayBuffer.Buffer, 0);
                        byte[] array2 = Convert.FromBase64CharArray(charArrayBuffer.Buffer, 0, chars);
                        target.Write(array2, 0, array2.Length);
                        num2 += array2.Length;
                    }
                    num += num3;
                }while (num < byteCount);
                result = num2;
            }
            finally
            {
                if (array != null)
                {
                    WBXmlStreamHelper.BytesBufferPool.Release(array);
                }
                if (charArrayBuffer != null)
                {
                    charArrayBuffer.Clear();
                    WBXmlStreamHelper.CharsBufferPool.Release(charArrayBuffer);
                }
            }
            return(result);
        }