Skip to content

Commit 690bbfb

Browse files
committed
- v10.0.2
1 parent 8313124 commit 690bbfb

5 files changed

Lines changed: 43 additions & 55 deletions

File tree

Shuttle.Core.Streams.Tests/Shuttle.Core.Streams.Tests.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFrameworks>netcoreapp2.0;net46</TargetFrameworks>
4+
<TargetFrameworks>netcoreapp2.1;net461</TargetFrameworks>
55
</PropertyGroup>
66

77
<ItemGroup>

Shuttle.Core.Streams.Tests/StreamExtensionsFixture.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public void Should_be_able_to_make_a_readonly_copy_of_a_stream()
5050
Assert.AreEqual(5, output.Position);
5151
Assert.AreEqual(5, stream.Position);
5252

53-
var copy = stream.CopyMemoryStream();
53+
var copy = stream.Copy();
5454

5555
Assert.AreEqual(5, copy.Length);
5656
Assert.AreEqual(0, copy.Position);
Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,10 @@
11
using System.Reflection;
22
using System.Runtime.InteropServices;
33

4-
#if NET46
5-
[assembly: AssemblyTitle(".NET Framework 4.6")]
6-
#endif
7-
84
#if NET461
95
[assembly: AssemblyTitle(".NET Framework 4.6.1")]
106
#endif
117

12-
#if NET462
13-
[assembly: AssemblyTitle(".NET Framework 4.6.2")]
14-
#endif
15-
16-
#if NET47
17-
[assembly: AssemblyTitle(".NET Framework 4.7")]
18-
#endif
19-
20-
#if NET471
21-
[assembly: AssemblyTitle(".NET Framework 4.7.1")]
22-
#endif
23-
24-
#if NETCOREAPP2_0
25-
[assembly: AssemblyTitle(".NET Core 2.0")]
26-
#endif
27-
288
#if NETCOREAPP2_1
299
[assembly: AssemblyTitle(".NET Core 2.1")]
3010
#endif
@@ -33,10 +13,10 @@
3313
[assembly: AssemblyTitle(".NET Standard 2.0")]
3414
#endif
3515

36-
[assembly: AssemblyVersion("10.0.1.0")]
16+
[assembly: AssemblyVersion("10.0.2.0")]
3717
[assembly: AssemblyCopyright("Copyright © Eben Roux 2018")]
3818
[assembly: AssemblyProduct("Shuttle.Core.Streams")]
3919
[assembly: AssemblyCompany("Shuttle")]
4020
[assembly: AssemblyConfiguration("Release")]
41-
[assembly: AssemblyInformationalVersion("10.0.1")]
21+
[assembly: AssemblyInformationalVersion("10.0.2")]
4222
[assembly: ComVisible(false)]

Shuttle.Core.Streams/Shuttle.Core.Streams.csproj

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFrameworks>net46;net461;net462;net47;net471;netstandard2.0;netcoreapp2.0;netcoreapp2.1</TargetFrameworks>
4+
<TargetFrameworks>net461;netstandard2.0;netcoreapp2.1</TargetFrameworks>
55
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
66
</PropertyGroup>
77

@@ -16,4 +16,19 @@
1616
<PackageReference Include="Shuttle.Core.Contract" Version="10.0.1" />
1717
</ItemGroup>
1818

19+
<ItemGroup>
20+
<Compile Update="Resources.Designer.cs">
21+
<DesignTime>True</DesignTime>
22+
<AutoGen>True</AutoGen>
23+
<DependentUpon>Resources.resx</DependentUpon>
24+
</Compile>
25+
</ItemGroup>
26+
27+
<ItemGroup>
28+
<EmbeddedResource Update="Resources.resx">
29+
<Generator>ResXFileCodeGenerator</Generator>
30+
<LastGenOutput>Resources.Designer.cs</LastGenOutput>
31+
</EmbeddedResource>
32+
</ItemGroup>
33+
1934
</Project>

Shuttle.Core.Streams/StreamExtensions.cs

Lines changed: 23 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System;
12
using System.IO;
23
using Shuttle.Core.Contract;
34

@@ -19,46 +20,38 @@ public static byte[] ToBytes(this Stream stream)
1920
}
2021

2122
/// <summary>
22-
/// Copies the stream to a new MemoryStream object without copying the internal buffer.
23-
/// This method returning the same result as the <see cref="Copy"/> method when the instance it not a <see cref="MemoryStream"/>
23+
/// Returns a copy of the given stream. The underlying type used is a `MemoryStream` and if the given `stream` is a `MemoryStream` the operation will attempt to use internal buffer if exposed and return a read-only stream; else a standard `MemoryStream` is used and the `stream` data copied to the that.
2424
/// </summary>
25-
/// <param name="stream">Input stream</param>
26-
/// <returns>Memory stream</returns>
27-
public static MemoryStream CopyMemoryStream(this Stream stream)
25+
/// <param name="stream">The `Stream` instance that contains the source data.</param>
26+
/// <returns>A new `MemoryStream` object.</returns>
27+
public static Stream Copy(this Stream stream)
2828
{
2929
Guard.AgainstNull(stream, nameof(stream));
3030

31-
if (stream is MemoryStream)
31+
MemoryStream result;
32+
33+
if (stream is MemoryStream ms && ms.TryGetBuffer(out var buffer))
3234
{
33-
var ms = (MemoryStream) stream;
34-
if (ms.TryGetBuffer(out var buffer))
35-
{
36-
return new MemoryStream(buffer.Array, buffer.Offset, (int) ms.Length, false, true);
37-
}
35+
result = new MemoryStream(buffer.Array ?? throw new InvalidOperationException(Resources.CopyBufferArrayException), buffer.Offset, (int)ms.Length, false, true);
3836
}
39-
40-
return (MemoryStream)Copy(stream);
41-
}
42-
43-
public static Stream Copy(this Stream stream)
44-
{
45-
Guard.AgainstNull(stream, nameof(stream));
46-
47-
var result = new MemoryStream {Capacity = (int) stream.Length};
37+
else
38+
{
39+
result = new MemoryStream {Capacity = (int) stream.Length};
4840

49-
var originalPosition = stream.Position;
41+
var originalPosition = stream.Position;
5042

51-
try
52-
{
53-
stream.Seek(0, SeekOrigin.Begin);
43+
try
44+
{
45+
stream.Seek(0, SeekOrigin.Begin);
5446

55-
stream.CopyTo(result);
47+
stream.CopyTo(result);
5648

57-
result.Seek(0, SeekOrigin.Begin);
58-
}
59-
finally
60-
{
61-
stream.Seek(originalPosition, SeekOrigin.Begin);
49+
result.Seek(0, SeekOrigin.Begin);
50+
}
51+
finally
52+
{
53+
stream.Seek(originalPosition, SeekOrigin.Begin);
54+
}
6255
}
6356

6457
return result;

0 commit comments

Comments
 (0)