Skip to content

Commit

Permalink
add specific implementation for netstd2.1
Browse files Browse the repository at this point in the history
  • Loading branch information
itn3000 committed Mar 15, 2019
1 parent ff32928 commit c45e65e
Show file tree
Hide file tree
Showing 9 changed files with 176 additions and 78 deletions.
3 changes: 3 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[*.cs]
indent_size = 4
indent_style = space
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* text=auto
4 changes: 2 additions & 2 deletions PooledStream.Benchmark/PooledStream.Benchmark.csproj
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFrameworks>netcoreapp2.0;netcoreapp2.1</TargetFrameworks>
<TargetFrameworks>netcoreapp2.0;netcoreapp3.0</TargetFrameworks>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="BenchmarkDotNet" Version="0.10.14" />
<PackageReference Include="BenchmarkDotNet" Version="0.11.4" />
<PackageReference Include="CodeProject.ObjectPool" Version="3.2.2" />
<PackageReference Include="Microsoft.IO.RecyclableMemoryStream" Version="1.2.2" />
</ItemGroup>
Expand Down
6 changes: 3 additions & 3 deletions PooledStream.Benchmark/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,10 @@ class MultiPlatformConfig : ManualConfig
{
public MultiPlatformConfig()
{
Add(Job.Default.WithWarmupCount(3).WithTargetCount(3)
Add(Job.Default.WithWarmupCount(3).WithIterationCount(3)
.With(CsProjCoreToolchain.NetCoreApp20));
Add(Job.Default.WithWarmupCount(3).WithTargetCount(3)
.With(CsProjCoreToolchain.NetCoreApp21));
Add(Job.Default.WithWarmupCount(3).WithIterationCount(3)
.With(CsProjCoreToolchain.NetCoreApp30));
}
}
class Program
Expand Down
11 changes: 7 additions & 4 deletions PooledStream.Test/PooledStream.Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,13 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.3.0-preview-20170427-09" />
<PackageReference Include="xunit" Version="2.2.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.2.0" />
<ProjectReference Include="../PooledStream/PooledStream.csproj"/>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.0.1" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.1">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<ProjectReference Include="../PooledStream/PooledStream.csproj" />
</ItemGroup>

</Project>
67 changes: 1 addition & 66 deletions PooledStream/PooledMemoryStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ namespace PooledStream
using System;
using System.IO;
using System.Buffers;
public class PooledMemoryStream : Stream
public partial class PooledMemoryStream : Stream
{
/// <summary>create writable memory stream with default parameters</summary>
/// <remarks>buffer is allocated from ArrayPool.Shared</remarks>
Expand Down Expand Up @@ -63,49 +63,6 @@ public override void Flush()
{
}

public override int Read(byte[] buffer, int offset, int count)
{
int readlen = count > (int)(_Length - _Position) ? (int)(_Length - _Position) : count;
if (readlen > 0)
{
Buffer.BlockCopy(_currentbuffer
, (int)_Position
, buffer, offset
, readlen)
;
_Position += readlen;
return readlen;
}
else
{
return 0;
}
}

public override long Seek(long offset, SeekOrigin origin)
{
long oldValue = _Position;
switch ((int)origin)
{
case (int)SeekOrigin.Begin:
_Position = offset;
break;
case (int)SeekOrigin.End:
_Position = _Length - offset;
break;
case (int)SeekOrigin.Current:
_Position += offset;
break;
default:
throw new InvalidOperationException("unknown SeekOrigin");
}
if (_Position < 0 || _Position > _Length)
{
_Position = oldValue;
throw new IndexOutOfRangeException();
}
return _Position;
}
void ReallocateBuffer(int minimumRequired)
{
var tmp = m_Pool.Rent(minimumRequired);
Expand Down Expand Up @@ -133,28 +90,6 @@ public override void SetLength(long value)
ReallocateBuffer((int)_Length);
}
}
/// <summary>write data to stream</summary>
/// <remarks>if stream data length is over int.MaxValue, this method throws IndexOutOfRangeException</remarks>
public override void Write(byte[] buffer, int offset, int count)
{
if (!_CanWrite)
{
throw new InvalidOperationException("stream is readonly");
}
long endOffset = _Position + count;
if (endOffset > _currentbuffer.Length)
{
ReallocateBuffer((int)(endOffset) * 2);
}
Buffer.BlockCopy(buffer, offset,
_currentbuffer, (int)_Position, count);
if (endOffset > _Length)
{
_Length = endOffset;
}
_Position = endOffset;
}

protected override void Dispose(bool disposing)
{
base.Dispose(disposing);
Expand Down
76 changes: 76 additions & 0 deletions PooledStream/PooledMemoryStream.netstd11.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
namespace PooledStream
{
#if NETSTANDARD1_1
using System;
using System.IO;
using System.Buffers;
public partial class PooledMemoryStream : Stream
{
public override int Read(byte[] buffer, int offset, int count)
{
int readlen = count > (int)(_Length - _Position) ? (int)(_Length - _Position) : count;
if (readlen > 0)
{
Buffer.BlockCopy(_currentbuffer
, (int)_Position
, buffer, offset
, readlen)
;
_Position += readlen;
return readlen;
}
else
{
return 0;
}
}

public override long Seek(long offset, SeekOrigin origin)
{
long oldValue = _Position;
switch ((int)origin)
{
case (int)SeekOrigin.Begin:
_Position = offset;
break;
case (int)SeekOrigin.End:
_Position = _Length - offset;
break;
case (int)SeekOrigin.Current:
_Position += offset;
break;
default:
throw new InvalidOperationException("unknown SeekOrigin");
}
if (_Position < 0 || _Position > _Length)
{
_Position = oldValue;
throw new IndexOutOfRangeException();
}
return _Position;
}
/// <summary>write data to stream</summary>
/// <remarks>if stream data length is over int.MaxValue, this method throws IndexOutOfRangeException</remarks>
public override void Write(byte[] buffer, int offset, int count)
{
if (!_CanWrite)
{
throw new InvalidOperationException("stream is readonly");
}
long endOffset = _Position + count;
if (endOffset > _currentbuffer.Length)
{
ReallocateBuffer((int)(endOffset) * 2);
}
Buffer.BlockCopy(buffer, offset,
_currentbuffer, (int)_Position, count);
if (endOffset > _Length)
{
_Length = endOffset;
}
_Position = endOffset;
}

}
#endif
}
79 changes: 79 additions & 0 deletions PooledStream/PooledMemoryStream.netstd21.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
namespace PooledStream
{
#if NETSTANDARD2_1
using System;
using System.IO;
using System.Buffers;
public partial class PooledMemoryStream : Stream
{
public override int Read(Span<byte> buffer)
{
int readlen = buffer.Length > (int)(_Length - _Position) ? (int)(_Length - _Position) : buffer.Length;
if(readlen > 0)
{
_currentbuffer.AsSpan((int)_Position, readlen).CopyTo(buffer);
_Position += readlen;
return readlen;
}
else
{
return 0;
}
}
public override int Read(byte[] buffer, int offset, int count)
{
return Read(buffer.AsSpan(offset, count));
}

public override long Seek(long offset, SeekOrigin origin)
{
long oldValue = _Position;
switch ((int)origin)
{
case (int)SeekOrigin.Begin:
_Position = offset;
break;
case (int)SeekOrigin.End:
_Position = _Length - offset;
break;
case (int)SeekOrigin.Current:
_Position += offset;
break;
default:
throw new InvalidOperationException("unknown SeekOrigin");
}
if (_Position < 0 || _Position > _Length)
{
_Position = oldValue;
throw new IndexOutOfRangeException();
}
return _Position;
}
public override void Write(ReadOnlySpan<byte> buffer)
{
if (!_CanWrite)
{
throw new InvalidOperationException("stream is readonly");
}
long endOffset = _Position + buffer.Length;
if (endOffset > _currentbuffer.Length)
{
ReallocateBuffer((int)(endOffset) * 2);
}
buffer.CopyTo(_currentbuffer.AsSpan((int)_Position));
if (endOffset > _Length)
{
_Length = endOffset;
}
_Position = endOffset;
}
/// <summary>write data to stream</summary>
/// <remarks>if stream data length is over int.MaxValue, this method throws IndexOutOfRangeException</remarks>
public override void Write(byte[] buffer, int offset, int count)
{
Write(buffer.AsSpan(offset, count));
}

}
#endif
}
7 changes: 4 additions & 3 deletions PooledStream/PooledStream.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard1.1</TargetFramework>
<TargetFrameworks>netstandard1.1;netstandard2.1</TargetFrameworks>
<!-- <TargetFramework>netstandard2.1</TargetFramework> -->
<PackageId>PooledStream</PackageId>
<PackageVersion>0.1.0</PackageVersion>
<Authors>itn3000</Authors>
Expand All @@ -16,7 +17,7 @@
</PropertyGroup>
<ItemGroup>
</ItemGroup>
<ItemGroup>
<PackageReference Include="System.Buffers" Version="4.3.0" />
<ItemGroup Condition="'$(TargetFramework)' == 'netstandard1.1'">
<PackageReference Include="System.Buffers" Version="4.5.0" />
</ItemGroup>
</Project>

0 comments on commit c45e65e

Please sign in to comment.