C#有效读取流内容并限制读取量

时间:2019-02-12 11:58:01

标签: c# stream memorystream

在某些情况下,Web API调用会返回很大的字符串响应。我拨打电话如下:

var multipartContent = new MultipartFormDataContent();
multipartContent.Add(new ByteArrayContent(blobStream.CopyToBytes()), 
                         "upload", Path.GetFileName(fileName));

var response = await _httpClient.PostAsync("api/v1/textResponse", multipartContent);
int responeLength = response.Content.Headers.ContentLength.HasValue ? 
                    (int)response.Content.Headers.ContentLength.Value : -1;

response.EnsureSuccessStatusCode();

我只需要处理响应中的前1Mb数据,因此,如果响应小于1Mb,我将读取所有内容,但如果更多,我将很难以1Mb停止读取。

我正在寻找最有效的阅读方法。我已经尝试过以下代码:

// section above...

response.EnsureSuccessStatusCode();

string contentText = null;

if (responeLength < maxAllowedLimit) // 1Mb
{
     // less then limit - read all as string.
     contentText = await response.Content.ReadAsStringAsync();
} 
else {
     var contentStream = await response.Content.ReadAsStreamAsync();
     using (var stream = new MemoryStream())
     {
         byte[] buffer = new byte[5120]; // read in chunks of 5KB
         int bytesRead;
         while((bytesRead = contentStream.Read(buffer, 0, buffer.Length)) > 0)
         {
             stream.Write(buffer, 0, bytesRead);
         }
         contentText = stream.ConvertToString();
     }
}

这是最有效的方法,如何限制读取的数量(其他)。我试过了这段代码,它总是返回一个空字符串。还有:

ReadAsStringAsync()
ReadAsByteArrayAsync()
ReadAsStreamAsync()
LoadIntoBufferAsync(int size)

这些方法中是否有更有效的方法?

在此先感谢任何指针!

1 个答案:

答案 0 :(得分:2)

我怀疑最有效(但仍然正确)的方法可能是这样的。由于您限制了读取的 bytes 的数量,而不是 characters 的数量,因此这变得更加复杂,因此我们不能使用StreamReader。请注意,我们必须注意不要在代码点中间停止读取-在许多情况下,一个字符使用多个字节表示,并且在中途停止会出现错误。

const int bufferSize = 1024;
var bytes = new byte[bufferSize];
var chars = new char[Encoding.UTF8.GetMaxCharCount(bufferSize)];
var decoder = Encoding.UTF8.GetDecoder();
// We don't know how long the result will be in chars, but one byte per char is a
// reasonable first approximation. This will expand as necessary.
var result = new StringBuilder(maxAllowedLimit);
int totalReadBytes = 0;
using (var stream = await response.Content.ReadAsStreamAsync())
{
    while (totalReadBytes <= maxAllowedLimit)
    {
        int readBytes = await stream.ReadAsync(
            bytes,
            0,
            Math.Min(maxAllowedLimit - totalReadBytes, bytes.Length));

        // We reached the end of the stream
        if (readBytes == 0)
            break;

        totalReadBytes += readBytes;

        int readChars = decoder.GetChars(bytes, 0, readBytes, chars, 0);
        result.Append(chars, 0, readChars);
    }
}

请注意,您可能要使用HttpCompletionOption.ResponseHeadersRead,否则HttpClient会继续下载整个正文。

如果您愿意限制个字符的数量,那么生活会更轻松:

string result;
using (var reader = new StreamReader(await response.Content.ReadAsStreamAsync()))
{
    char[] chars = new char[maxAllowedLimit];
    int read = reader.ReadBlock(chars, 0, chars.Length);
    result = new string(chars, 0, read);
}