Skip to content

Commit

Permalink
Multitarget
Browse files Browse the repository at this point in the history
  • Loading branch information
JanKrivanek committed Nov 3, 2023
1 parent 39ea36f commit db463ae
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 33 deletions.
5 changes: 3 additions & 2 deletions src/DotUtils.StreamUtils/DotUtils.StreamUtils.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@

<PropertyGroup>
<OutputType>Library</OutputType>
<TargetFramework>net6.0</TargetFramework>
<TargetFrameworks>net472;net6.0</TargetFrameworks>
<LangVersion>latest</LangVersion>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<IsPackable>true</IsPackable>
<!-- packaging properties -->
<PackageVersion>0.0.3</PackageVersion>
<PackageVersion>0.0.4</PackageVersion>
<Title>DotUtils.StreamUtils library</Title>
<PackageId>DotUtils.StreamUtils</PackageId>
<PackageReadmeFile>README.md</PackageReadmeFile>
Expand Down
39 changes: 34 additions & 5 deletions src/DotUtils.StreamUtils/StreamExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System.Buffers;
#if NET
using System.Buffers;
#endif
using System.Diagnostics;

namespace DotUtils.StreamUtils;
Expand Down Expand Up @@ -30,19 +32,46 @@ public static int ReadAtLeast(this Stream stream, byte[] buffer, int offset, int
return totalRead;
}

public static int SkipBytes(this Stream stream, int bytesCount, bool throwOnEndOfStream)
private static bool CheckIsSkipNeeded(long bytesCount)
{
byte[] buffer = ArrayPool<byte>.Shared.Rent(4096);
if (bytesCount is < 0 or > int.MaxValue)
{
throw new ArgumentOutOfRangeException(nameof(bytesCount), $"Attempt to skip {bytesCount} bytes, only non-negative offset up to int.MaxValue is allowed.");
}

return bytesCount > 0;
}

public static int SkipBytes(this Stream stream, long bytesCount, bool throwOnEndOfStream)
{
if (!CheckIsSkipNeeded(bytesCount))
{
return 0;
}

const int bufferSize = 4096;
byte[] buffer;

#if NET
buffer = ArrayPool<byte>.Shared.Rent(bufferSize);
using var _ = new CleanupScope(() => ArrayPool<byte>.Shared.Return(buffer));
#else
buffer = new byte[bufferSize];
#endif
return SkipBytes(stream, bytesCount, throwOnEndOfStream, buffer);
}

public static int SkipBytes(this Stream stream, int bytesCount, bool throwOnEndOfStream, byte[] buffer)
public static int SkipBytes(this Stream stream, long bytesCount, bool throwOnEndOfStream, byte[] buffer)
{
if (!CheckIsSkipNeeded(bytesCount))
{
return 0;
}

int totalRead = 0;
while (totalRead < bytesCount)
{
int read = stream.Read(buffer, 0, Math.Min(bytesCount - totalRead, buffer.Length));
int read = stream.Read(buffer, 0, (int)Math.Min(bytesCount - totalRead, buffer.Length));
if (read == 0)
{
if (throwOnEndOfStream)
Expand Down
31 changes: 5 additions & 26 deletions src/DotUtils.StreamUtils/TransparentReadStream.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System.Buffers;
#if NET
using System.Buffers;
#endif

namespace DotUtils.StreamUtils;

Expand Down Expand Up @@ -47,7 +49,7 @@ public int? BytesCountAllowedToRead
public override long Position
{
get => _position;
set => SkipBytes(value - _position);
set => this.SkipBytes(value - _position, true);
}

public override void Flush()
Expand All @@ -74,7 +76,7 @@ public override long Seek(long offset, SeekOrigin origin)
throw new InvalidOperationException("Only seeking from SeekOrigin.Current is supported.");
}

SkipBytes(offset);
this.SkipBytes(offset, true);

return _position;
}
Expand All @@ -89,29 +91,6 @@ public override void Write(byte[] buffer, int offset, int count)
throw new InvalidOperationException("Writing is not supported.");
}

private void SkipBytes(long count)
{
if(count < 0)
{
throw new InvalidOperationException("Seeking backwards is not supported.");
}

if(count == 0)
{
return;
}

byte[] buffer = ArrayPool<byte>.Shared.Rent((int)count);
try
{
_position += _stream.ReadAtLeast(buffer, 0, (int)count, throwOnEndOfStream: true);
}
finally
{
ArrayPool<byte>.Shared.Return(buffer);
}
}

public override void Close() => _stream.Close();
}

0 comments on commit db463ae

Please sign in to comment.