From 2c5ae68ab6e61ebc025c841336a3df41790d241d Mon Sep 17 00:00:00 2001 From: sornaks Date: Tue, 3 Feb 2015 11:25:23 -0800 Subject: [PATCH] Issue #1944: Move DelegatingStream to internal namespace and rename to NonDisposableStream. --- .../Formatters/JsonOutputFormatter.cs | 5 ++-- .../NonDisposableStream.cs} | 8 +++--- ...XmlDataContractSerializerInputFormatter.cs | 3 +- ...mlDataContractSerializerOutputFormatter.cs | 3 +- .../XmlSerializerInputFormatter.cs | 3 +- .../XmlSerializerOutputFormatter.cs | 3 +- ...eamTests.cs => NonDisposableStreamTest.cs} | 28 +++++++++---------- .../ConnegWebSite/VCardFormatter_V3.cs | 3 +- .../ConnegWebSite/VCardFormatter_V4.cs | 3 +- 9 files changed, 33 insertions(+), 26 deletions(-) rename src/Microsoft.AspNet.Mvc.Core/{Formatters/DelegatingStream.cs => Internal/NonDisposableStream.cs} (96%) rename test/Microsoft.AspNet.Mvc.Core.Test/Formatters/{DelegatingStreamTests.cs => NonDisposableStreamTest.cs} (67%) diff --git a/src/Microsoft.AspNet.Mvc.Core/Formatters/JsonOutputFormatter.cs b/src/Microsoft.AspNet.Mvc.Core/Formatters/JsonOutputFormatter.cs index b882f5ce8a..2d5b016b1f 100644 --- a/src/Microsoft.AspNet.Mvc.Core/Formatters/JsonOutputFormatter.cs +++ b/src/Microsoft.AspNet.Mvc.Core/Formatters/JsonOutputFormatter.cs @@ -4,6 +4,7 @@ using System; using System.IO; using System.Threading.Tasks; +using Microsoft.AspNet.Mvc.Internal; using Microsoft.Net.Http.Headers; using Newtonsoft.Json; @@ -68,8 +69,8 @@ namespace Microsoft.AspNet.Mvc var response = context.ActionContext.HttpContext.Response; var selectedEncoding = context.SelectedEncoding; - using (var delegatingStream = new DelegatingStream(response.Body)) - using (var writer = new StreamWriter(delegatingStream, selectedEncoding, 1024, leaveOpen: true)) + using (var nonDisposableStream = new NonDisposableStream(response.Body)) + using (var writer = new StreamWriter(nonDisposableStream, selectedEncoding, 1024, leaveOpen: true)) { WriteObject(writer, context.Object); } diff --git a/src/Microsoft.AspNet.Mvc.Core/Formatters/DelegatingStream.cs b/src/Microsoft.AspNet.Mvc.Core/Internal/NonDisposableStream.cs similarity index 96% rename from src/Microsoft.AspNet.Mvc.Core/Formatters/DelegatingStream.cs rename to src/Microsoft.AspNet.Mvc.Core/Internal/NonDisposableStream.cs index 0be1a3b322..62a5c5a055 100644 --- a/src/Microsoft.AspNet.Mvc.Core/Formatters/DelegatingStream.cs +++ b/src/Microsoft.AspNet.Mvc.Core/Internal/NonDisposableStream.cs @@ -8,22 +8,22 @@ using System.IO; using System.Threading; using System.Threading.Tasks; -namespace Microsoft.AspNet.Mvc +namespace Microsoft.AspNet.Mvc.Internal { /// /// Stream that delegates to an inner stream. /// This Stream is present so that the inner stream is not closed /// even when Close() or Dispose() is called. /// - public class DelegatingStream : Stream + public class NonDisposableStream : Stream { private readonly Stream _innerStream; /// - /// Initializes a new . + /// Initializes a new . /// /// The stream which should not be closed or flushed. - public DelegatingStream([NotNull] Stream innerStream) + public NonDisposableStream([NotNull] Stream innerStream) { _innerStream = innerStream; } diff --git a/src/Microsoft.AspNet.Mvc.Xml/XmlDataContractSerializerInputFormatter.cs b/src/Microsoft.AspNet.Mvc.Xml/XmlDataContractSerializerInputFormatter.cs index f9ae5007fa..8b0aad030c 100644 --- a/src/Microsoft.AspNet.Mvc.Xml/XmlDataContractSerializerInputFormatter.cs +++ b/src/Microsoft.AspNet.Mvc.Xml/XmlDataContractSerializerInputFormatter.cs @@ -10,6 +10,7 @@ using System.Runtime.Serialization; using System.Text; using System.Threading.Tasks; using System.Xml; +using Microsoft.AspNet.Mvc.Internal; using Microsoft.Net.Http.Headers; namespace Microsoft.AspNet.Mvc.Xml @@ -168,7 +169,7 @@ namespace Microsoft.AspNet.Mvc.Xml { var request = context.ActionContext.HttpContext.Request; - using (var xmlReader = CreateXmlReader(new DelegatingStream(request.Body))) + using (var xmlReader = CreateXmlReader(new NonDisposableStream(request.Body))) { var type = GetSerializableType(context.ModelType); diff --git a/src/Microsoft.AspNet.Mvc.Xml/XmlDataContractSerializerOutputFormatter.cs b/src/Microsoft.AspNet.Mvc.Xml/XmlDataContractSerializerOutputFormatter.cs index 1b5da1437a..285d8416d5 100644 --- a/src/Microsoft.AspNet.Mvc.Xml/XmlDataContractSerializerOutputFormatter.cs +++ b/src/Microsoft.AspNet.Mvc.Xml/XmlDataContractSerializerOutputFormatter.cs @@ -7,6 +7,7 @@ using System.IO; using System.Runtime.Serialization; using System.Threading.Tasks; using System.Xml; +using Microsoft.AspNet.Mvc.Internal; using Microsoft.Net.Http.Headers; namespace Microsoft.AspNet.Mvc.Xml @@ -161,7 +162,7 @@ namespace Microsoft.AspNet.Mvc.Xml var innerStream = context.ActionContext.HttpContext.Response.Body; - using (var outputStream = new DelegatingStream(innerStream)) + using (var outputStream = new NonDisposableStream(innerStream)) using (var xmlWriter = CreateXmlWriter(outputStream, tempWriterSettings)) { var obj = context.Object; diff --git a/src/Microsoft.AspNet.Mvc.Xml/XmlSerializerInputFormatter.cs b/src/Microsoft.AspNet.Mvc.Xml/XmlSerializerInputFormatter.cs index d553064849..c187a0412c 100644 --- a/src/Microsoft.AspNet.Mvc.Xml/XmlSerializerInputFormatter.cs +++ b/src/Microsoft.AspNet.Mvc.Xml/XmlSerializerInputFormatter.cs @@ -10,6 +10,7 @@ using System.Text; using System.Threading.Tasks; using System.Xml; using System.Xml.Serialization; +using Microsoft.AspNet.Mvc.Internal; using Microsoft.Net.Http.Headers; namespace Microsoft.AspNet.Mvc.Xml @@ -146,7 +147,7 @@ namespace Microsoft.AspNet.Mvc.Xml { var request = context.ActionContext.HttpContext.Request; - using (var xmlReader = CreateXmlReader(new DelegatingStream(request.Body))) + using (var xmlReader = CreateXmlReader(new NonDisposableStream(request.Body))) { var type = GetSerializableType(context.ModelType); diff --git a/src/Microsoft.AspNet.Mvc.Xml/XmlSerializerOutputFormatter.cs b/src/Microsoft.AspNet.Mvc.Xml/XmlSerializerOutputFormatter.cs index cde4a7ac53..d485d268f9 100644 --- a/src/Microsoft.AspNet.Mvc.Xml/XmlSerializerOutputFormatter.cs +++ b/src/Microsoft.AspNet.Mvc.Xml/XmlSerializerOutputFormatter.cs @@ -7,6 +7,7 @@ using System.IO; using System.Threading.Tasks; using System.Xml; using System.Xml.Serialization; +using Microsoft.AspNet.Mvc.Internal; using Microsoft.Net.Http.Headers; namespace Microsoft.AspNet.Mvc.Xml @@ -137,7 +138,7 @@ namespace Microsoft.AspNet.Mvc.Xml var innerStream = context.ActionContext.HttpContext.Response.Body; - using (var outputStream = new DelegatingStream(innerStream)) + using (var outputStream = new NonDisposableStream(innerStream)) using (var xmlWriter = CreateXmlWriter(outputStream, tempWriterSettings)) { var obj = context.Object; diff --git a/test/Microsoft.AspNet.Mvc.Core.Test/Formatters/DelegatingStreamTests.cs b/test/Microsoft.AspNet.Mvc.Core.Test/Formatters/NonDisposableStreamTest.cs similarity index 67% rename from test/Microsoft.AspNet.Mvc.Core.Test/Formatters/DelegatingStreamTests.cs rename to test/Microsoft.AspNet.Mvc.Core.Test/Formatters/NonDisposableStreamTest.cs index 8d4a09730d..8a63dc275d 100644 --- a/test/Microsoft.AspNet.Mvc.Core.Test/Formatters/DelegatingStreamTests.cs +++ b/test/Microsoft.AspNet.Mvc.Core.Test/Formatters/NonDisposableStreamTest.cs @@ -7,19 +7,19 @@ using System.Threading.Tasks; using Microsoft.AspNet.Mvc.Core; using Xunit; -namespace Microsoft.AspNet.Mvc +namespace Microsoft.AspNet.Mvc.Internal { - public class DelegatingStreamTests + public class NonDisposableStreamTest { [Fact] public void InnerStreamIsOpenOnClose() { // Arrange var innerStream = new MemoryStream(); - var delegatingStream = new DelegatingStream(innerStream); + var nonDisposableStream = new NonDisposableStream(innerStream); // Act - delegatingStream.Close(); + nonDisposableStream.Close(); // Assert Assert.True(innerStream.CanRead); @@ -30,10 +30,10 @@ namespace Microsoft.AspNet.Mvc { // Arrange var innerStream = new MemoryStream(); - var delegatingStream = new DelegatingStream(innerStream); + var nonDisposableStream = new NonDisposableStream(innerStream); // Act - delegatingStream.Dispose(); + nonDisposableStream.Dispose(); // Assert Assert.True(innerStream.CanRead); @@ -43,10 +43,10 @@ namespace Microsoft.AspNet.Mvc public void InnerStreamIsNotFlushedOnDispose() { var stream = FlushReportingStream.GetThrowingStream(); - var delegatingStream = new DelegatingStream(stream); + var nonDisposableStream = new NonDisposableStream(stream); // Act & Assert - delegatingStream.Dispose(); + nonDisposableStream.Dispose(); } [Fact] @@ -55,10 +55,10 @@ namespace Microsoft.AspNet.Mvc // Arrange var stream = FlushReportingStream.GetThrowingStream(); - var delegatingStream = new DelegatingStream(stream); + var nonDisposableStream = new NonDisposableStream(stream); // Act & Assert - delegatingStream.Close(); + nonDisposableStream.Close(); } [Fact] @@ -67,10 +67,10 @@ namespace Microsoft.AspNet.Mvc // Arrange var stream = FlushReportingStream.GetThrowingStream(); - var delegatingStream = new DelegatingStream(stream); + var nonDisposableStream = new NonDisposableStream(stream); // Act & Assert - delegatingStream.Flush(); + nonDisposableStream.Flush(); } [Fact] @@ -79,10 +79,10 @@ namespace Microsoft.AspNet.Mvc // Arrange var stream = FlushReportingStream.GetThrowingStream(); - var delegatingStream = new DelegatingStream(stream); + var nonDisposableStream = new NonDisposableStream(stream); // Act & Assert - await delegatingStream.FlushAsync(); + await nonDisposableStream.FlushAsync(); } } } diff --git a/test/WebSites/ConnegWebSite/VCardFormatter_V3.cs b/test/WebSites/ConnegWebSite/VCardFormatter_V3.cs index 00ce788393..83c75981a2 100644 --- a/test/WebSites/ConnegWebSite/VCardFormatter_V3.cs +++ b/test/WebSites/ConnegWebSite/VCardFormatter_V3.cs @@ -8,6 +8,7 @@ using System.Text; using System.Threading.Tasks; using ConnegWebSite.Models; using Microsoft.AspNet.Mvc; +using Microsoft.AspNet.Mvc.Internal; using Microsoft.Net.Http.Headers; namespace ConnegWebSite @@ -38,7 +39,7 @@ namespace ConnegWebSite builder.AppendLine(); builder.AppendLine("END:VCARD"); - var responseStream = new DelegatingStream(context.ActionContext.HttpContext.Response.Body); + var responseStream = new NonDisposableStream(context.ActionContext.HttpContext.Response.Body); using (var writer = new StreamWriter(responseStream, context.SelectedEncoding, bufferSize: 1024)) { await writer.WriteAsync(builder.ToString()); diff --git a/test/WebSites/ConnegWebSite/VCardFormatter_V4.cs b/test/WebSites/ConnegWebSite/VCardFormatter_V4.cs index 9997f07ace..acd954fa01 100644 --- a/test/WebSites/ConnegWebSite/VCardFormatter_V4.cs +++ b/test/WebSites/ConnegWebSite/VCardFormatter_V4.cs @@ -8,6 +8,7 @@ using System.Text; using System.Threading.Tasks; using ConnegWebSite.Models; using Microsoft.AspNet.Mvc; +using Microsoft.AspNet.Mvc.Internal; using Microsoft.Net.Http.Headers; namespace ConnegWebSite @@ -41,7 +42,7 @@ namespace ConnegWebSite builder.AppendLine(); builder.AppendLine("END:VCARD"); - var responseStream = new DelegatingStream(context.ActionContext.HttpContext.Response.Body); + var responseStream = new NonDisposableStream(context.ActionContext.HttpContext.Response.Body); using (var writer = new StreamWriter(responseStream, context.SelectedEncoding, bufferSize: 1024)) { await writer.WriteAsync(builder.ToString());