From c45e65edfec1c66c07c2d9a33e7ddc92cc2a6f15 Mon Sep 17 00:00:00 2001 From: itn3000 Date: Fri, 15 Mar 2019 14:06:40 +0900 Subject: [PATCH] add specific implementation for netstd2.1 --- .editorconfig | 3 + .gitattributes | 1 + .../PooledStream.Benchmark.csproj | 4 +- PooledStream.Benchmark/Program.cs | 6 +- PooledStream.Test/PooledStream.Test.csproj | 11 ++- PooledStream/PooledMemoryStream.cs | 67 +--------------- PooledStream/PooledMemoryStream.netstd11.cs | 76 ++++++++++++++++++ PooledStream/PooledMemoryStream.netstd21.cs | 79 +++++++++++++++++++ PooledStream/PooledStream.csproj | 7 +- 9 files changed, 176 insertions(+), 78 deletions(-) create mode 100644 .editorconfig create mode 100644 .gitattributes create mode 100644 PooledStream/PooledMemoryStream.netstd11.cs create mode 100644 PooledStream/PooledMemoryStream.netstd21.cs diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..4850092 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,3 @@ +[*.cs] +indent_size = 4 +indent_style = space \ No newline at end of file diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..2125666 --- /dev/null +++ b/.gitattributes @@ -0,0 +1 @@ +* text=auto \ No newline at end of file diff --git a/PooledStream.Benchmark/PooledStream.Benchmark.csproj b/PooledStream.Benchmark/PooledStream.Benchmark.csproj index e9a78d6..c89c36a 100644 --- a/PooledStream.Benchmark/PooledStream.Benchmark.csproj +++ b/PooledStream.Benchmark/PooledStream.Benchmark.csproj @@ -1,10 +1,10 @@ Exe - netcoreapp2.0;netcoreapp2.1 + netcoreapp2.0;netcoreapp3.0 - + diff --git a/PooledStream.Benchmark/Program.cs b/PooledStream.Benchmark/Program.cs index f55de11..d439fb6 100644 --- a/PooledStream.Benchmark/Program.cs +++ b/PooledStream.Benchmark/Program.cs @@ -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 diff --git a/PooledStream.Test/PooledStream.Test.csproj b/PooledStream.Test/PooledStream.Test.csproj index a623966..483b28f 100644 --- a/PooledStream.Test/PooledStream.Test.csproj +++ b/PooledStream.Test/PooledStream.Test.csproj @@ -7,10 +7,13 @@ - - - - + + + + runtime; build; native; contentfiles; analyzers; buildtransitive + all + + diff --git a/PooledStream/PooledMemoryStream.cs b/PooledStream/PooledMemoryStream.cs index 50bb19a..bc3db31 100644 --- a/PooledStream/PooledMemoryStream.cs +++ b/PooledStream/PooledMemoryStream.cs @@ -3,7 +3,7 @@ namespace PooledStream using System; using System.IO; using System.Buffers; - public class PooledMemoryStream : Stream + public partial class PooledMemoryStream : Stream { /// create writable memory stream with default parameters /// buffer is allocated from ArrayPool.Shared @@ -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); @@ -133,28 +90,6 @@ public override void SetLength(long value) ReallocateBuffer((int)_Length); } } - /// write data to stream - /// if stream data length is over int.MaxValue, this method throws IndexOutOfRangeException - 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); diff --git a/PooledStream/PooledMemoryStream.netstd11.cs b/PooledStream/PooledMemoryStream.netstd11.cs new file mode 100644 index 0000000..c0c98e1 --- /dev/null +++ b/PooledStream/PooledMemoryStream.netstd11.cs @@ -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; + } + /// write data to stream + /// if stream data length is over int.MaxValue, this method throws IndexOutOfRangeException + 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 +} \ No newline at end of file diff --git a/PooledStream/PooledMemoryStream.netstd21.cs b/PooledStream/PooledMemoryStream.netstd21.cs new file mode 100644 index 0000000..e816d9a --- /dev/null +++ b/PooledStream/PooledMemoryStream.netstd21.cs @@ -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 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 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; + } + /// write data to stream + /// if stream data length is over int.MaxValue, this method throws IndexOutOfRangeException + public override void Write(byte[] buffer, int offset, int count) + { + Write(buffer.AsSpan(offset, count)); + } + + } +#endif +} \ No newline at end of file diff --git a/PooledStream/PooledStream.csproj b/PooledStream/PooledStream.csproj index 77a8c98..6c01907 100644 --- a/PooledStream/PooledStream.csproj +++ b/PooledStream/PooledStream.csproj @@ -1,7 +1,8 @@ - netstandard1.1 + netstandard1.1;netstandard2.1 + PooledStream 0.1.0 itn3000 @@ -16,7 +17,7 @@ - - + + \ No newline at end of file