Example #1
0
        // Token: 0x0600159A RID: 5530 RVA: 0x0007FF68 File Offset: 0x0007E168
        internal static int CopyStream(Stream source, Stream target, Encoding encoding, int charCount, bool needCrc, out uint crc)
        {
            crc = 0U;
            byte[] array = null;
            StreamHelper.CharArrayBuffer charArrayBuffer = null;
            int num = 0;

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

            try
            {
                array           = StreamHelper.bytesBufferPool.Acquire();
                charArrayBuffer = StreamHelper.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 (StreamHelper.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 message = string.Format(CultureInfo.InvariantCulture, "Invalid surrogate chars: first={0}, second={1}", new object[]
                                {
                                    c2,
                                    c
                                });
                                AirSyncDiagnostics.TraceDebug(ExTraceGlobals.RequestsTracer, null, message);
                            }
                            num3++;
                        }
                    }
                    num += num3;
                    int bytes = encoding.GetBytes(charArrayBuffer.Buffer, 0, num3, array, 0);
                    target.Write(array, 0, bytes);
                    if (needCrc)
                    {
                        crc = StreamHelper.UpdateCrc32(crc, array, 0, bytes);
                    }
                }while (num < charCount);
                result = num;
            }
            finally
            {
                if (array != null)
                {
                    StreamHelper.bytesBufferPool.Release(array);
                }
                if (charArrayBuffer != null)
                {
                    charArrayBuffer.Clear();
                    StreamHelper.charsBufferPool.Release(charArrayBuffer);
                }
            }
            return(result);
        }
Example #2
0
        // Token: 0x06001599 RID: 5529 RVA: 0x0007FE28 File Offset: 0x0007E028
        public 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;
            StreamHelper.CharArrayBuffer charArrayBuffer = null;
            int num  = 0;
            int num2 = 0;
            int result;

            try
            {
                array           = StreamHelper.bytesBufferPool.Acquire();
                charArrayBuffer = StreamHelper.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)
                {
                    StreamHelper.bytesBufferPool.Release(array);
                }
                if (charArrayBuffer != null)
                {
                    charArrayBuffer.Clear();
                    StreamHelper.charsBufferPool.Release(charArrayBuffer);
                }
            }
            return(result);
        }