Skip to content
This repository has been archived by the owner on Dec 14, 2018. It is now read-only.

Commit

Permalink
Issue #1944: Move DelegatingStream to internal namespace and rename t…
Browse files Browse the repository at this point in the history
…o NonDisposableStream.
  • Loading branch information
sornaks committed Feb 3, 2015
1 parent c669f7a commit 2c5ae68
Show file tree
Hide file tree
Showing 9 changed files with 33 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -68,8 +69,8 @@ public override Task WriteResponseBodyAsync(OutputFormatterContext context)
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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,22 @@
using System.Threading;
using System.Threading.Tasks;

namespace Microsoft.AspNet.Mvc
namespace Microsoft.AspNet.Mvc.Internal
{
/// <summary>
/// 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.
/// </summary>
public class DelegatingStream : Stream
public class NonDisposableStream : Stream
{
private readonly Stream _innerStream;

/// <summary>
/// Initializes a new <see cref="DelegatingStream"/>.
/// Initializes a new <see cref="NonDisposableStream"/>.
/// </summary>
/// <param name="innerStream">The stream which should not be closed or flushed.</param>
public DelegatingStream([NotNull] Stream innerStream)
public NonDisposableStream([NotNull] Stream innerStream)
{
_innerStream = innerStream;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
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
Expand Down Expand Up @@ -168,7 +169,7 @@ private Task<object> ReadInternalAsync(InputFormatterContext context)
{
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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
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
Expand Down Expand Up @@ -161,7 +162,7 @@ public override Task WriteResponseBodyAsync([NotNull] OutputFormatterContext con

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;
Expand Down
3 changes: 2 additions & 1 deletion src/Microsoft.AspNet.Mvc.Xml/XmlSerializerInputFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
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
Expand Down Expand Up @@ -146,7 +147,7 @@ private Task<object> ReadInternalAsync(InputFormatterContext context)
{
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);

Expand Down
3 changes: 2 additions & 1 deletion src/Microsoft.AspNet.Mvc.Xml/XmlSerializerOutputFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
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
Expand Down Expand Up @@ -137,7 +138,7 @@ public override Task WriteResponseBodyAsync([NotNull] OutputFormatterContext con

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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,19 @@
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);
Expand All @@ -30,10 +30,10 @@ public void InnerStreamIsOpenOnDispose()
{
// 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);
Expand All @@ -43,10 +43,10 @@ public void InnerStreamIsOpenOnDispose()
public void InnerStreamIsNotFlushedOnDispose()
{
var stream = FlushReportingStream.GetThrowingStream();
var delegatingStream = new DelegatingStream(stream);
var nonDisposableStream = new NonDisposableStream(stream);

// Act & Assert
delegatingStream.Dispose();
nonDisposableStream.Dispose();
}

[Fact]
Expand All @@ -55,10 +55,10 @@ public void InnerStreamIsNotFlushedOnClose()
// Arrange
var stream = FlushReportingStream.GetThrowingStream();

var delegatingStream = new DelegatingStream(stream);
var nonDisposableStream = new NonDisposableStream(stream);

// Act & Assert
delegatingStream.Close();
nonDisposableStream.Close();
}

[Fact]
Expand All @@ -67,10 +67,10 @@ public void InnerStreamIsNotFlushedOnFlush()
// Arrange
var stream = FlushReportingStream.GetThrowingStream();

var delegatingStream = new DelegatingStream(stream);
var nonDisposableStream = new NonDisposableStream(stream);

// Act & Assert
delegatingStream.Flush();
nonDisposableStream.Flush();
}

[Fact]
Expand All @@ -79,10 +79,10 @@ public async Task InnerStreamIsNotFlushedOnFlushAsync()
// Arrange
var stream = FlushReportingStream.GetThrowingStream();

var delegatingStream = new DelegatingStream(stream);
var nonDisposableStream = new NonDisposableStream(stream);

// Act & Assert
await delegatingStream.FlushAsync();
await nonDisposableStream.FlushAsync();
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion test/WebSites/ConnegWebSite/VCardFormatter_V3.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using System.Threading.Tasks;
using ConnegWebSite.Models;
using Microsoft.AspNet.Mvc;
using Microsoft.AspNet.Mvc.Internal;
using Microsoft.Net.Http.Headers;

namespace ConnegWebSite
Expand Down Expand Up @@ -38,7 +39,7 @@ public override async Task WriteResponseBodyAsync(OutputFormatterContext context
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());
Expand Down
3 changes: 2 additions & 1 deletion test/WebSites/ConnegWebSite/VCardFormatter_V4.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using System.Threading.Tasks;
using ConnegWebSite.Models;
using Microsoft.AspNet.Mvc;
using Microsoft.AspNet.Mvc.Internal;
using Microsoft.Net.Http.Headers;

namespace ConnegWebSite
Expand Down Expand Up @@ -41,7 +42,7 @@ public override async Task WriteResponseBodyAsync(OutputFormatterContext context
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());
Expand Down

0 comments on commit 2c5ae68

Please sign in to comment.