tencentyun / qcloud-sdk-dotnet

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

GetObjectBytesResult GetObject(GetObjectBytesRequest request) 有bug

sitsh opened this issue · comments

GetObjectBytesResult GetObject(GetObjectBytesRequest request) 有bug

返回的数据byte[] 不完整 不是 bucket 中存储的原始数据。代码中只读取了部分 byte[]

GetObjectBytesResult.cs

internal override void ParseResponseBody(Stream inputStream, string contentType, long contentLength) 有错误的方法

//content = new byte[contentLength];
            //int recvLen = inputStream.Read(content, 0, content.Length);
            //int completed = 0;
            //while (recvLen != 0)
            //{
            //    completed += recvLen;
            //    if (progressCallback != null)
            //    {
            //        progressCallback(completed, content.Length);
            //    }
            //    recvLen = inputStream.Read(content, recvLen, content.Length - completed);
            //}

就是 上面这段读取代码有问题。

用下面这段 读取代码替换 能修复bug 获取完整的 byte[]



public static byte[] ReadToEnd(System.IO.Stream stream)
        {
            long originalPosition = 0;

            if (stream.CanSeek)
            {
                originalPosition = stream.Position;
                stream.Position = 0;
            }

            try
            {
                byte[] readBuffer = new byte[4096];

                int totalBytesRead = 0;
                int bytesRead;

                while ((bytesRead = stream.Read(readBuffer, totalBytesRead, readBuffer.Length - totalBytesRead)) > 0)
                {
                    totalBytesRead += bytesRead;

                    if (totalBytesRead == readBuffer.Length)
                    {
                        int nextByte = stream.ReadByte();
                        if (nextByte != -1)
                        {
                            byte[] temp = new byte[readBuffer.Length * 2];
                            Buffer.BlockCopy(readBuffer, 0, temp, 0, readBuffer.Length);
                            Buffer.SetByte(temp, totalBytesRead, (byte)nextByte);
                            readBuffer = temp;
                            totalBytesRead++;
                        }
                    }
                }

                byte[] buffer = readBuffer;
                if (readBuffer.Length != totalBytesRead)
                {
                    buffer = new byte[totalBytesRead];
                    Buffer.BlockCopy(readBuffer, 0, buffer, 0, totalBytesRead);
                }
                return buffer;
            }
            finally
            {
                if (stream.CanSeek)
                {
                    stream.Position = originalPosition;
                }
            }
        }
commented

问题已经解决,用新版 5.4.6 解决