{"id":404636,"date":"2024-06-29T17:49:28","date_gmt":"2024-06-29T17:49:28","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=404636"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=404636","title":{"rendered":"<span>Sharing .NET-experience: Streams pooling by using Microsoft.IO.RecyclableMemoryStream<\/span>"},"content":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p>The pooling is the good practice to reuse created resources instead of a new allocation. I found <a href=\"https:\/\/github.com\/Microsoft\/Microsoft.IO.RecyclableMemoryStream\" rel=\"noopener noreferrer nofollow\">Microsoft.IO.RecyclableMemoryStream<\/a> package for using memory streams pooling during logging HTTP-requests in my server.  <\/p>\n<pre><code class=\"cs\">RecyclableMemoryStreamManager _recyclableMemoryStreamManager = new RecyclableMemoryStreamManager(); using var memoryStream = _recyclableMemoryStreamManager.GetStream(); bodyStream.Seek(0, SeekOrigin.Begin); bodyStream.CopyTo(memoryStream); memoryStream.Seek(0, SeekOrigin.Begin); string responseBody;  const int readChunkBufferLength = 4096; using var textWriter = new StringWriter();  using (var streamReader = new StreamReader(memoryStream)) {     var readChunk = new char[readChunkBufferLength];     int readChunkLength;     do     {         readChunkLength = streamReader.ReadBlock(readChunk,         0,         readChunkBufferLength);         textWriter.Write(readChunk, 0, readChunkLength);     }     while (readChunkLength > 0); } bodyStream.Seek(0, SeekOrigin.Begin); responseBody = textWriter.ToString();<\/code><\/pre>\n<\/p>\n<\/div>\n<\/div>\n<\/div>\n<p><!----><!----><\/div>\n<p><!----><!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/articles\/697570\/\"> https:\/\/habr.com\/ru\/articles\/697570\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p>The pooling is the good practice to reuse created resources instead of a new allocation. I found <a href=\"https:\/\/github.com\/Microsoft\/Microsoft.IO.RecyclableMemoryStream\" rel=\"noopener noreferrer nofollow\">Microsoft.IO.RecyclableMemoryStream<\/a> package for using memory streams pooling during logging HTTP-requests in my server.  <\/p>\n<pre><code class=\"cs\">RecyclableMemoryStreamManager _recyclableMemoryStreamManager = new RecyclableMemoryStreamManager(); using var memoryStream = _recyclableMemoryStreamManager.GetStream(); bodyStream.Seek(0, SeekOrigin.Begin); bodyStream.CopyTo(memoryStream); memoryStream.Seek(0, SeekOrigin.Begin); string responseBody;  const int readChunkBufferLength = 4096; using var textWriter = new StringWriter();  using (var streamReader = new StreamReader(memoryStream)) {     var readChunk = new char[readChunkBufferLength];     int readChunkLength;     do     {         readChunkLength = streamReader.ReadBlock(readChunk,         0,         readChunkBufferLength);         textWriter.Write(readChunk, 0, readChunkLength);     }     while (readChunkLength > 0); } bodyStream.Seek(0, SeekOrigin.Begin); responseBody = textWriter.ToString();<\/code><\/pre>\n<\/p>\n<\/div>\n<\/div>\n<\/div>\n<p><!----><!----><\/div>\n<p><!----><!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/articles\/697570\/\"> https:\/\/habr.com\/ru\/articles\/697570\/<\/a><br \/><\/br><\/br><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-404636","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/404636","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=404636"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/404636\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=404636"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=404636"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=404636"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}