0

Web サービスのファイルをストリーミングするためのコードで見られる OOM (メモリ不足) の問題に対する素晴らしい提案をありがとうございます。[もう少し詳細を提供する別のスレッドを開始しても問題ないことを願っています。] 提案から、ファイルからの読み取りに使用されるバッファーサイズを縮小しました。 OOM の問題です。この問題は、ファイル サイズが 5MB ほどの小さいサイズでも見られます。10 倍の大きさのファイルを処理したい可能性があります。

私の問題は、TextWriter の使用にあるようです。

次のようにリクエストを作成します [コードを縮小するためにいくつか編集します]:

HttpWebRequest oRequest = (HttpWebRequest)WebRequest.Create(new Uri(strURL));
oRequest.Method = httpMethod;
oRequest.ContentType = "application/atom+xml";
oRequest.Headers["Authorization"] = getAuthHeader();
oRequest.ContentLength = strHead.Length + strTail.Length + longContentSize;
oRequest.SendChunked = true;

using (TextWriter tw = new StreamWriter(oRequest.GetRequestStream()))
{
    tw.Write(strHead);
    using (FileStream fileStream = new FileStream(strPath, FileMode.Open, 
           FileAccess.Read, System.IO.FileShare.ReadWrite))
    {
        StreamEncode(fileStream, tw);
    }
    tw.Write(strTail);
}
.....

ルーチンを呼び出すのは次のとおりです。

public void StreamEncode(FileStream inputStream, TextWriter tw)
{
    // For Base64 there are 4 bytes output for every 3 bytes of input
    byte[] base64Block = new byte[9000];
    int bytesRead = 0;
    string base64String = null;

    do
    {
        // read one block from the input stream
        bytesRead = inputStream.Read(base64Block, 0, base64Block.Length);

        // encode the base64 string
        base64String = Convert.ToBase64String(base64Block, 0, bytesRead);

        // write the string
        tw.Write(base64String);


    } while (bytesRead !=0 );

}

コンテンツが大きくなる可能性があるため、TextWriter 以外のものを使用する必要がありますか? リクエストのペイロード全体を作成できるのは非常に便利です。

これは完全に間違ったアプローチですか?非常に大きなファイルをサポートできるようにしたい。

4

0 に答える 0