Skip to content

Commit

Permalink
CoreFx #22409 StreamWriter Span based API and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
WinCPP committed Sep 9, 2017
1 parent e150f00 commit b687d8b
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using Xunit;

namespace System.IO.Tests
{
public partial class WriteTests
{
[Fact]
public void WriteReadOnlySpanTest()
{
char[] chArr = setupArray();
ReadOnlySpan<char> readSpan = new ReadOnlySpan<char>(chArr);

Stream ms = CreateStream();
StreamWriter sw = new StreamWriter(ms);
StreamReader sr;

sw.Write(readSpan);
sw.Flush();
ms.Position = 0;
sr = new StreamReader(ms);

for (int i = 0; i < chArr.Length; i++)
{
Assert.Equal((int)chArr[i], sr.Read());
}
ms.Dispose();
}

[Fact]
public void WriteLineReadOnlySpanTest()
{
char[] chArr = setupArray();
ReadOnlySpan<char> readSpan = new ReadOnlySpan<char>(chArr);

Stream ms = CreateStream();
StreamWriter sw = new StreamWriter(ms);
StreamReader sr;

sw.Write(readSpan);
sw.Flush();
ms.Position = 0;
sr = new StreamReader(ms);

string readData = sr.ReadLine();
Assert.Equal(new string(chArr), readData);

ms.Dispose();
}
}
}
1 change: 1 addition & 0 deletions src/System.IO/tests/System.IO.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
<Compile Include="IndentedTextWriter.cs" />
<Compile Include="BinaryReader\BinaryReaderTests.cs" />
<Compile Include="MemoryStream\MemoryStream.GetBufferTests.cs" />
<Compile Include="StreamWriter\StreamWriter.WriteTests.netcoreapp.cs" Condition="'$(TargetGroup)' == 'netcoreapp'" />
<Compile Include="Stream\Stream.cs" />
<Compile Include="StreamReader\StreamReader.cs" />
<Compile Include="StreamReader\StreamReader.StringCtorTests.cs" />
Expand Down
23 changes: 22 additions & 1 deletion src/System.Runtime.Extensions/src/System/IO/StreamWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System.Buffers;
using System.Diagnostics;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;

namespace System.IO
{
Expand Down Expand Up @@ -446,6 +447,21 @@ public override void Write(char[] buffer, int index, int count)
}
}

public override void Write(ReadOnlySpan<char> source)
{
char[] buffer = ArrayPool<char>.Shared.Rent(source.Length);

try
{
source.CopyTo(new Span<char>(buffer));
Write(buffer, 0, source.Length);
}
finally
{
ArrayPool<char>.Shared.Return(buffer);
}
}

public override void Write(string value)
{
if (value == null)
Expand Down Expand Up @@ -555,6 +571,11 @@ public override void WriteLine(string value)
}
}

public override void WriteLine(ReadOnlySpan<char> source)
{
WriteLine(new string(source.ToArray()));
}

#region Task based Async APIs
public override Task WriteAsync(char value)
{
Expand Down

0 comments on commit b687d8b

Please sign in to comment.